CSLA .NET

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

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

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

Top 25 Contributor
385 Posts
Jav posted on Fri, May 25 2012 3:01 PM

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

Answered (Verified) Verified Answer

Top 10 Contributor
1,772 Posts
Verified by Jav

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.

 

Jonny Bekkum, Norway CslaContrib Coordinator

All Replies

Top 10 Contributor
1,772 Posts
Verified by Jav

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.

 

Jonny Bekkum, Norway CslaContrib Coordinator

Top 25 Contributor
385 Posts
Jav replied on Fri, May 25 2012 7:10 PM

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

 

Not Ranked
1 Posts

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

oil painting reproductions

Top 10 Contributor
1,772 Posts

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.

Jonny Bekkum, Norway CslaContrib Coordinator

Page 1 of 1 (5 items) | RSS

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