How to validate value in child object against value in another child object?

How to validate value in child object against value in another child object?

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


reagan123 posted on Monday, February 23, 2009

Okay,
I'll see if I can describe this issue.  I have a parent object that has a child collection.  I need to be able to validate the a value in one of the child objects is less than the value in the other child object.

Is there a way to do this?

I hope that made sense. :)
                   

jasona22 replied on Tuesday, February 24, 2009

I have a similar scenario.  I have a collection of child objects and I need to compare values between them.  Since the children don't know anything about one another (as it should be), when the custom validation fires, I need to get a reference to the parent so I can have the parent compare values on the children.

Calling "Parent" on the child only provides "AcceptEdit" and "RemoveChild".  So, since the value that needs to fire validation is on the child, the child doesn't know about its siblings and the only parent reference doesn't seem to give access to the child collection, I have created my own.

This sounds easy, but apparently not.  I added a parameter to the ctor of my child and upon creation the parent sends a reference to the child. I take this reference and set a public property on the child (ParentReference).

When the custom validation function fires, I am able to access my ParentReference property and get a reference to the parent.  At that point, I can iterate over my collection of children and do my validation.

Where the problem occurs is when the custom function does fire and the rule fails, the code seems to go into an endless loop over the ParentCollection property on the child and the call stack says external.  Since I just added this property and only for the purpose of validation, nothing external should be calling it; so I presume it is something in the CSLA engine that is.

Has anyone attempted something similar and have any suggestions or hints?

Thanks

JoeFallon1 replied on Tuesday, February 24, 2009

I thought Rocky added a Parent property in CSLA 3.5 or 3.6 to resolve this sort of thing. It did not exist in 1.x or 2.x.

So a Root BO with a Child Collection filled with Child objects would look like this when inverted:

Child

  Child.Parent  (the collection)

    Child.Parent.Parent   (the root BO)

Not sure if any casting is required though. Plus I have not actually tried this kind of code yet.

Joe

 

 

ajj3085 replied on Tuesday, February 24, 2009

jasona22:
Calling "Parent" on the child only provides "AcceptEdit" and "RemoveChild".  So, since the value that needs to fire validation is on the child, the child doesn't know about its siblings and the only parent reference doesn't seem to give access to the child collection, I have created my own.


That's true, but you can also cast Parent to the appropriate type and access all of that types members.

jasona22:
When the custom validation function fires, I am able to access my ParentReference property and get a reference to the parent.  At that point, I can iterate over my collection of children and do my validation.

Where the problem occurs is when the custom function does fire and the rule fails, the code seems to go into an endless loop over the ParentCollection property on the child and the call stack says external.  Since I just added this property and only for the purpose of validation, nothing external should be calling it; so I presume it is something in the CSLA engine that is.

Depending on where I want validation errors to show (in the grid, or a message outside the grid), I usually put the rule into the parent... or at the very least have the parent ask the children to recheck themselves.  I think to avoid endless loops, you can turn off RaiseListChangedEvents, run your rules then re-enable it.  Alternately, you can have a flag that indicates you're checking rules and to ignore other processing.

Copyright (c) Marimer LLC