Help understanding CSLA 4 business rules subsystem

Help understanding CSLA 4 business rules subsystem

Old forum URL:

TerryH posted on Monday, October 04, 2010

Can someone please explain how "dependency" and the business rules works within the following example. On a form there are two fields that a user must enter a value,  Quantity and Price. Total is the calculated field on the form. When either Quantity or Price changes, Total should be calculated.

I have been studying the example called "BusinessRuleDemo", in the file "Root.cs" there is this method AddBusinessRules method there is this






new Dependency(Num1Property, SumProperty));

Is this saying that SumProperty is dependent on Num1Property?

BusinessRules.AddRule(new Dependency(Num2Property, SumProperty));



new Dependency(Num2Property, Num1Property));

How should this rule be understood ?




RockfordLhotka replied on Monday, October 04, 2010

A BusinessRule can have affected properties. If a rule has affected properties, then the rules for those properties will be run after the current property's rules complete. This is true for any rule.

To make this easier to understand, we created the rule named Dependency, which doesn't actually do anything as a rule - it doesn't enforce any actual rule. But what it does, is act as a vehicle through which you can attach affected properties to the current property.

So something like

new Dependency(Num1Property, Num2Property)

means Num1Property is the primary property and Num2Property is an affected property. So after the rules for Num1Property run, CSLA will run the rules for Num2Property.

Hopefully that makes sense.

Given that though, you can have some fun, since any rule can have affected properties. So you don't necessarily need the Dependency rule to get dependent rules to run.

For example, consider a TwoFieldsRequired rule

new TwoFieldsRequired(Field1Property, Field2Property)

if your rule adds Field2Property to its AffectedProperties list, then after the Field1 rules run, the Field2 rules will also be run.

Of course I would assume TwoFieldsRequired would also add Field1Property and Field2Property to its InputProperties list so it gets those values and can use them to make sure they both exist (or whatever the rule is checking).

JonnyBee replied on Tuesday, October 05, 2010


You must be aware that there is 2 different dependencies in root.cs:

1. SumProperty must be calculated and validated (in proper sequence) whenever Num1Property or Num2Property are changed). Hence:

      BusinessRules.AddRule(new Dependency(Num1Property, SumProperty));
      BusinessRules.AddRule(new Dependency(Num2Property, SumProperty));

      BusinessRules.AddRule(new CalcSum(SumProperty, Num1Property, Num2Property) { Priority = -1 });
      BusinessRules.AddRule(new MinValue<int>(SumProperty, 1));

2. Num1Property must be less than Num2Property so whenever Num2Changes rules for Num1Property must also be checked:

      BusinessRules.AddRule(new Dependency(Num2Property, Num1Property));              // when Num2 is changed also check rules for Num1
      BusinessRules.AddRule(new LessThanProperty(Num1Property, Num2Property));   // rule is caled whenever Num1 is changed

Unless you specify a priority  rules are assigned Priority = 0. So setting Priority = -1 will make that rule run before all rules with default priority.



Copyright (c) Marimer LLC