Recursive Validation Question

Posts   
 
    
MarcoP avatar
MarcoP
User
Posts: 270
Joined: 29-Sep-2004
# Posted on: 30-May-2006 21:23:38   

I am trying to write a recursive routine that will call the validate method on all my entities when a save takes place. I know they are called by the runtime automatically, but I want my client-side code to be able to take advantage of this without wasting a trip to the middle tier. I get a stack overflow exception with the following. Anyone see what's going wrong here?

        protected void Validate(IEntity2 entity)
        {
            entity.Validate();
            foreach (IEntity2 e in (EntityCollectionBase2)entity.GetDependentRelatedEntities())
            {
                Validate(e);
            }
            foreach (IEntity2 e in (EntityCollectionBase2)entity.GetDependingRelatedEntities())
            {
                Validate(e);
            }
            foreach (EntityCollectionBase2 col in entity.GetMemberEntityCollections())
            {
                foreach (IEntity2 e in col)
                {
                    Validate(e);
                }
            }
        }
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 31-May-2006 07:34:22   

Please post the stack trace.

Anyway I guess you should take the methods which returns the collections out of the for loops, because the way it's written will not get you out of the loop, every time a collection is returned and the first entity is accessed without any advance (endless loop).

Could you please try te following code instead:

protected void Validate(IEntity2 entity)
        {
            entity.Validate();
            EntityCollectionBase2 col1 = (EntityCollectionBase2)entity.GetDependentRelatedEntities();
            foreach (IEntity2 e in col1)
            {
                Validate(e);
            }

// The same idea for the rest of the loops.

        }
MarcoP avatar
MarcoP
User
Posts: 270
Joined: 29-Sep-2004
# Posted on: 31-May-2006 16:18:37   

Ok, I tried it the way you specified and I get the same result. Unfortuantely, since it causes an infinite loop, I am never able to retrieve the stack trace. Any other ideas?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Jun-2006 07:27:44   

You may trace it by placing a break point at the first line of your function and see where the call goes from there. You willeasily detect the closed loop.