Rules on affected properties

Rules on affected properties

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


g.beraudo@castsoftware.com posted on Thursday, June 06, 2013

Dear all,

I'm using CSLA 4.5.10. I'm facing trouble with some trivial rules computation:

SubTotal1 = Property1 * Property2

SubTotal2 = Property3 * Property4

Total = SubTotal1 + SubTotal2

 

I'm adding to my business object the following rules (the first parameter is the primary property, the 1st and 2nd parameters are input properties, the third parameter is an affected property):

ProdRule(Property1, Property2, SubTotal1),

ProdRule(Property3, Property3, SubTotal2),

SumRule(SubTotal1, SubTotal2, SubTotal)

 

I did understand that (if not overidden) rule only cascade 1 level. Thus that the above scenario was supported.

However, concretely this only works if 'SubTotal1' (the primary property) is affected.

This does not work when 'SubTotal2' is affected (ie. when either Property3 or Property4 were updated).


I was surprised to see that only PrimaryProperty is taken into account (instead of all InputProperties), to define which rules should run, cf. businessrules.cs ln 568:

      var rules = from r in TypeRules.Rules

                  where ReferenceEquals(r.PrimaryProperty, property)   //I was not expecting that

                    && CanRunRule(r, executionContext)

                  orderby r.Priority

 

                  select r;

 

Am I wrongly assuming that CSLA engine should be designed to have my 'Total' computed (ie. the rule run) if either 'SubTotal1' or 'SubTotal2' are modified.

 

Thanks for your light,

Regards,

Gilles

Note: I would have expected to see something like, 

  var rules = from r in TypeRules.Rules

                  where ((ReferenceEquals(r.PrimaryProperty, property)

                                  ||  (r.InputProperties != null && r.InputProperties.Contains(property))) //I was expecting that instead

                    && CanRunRule(r, executionContext)

                  orderby r.Priority

                  select r;

 

JonnyBee replied on Thursday, June 06, 2013

Hi,

In these cases I prefer to draw an analogy to Excel / Spreadsheets.

1. You put the Sum rule on the propety to be calculated

     IE: The SubTotal1, SubTotal2 and Total should be the primary property of CalcSum/ProdRule rule with the properties to sum as InputPropertiies 

2. To have them cascade down make sure to set 

     BusinessRules.CascadeOnDirtyProperties to true on that business object (in DataPortal_Create and DatePortal_Fetch)

This will make the rules cascade so long as context.AddOutValue makes a property dirty. 

Look at the CalcSum rule in the Samples/Net/cs/RuleTotorial sample for a readymade CalcSum rule. 

AsIs: when Property1 is changed the rule for SubTotal1 (where SunTotal1 is primary property) will be run as level 1. No further level rules will be run.

JonnyBee replied on Friday, June 07, 2013

Hi,

You should also read the Csla 4.5 RuleEngine Update blog post on my blog.

Copyright (c) Marimer LLC