Is there a way to suspend rule execution in a given object?

Is there a way to suspend rule execution in a given object?

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


Jav posted on Friday, May 25, 2012

I have an object hierarchy that is used in multiple "areas" in the same running application. In one of those areas, the rules should not be executed. Is there a way to do that?  All of the rule, and there are not many, are the DataAnnotation kind.

Jav

JonnyBee replied on Friday, May 25, 2012

Hi Jav,

Well, there are 2 possibilities I can think of:

1. RuleSets - DataAnnotation rules is typically added to the "default"  (actually the current ruleset) in base.AddBusinessRules.  You could create an "Empty" ruleset and have none or only a subset of rules.

2. Add ShortCircuiting rules with a priority below 0 (ex 1). DataAnnotation rules is added at Priority=0 so ShortCircuiting at a lower priority will prevent the rules from running. In order to use this you must have a "condition" to test for whether to ShortCircuit or not. .

Is the object editable when you do not want the rules to run. I you do not allow editing then it is even a third option:
3. Have an overload or flag that make CanWriteProperty always return false and use a StopIfNotCanWrite rule with Priority = -1:This can also be used with UI helpers like ReadWriteAuthorization and PropertyStatus to "diaable" UI controls.

  public class StopIfNotCanWrite : BusinessRule
  {
    public StopIfNotCanWrite(IPropertyInfo primaryProperty)
      : base(primaryProperty)
    { }

    protected override void Execute(RuleContext context)
    {
      var target = (Csla.Core.BusinessBase)context.Target;

      // Short circuit rule processing for this property if user is not allowed to edit field.
      if (!target.CanWriteProperty(PrimaryProperty))
      {
        context.AddSuccessResult(true);
      }
    }
  }

This rule and many more can be found in the RuleTutorial Sample (Csla Sample download) and CslaGenFork on CodePlex.

The 1st suggestion is probably the easiest to implement. Just set BusinessRules.RuleSet to an "unknown" rule set name and your object will have no rules.

 

Jav replied on Friday, May 25, 2012

Thanks Johny,

That was helpful.  My situation is relatively simple.

I have to deal with incoming XML mesages, which are to be deciphered and interpreted for the user, who will then answer with a message which is the converted to XML and sent back. The requirement is that incoming message be received without validation, but the outgoing messages be thoroughly vetted before being sent back.

At the heart of the process, I have a single, csla based, object hierarchy.  Everything is working great but  by my objects need to be lenient on the incoming and strict on the outgoing, and I am trying to achieve that.

Jav

 

RonHickman replied on Monday, May 28, 2012

Hey jonny , still I couldn't understand those possibilities, how it works exactly?

oil painting reproductions

JonnyBee replied on Monday, May 28, 2012

Read the blog article here: http://www.lhotka.net/weblog/CSLA4BusinessRulesSubsystem.aspx 

1. All BusinessBase objects support RuleSet (starting in 4.x) and you can have an "unknown" (or undefined) RuleSet that has no registered  rules and thus "disable" all the rules in the "default" ruleset. .

2. Rules run in the given Priority (low to high) per Property and you may short circuit processing (immediate stop) by calling f,.ex context.AddSuccessResult(true).  Look at the Net\cs\BusinessRuleDemo in the samples download on how to use this for conditional validation.

Copyright (c) Marimer LLC