Combination of Business Rule and Valdation Rule

Combination of Business Rule and Valdation Rule

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


chatzikk posted on Tuesday, October 28, 2014

I develop a WinForm application with CSLA 4.3.13 and I have the following case:

I have a class with three properties:

CurrentValue

MaxValue

Result

The validation rule is CurrentValue<MaxValue

The business rule is that changing the value of the Result property changes the value of the MaxValue property.

I wrote the following rules to implement this scenario:

BusinessRules.AddRule(new Rules.MaxValueGetFromResult(MaxValueProperty, ResultProperty) { Priority = -1 });  //Business Rule

BusinessRules.AddRule(new Rules.LessThanProperty(CurrentValueProperty, MaxValueProperty) ); //Validation Rule

My problem is that when the user changes the Result property the validation rule is executed before the business rule and for that reason it uses the old value of MaxValue property and not the one that is set after the execution of the business rule.
Thanks

JonnyBee replied on Tuesday, October 28, 2014

Hi, 

First of all - you have misunderstood how the Proritiy is used. It is only effective within rules on the same PrimaryProperty. Not across different PrimaryProperties.

So I would recommend that you change the MaxValueGetFromResult rule so that you have ResultProperty as the PrimaryProperty and MaxValueProperty as the "calculated property" and an OutputPropertyValue in that rule. Now you should also add CurrentValue as an AffectedProperty to the MaxValueGetFromResult rule. 

The RuleEngine in CSLA 4.3 will NOT cascade down. 

1. First pass is all rules where PrimaryProperty is the "changed property"

- ForEach BusinessRule - when complete - update BO from POutputProperties and add AffectedProperties to Aggregate.

2. Second pass is to rerun all rules for the distinct sum of (the aggregate of AffectedProperties in FirstPass AND PrimaryProperties from Rules where FirstPass.PrimaryProperty is an InputProperty). 

 And after this the execution stops and PropertyChanged event is raised as appropriate to notify the UI. 

So to sum it up: 

- the calculation rule should be defclared with PrimaryProperty = the changed property to run in 1)

- and the calculation rule should add all affected properties to revalidate in second pass.

chatzikk replied on Tuesday, October 28, 2014

Thank you very much. 

Copyright (c) Marimer LLC