Validate Grandchild Property against Top Level Parent Property

Validate Grandchild Property against Top Level Parent Property

Old forum URL: forums.lhotka.net/forums/t/8783.aspx


reagan123 posted on Thursday, April 08, 2010

Hello.  I currently have a scenario where I have a parent--child--grandchild relationship.  I have a property in the grandchild that needs to be validated against an item in the parent.  Can anyone explain how to do this?  I tried looking into the .Parent property, but can't figure anything out.

Can anyone explain to me how to do this?

Any help greatly appreciated!

 

Parent     <------------------------------------------------------------------------------------------------------------------
-----ChildCollection                                                                                                                                        |
-----------ChildItem                                                                                                                                          |
------------------GrandChildCollection                                                                                                           |
-----------------------------GrandChildItem  (Need to validate property in here against property here |

 

 

rsbaker0 replied on Thursday, April 08, 2010

Both BusinessBase and BusinessListBase objects have access to their parent. I've written some helper methods that let me walk arbitrarily up the parent hierarchy to locate the "ancestor" of the appropriate type. I can post more details if you think this approach would solve your problem.

reagan123 replied on Thursday, April 08, 2010

I would love to see the helper methods.  That would be great if you could give some more detail.

Thanks!

rsbaker0 replied on Friday, April 09, 2010

Here is the basic implementation. The "Parent" properties of the CSLA objects we started with weren't public, so I added an IChild interface that implements a single GetParent() method for returning the immediate parent of all of my CSLA derived classes. So you can always walk as far up the object graph as you need to.

        /// 
        /// Gets the first ancestor object of the specified type
        /// 
        /// Matching ancestor, if found.
        public static object GetAncestor(object current, Type findType)
        {
            object found = null;
            while (current is IChild && ((IChild)current).GetParent() != null)
            {
                current = ((IChild)current).GetParent();
                if (findType.IsAssignableFrom(current.GetType()))
                {
                    found = current;
                    break;
                }
            }

            return found;
        }

ajj3085 replied on Friday, April 09, 2010

You may want to rethink things if possible.  That is, the rule should be in the Parent, checking its grandchildren, not the other way around.

Its ok for a parent to know about its children (it has to), but you want avoid creating a loop by having the grandchildren know about the parent, if possible.  Having that loop increases coupling.

cousinanthony replied on Saturday, May 15, 2010

I have run into the same exact issue and was going down the same path of walking back up the object tree because I have a validation rule in the grandchild object with is dependent on a parent property.  I would be interested  to know how you would approach this issue.

 

Here is my scaled down object model

 

I have an Event which has a name, start date and end date

The event contains a collection of Tracks which simply has a name and a collection of TrackDetails

The TrackDetail contains a start date, start time, and end time

 

Event

    Tracks – (0  - n)

        TrackDetails (0 – n)

 

I have a validation rule in TrackDetail that checks if the start date is within the Event.StartDate & Event.EndDate.

 

Currently I have a property in each object that returns a reference to its  Parent using the  Ctype(Me.Parent, [type])

The Parent value seems to have the proper reference when you are creating a new object, but when an object is initialized from the database it always  seems to be null/Nothing.  So I don’t fully understand how this Property works under the covers.

 

If I understand what you are suggesting, my validation rule would need to be moved to the Event object.  My question would be is which property do I attach the broken rule to?  Would it be the property the return the Track collection?   And then from the custom validator function, loop through each track and for each track loop though each track detail and compare the start dates.

 

If that is the correct path, my next question is what is the best practice for overall validation

In the case I described above, the validation would be at the parent because there is a dependency on a parent property.

But what about child rules where there is no dependency?  For example, start and end times for the TrackDetail.  A validator is needed to ensure the end time is after the start time.  Can this remain in the grandchild property?

 

 

Thanks

 

Copyright (c) Marimer LLC