Run rule only if property value has changed since loaded

Run rule only if property value has changed since loaded

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


ajj3085 posted on Monday, July 22, 2013

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?

cfarren replied on Monday, July 22, 2013

Hey,

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.

JonnyBee replied on Tuesday, July 23, 2013

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)

ajj3085 replied on Tuesday, July 23, 2013

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?

JonnyBee replied on Tuesday, July 23, 2013

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