CheckRules() on business type itself rules.

CheckRules() on business type itself rules.

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


rxelizondo posted on Saturday, January 28, 2012

Hello,

I have a rule scoped to the business type itself and not to a specific property (the type of rule that takes a null as the property the rule is associated to).

What I would like to do now is to be able to execute this rule and *only* this rule when I need to do so. The problem is that since the rule is not associated to a specific property I can’t do something like: “CheckRule(SomeTypeItselfProperty)”, instead the only way that I see I can run type itself rules is by doing a global “CheckRules()” call but this has the unfortunate side effect of running all the rules and not just type itself rules.

Other than creating a bogus property to associate type itself rules to that property and be able to run only the rules associate to the bogus property, is there another way to only run type itself rules? I tried “CheckRules(null)” but that crashes.

Thanks.

JonnyBee replied on Saturday, January 28, 2012

Starting from CSLA 4.2, you can call <bo>.CheckObjectRules() to run all "object" level rules and raise OnPropertyChanged for AffectedProperties.

You can call this method on an object that is active in databinding.

Or if on a previous version  - you can add the following code to your BusinessBase class:

    /// <summary>
    /// Checks the object rules and notifies UI of properties that may have changed.
    /// </summary>
    protected virtual void CheckObjectRules()
    {
      var propertyNames = BusinessRules.CheckObjectRules();
      if (ApplicationContext.PropertyChangedMode == ApplicationContext.PropertyChangedModes.Windows)
      {
        OnUnknownPropertyChanged();
      }
      else
        foreach (var name in propertyNames)
          OnPropertyChanged(name);
    }

rxelizondo replied on Sunday, January 29, 2012

 

Thanks Jonny, thats exelent news.

We are not using the CSLA 4.2 as of now (we are using 4.1) but I think I will update the project this coming Monday to start using it and take advantage of the new features.

Thanks.

rxelizondo replied on Tuesday, January 31, 2012

Just out of curios, is there a way to associate a specific property to a type rule? Something like:

 

BusinessRules.AddRule(new Dependency(SomeProperty, <PropertyThaPointsToTypeItselfRule> ));

 

In this case, when the “SomeProerty” is changed, I would like all type rules to run. As of now, I am handling the situation by overriding the “OnPropertyChanged” as in:

protected override void OnPropertyChanged(string propertyName)

{

if (propertyName == SomeProperty.Name)

       {

              this.BusinessRules.CheckObjectRules();

}

}

Thanks.

JonnyBee replied on Thursday, February 02, 2012

Hi,

I'd recomment to do this with an override of  PropertyHasChanged and call <bo>.CheckObjectRules so that you will get an OnPropertyChanged for AffectedProperties from the object rules. 

Having this code in the OnPropertChanged may create recursive calls/infinite loops as OnPropertyChanged is called from both PropertyHasChanged and CheckObjectRules.

Copyright (c) Marimer LLC