In working with CSLA for a couple of years now, I've found that validation rules can be useful, if carefully coded, for doing transformation on the object data rather than just enforcing rules or displaying error indicators to the user.
The problem is that a rule can be called for at least 3 reasons that I know of:
(1) ValidationRules.CheckRules() was explicitly invoked
(2) The property associated with the rule changed
(3) A dependent property as changed.
If there were an additional property in RuleArgs - say an enum that indicated the invocation cause and was internal to CSLA, set before invoking the rule -- this could greatly simply the coding of transformational rules (and perhaps other rules, for example, those that intend to block changes to an object after it has been locked, etc.). I'd think this could possibly be done as a non-breaking change because existing rule implementations wouldn't care about a new property in RuleArgs.
With the current implementation, the only way I know of for a rule method to be reasonably certain it is being called because the associated property actually changed is to cache the previous property value somehow and compare in the rule method, which increases the weight of the object for each such rule.
Yes, you can accomplish this otherwise by overrides on OnPropertyChanged() or hooking the PropertyChanged event, but rules are already plugged into this mechanism and the determination of which property has changed has already been done. PropertyChanged gets called all the time for unrelated properties which requires a string comparison to test for the changed property, which is also a maintenance issue. Events also have to be hooked and unhooked (and rehooked after saving). Using rules instead would certainly be convenient and wouldn't seem to add as much cost/complexity.
Thanks!
Copyright (c) Marimer LLC