Validation rule that is dependent on two objects?

Validation rule that is dependent on two objects?

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


reagan123 posted on Sunday, September 13, 2009

Hello.

I've been thinking about this for a bit today and am stumped.  Lets say I have the following two parent child objects. 

ObjectA
   ObjectAChildCollection
        ObjectAChild

ObjectB
    ObjectBChildCollection
          ObjectBChild

I have a rule that a value in the ObjectBChild cannot be greater than a value in the ObjectAChild.  How would my validation rule look in this situation and which class would it be in?  Any help greatly appreciated.

rfcdejong replied on Sunday, September 13, 2009

Lets say the name of the root object containing both childs is ObjectAB, u must have a root object for both collections, but then u won't need a ObjectA and ObjectB, only the collections as child of the root object.

ObjectAB
ObjectA
ObjectAChildCollection
ObjectAChild
ObjectB
ObjectBChildCollection
ObjectBChild

I would impliment a interface like IContainObjectA in ObjectAB which supply a ObjectAChildByName() property or something simular.

In the validation i then would do something like:
currentitem = this;
repeat
currentitemparent = currentitem.parent
currentitem = currentitemparent;
until typeof(IContainObjectA).IsAssignableFrom(currentitem)

ofcourse this is just an idea on how i would start.

tmg4340 replied on Sunday, September 13, 2009

Well... the first thing that comes to mind is that when ObjectB is created, it's passed a reference to ObjectA.  Then ObjectBChild can use the ObjectA reference (via the Parent property) to get down to the appropriate ObjectAChild object.  Or, if you can determine the correct ObjectAChild for the ObjectBChild instance at creation time, you could set a reference property on ObjectBChild at that point, saving you the navigation-property-chain calls each time your validation rule runs.  That also loosens the coupling between ObjectA and ObjectB.  It depends on how the rule is constructed - is it a one-to-one mapping of A to B, or is it "the value in B can't be greater than any value A?"

And in case you couldn't tell, I believe your validation rule belongs in ObjectBChild.  The way you've expressed your rule, ObjectBChild has the dependency on ObjectAChild.

HTH

- Scott

Copyright (c) Marimer LLC