I have a rule that should only run if the property value has changed since the object was fetched from the database. I do need to call CheckRules though before the end of the DP_F method.
Is there a way to suppress just this one rule from CheckRules?
If not, I was thinking of creating a PropertyInfo field and private instance property who's only purpose is to hold the value from the DB, and the rule would use this value as one of the input properties to determine if it should run or not. Would there be anything wrong with such an approach?
You can use the FieldManager.IsFieldDirty method. The rule will still run but if you add this check in the rule before any validation occurs you can exit the rule unless the field has been changed and is marked as dirty.
If your rule inherits from CommonBusinessRule you can set
CanRunOnServer = false
and the rule will be excluded when called in the logical serverside of the DataPortal (Data Access layer)
Thanks Jonny, that's good to know. If the rule has to be a ValidationAttribute (so that I can write an adapter to emit JS validation in MVC), do you see any problem with the BO storing the original value as a property?
Well, the problem is that you cannot set CanRunOnServer on a DataAnnotation rule.
DataAnnotation rules is wrappen in a Csla rule and the automated add will add these with all default values (including Priority = 0).
You _could_ however create the rule as a CSLA rule and then add a Dummy/Empty DataAnnotation rule to emit the JS Validation.
You may also want to look at the CustomFieldData from Jason Bock. One of the implementations is to have a BackingField that stores the old original field and the edited value to make a proper check if the field has been changed. An updated version of this is available in source of CslaContrib.
Copyright (c) Marimer LLC