CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Forum has moved

New location: CSLA .NET forum


CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • Run rule only if property value has changed since loaded

    rated by 0 users
    Answered (Verified) This post has 1 verified answer | 4 Replies | 0 Followers

    Top 10 Contributor
    4,106 Posts
    Andy posted on Mon, Jul 22 2013 10:49 AM

    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?

    Answered (Verified) Verified Answer

    Top 10 Contributor
    2,279 Posts
    Verified by Andy

    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)

    Jonny Bekkum, Norway CslaContrib Coordinator

    All Replies

    Top 500 Contributor
    19 Posts

    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.

    Top 10 Contributor
    2,279 Posts
    Verified by Andy

    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)

    Jonny Bekkum, Norway CslaContrib Coordinator

    Top 10 Contributor
    4,106 Posts
    Andy replied on Tue, Jul 23 2013 1:15 PM

    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?

    Top 10 Contributor
    2,279 Posts

    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.  

    Jonny Bekkum, Norway CslaContrib Coordinator

    Page 1 of 1 (5 items) | RSS

    Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
    Email admin@lhotka.net for support.
    Powered by Community Server (Non-Commercial Edition), by Telligent Systems