Rule in child object using properties in parent object

Rule in child object using properties in parent object

Old forum URL:

correodemarques posted on Monday, May 20, 2013

I have a rule in my children objects that uses some values from the parent object. The rule must be executed when any of those values in the parent changes. The rule also adds different error result messages depending of certain conditions.

The options I have are:

1-Add the needed properties to the child object and update its values every time the values in the parent change using PropertyHasChanged. Those properties have to be loaded also for every new child and when the children objects are being load from the database.

What I don't like about this approach is to add properties to the child that don't really belong there and all the code needed to load those properties.

 2-Create a lambda rule in the child that uses the values from the parent through the Parent property of the child object. Add some method in the child object to call BusinessRules.CheckRules(MyProperty). That method will be used from the parent object inside PropertyHasChanged to trigger the execution of the rule in the children.

In this option I don't know how to output different messages for the rule depending of conditions inside the rule code. This is how I define the rule:


o => {

                …rule code here using (MyParent)o.Parent …

}, “Broken rule message”);


My questions are:

Is there a better solution for this?

If not, how can I output different messages from the rule using the second option?

Thanks in advance.

JonnyBee replied on Monday, May 20, 2013

Which version of CSLA do you use? 

My suggestion for this solution is to override PropertyHasChanged and ChildChanged in the "parent" object.

PropertyHasChanged is only called when a property value is updated and you can loop check which property was updated and loop through the child objects to call a method to "recheck" rules for the given properties. 

As for Severity there is different solutions depending on version (3.x or 4.x) and you cannot use a lambda rule to do this.  The rules in the child object must read the values from the "parent" object and you must make sure that your rules can run even when the parent is null (ie CheckRules is called in DataPortal_Create before the object is added to the parent). 

correodemarques replied on Monday, May 20, 2013

Hi Jonny,

I'm using CSLA 4.5.

If I go with your suggestion, how can I get the parent from within the rule in the child object? Using the Target property?

And when the parent is null, I will have to make the rule to skip the execution and add code to the ChildrenList object to rerun the rules in the child after they had been created and added to the collection. Is that the right approach?

Thanks a lot for your time.


JonnyBee replied on Monday, May 20, 2013

Yes, you would use the context.Target property. 

I would "hook" into the ChildChanged event on the "parent" object to run rules on a "inserted" item. When an item is added to the list you have no guarantee that the list has been added to the parent. 




correodemarques replied on Tuesday, May 21, 2013

Great idea to use ChildChanged instead of adding the code to the list. Thank you very much for your explanations.

Copyright (c) Marimer LLC