Business Rules Chaining

Business Rules Chaining

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


Turntwo posted on Thursday, August 26, 2010

I'm trying to chain business rules under the new 4.0 model, and I've run into a problem - I can workaround it, but I want some feedback on the "correct" method to use. 

In the example in the blog entry on Rule Chaining we have the following;

if (obj != null && obj.IsNew)
      _innerRule.Execute(context.GetChainedContext(required));

However, when I attempt to chain a rule this way, the Execute method is not available on _innerRule, because it is protected (I inherited from BusinessRule, and overrode the protected method). 

For my own Rules I can change the protected to public using new instead of override, but what if I want to chain Csla CommonRules?

JonnyBee replied on Thursday, August 26, 2010

Hi,

See my sample code here - your inner rule should be declared as IBusinessRule and that will allow you to call the execute method.

  public class NewGateRule : BusinessRule
  {
    private IBusinessRule _innerRule;

    public NewGateRule(Csla.Core.IPropertyInfo primaryProperty)
      : base(primaryProperty)
    {
            // create instance of the chained rule 
      _innerRule = (IBusinessRule)new Csla.Rules.CommonRules.Required(PrimaryProperty);

      // must add input properties from the inner rule  
      InputProperties = new List<Csla.Core.IPropertyInfo>();
      InputProperties.AddRange(_innerRule.InputProperties);
    }


    protected override void Execute(RuleContext context)
    {
      var obj = context.Target as Csla.Core.ITrackStatus;
      if (obj != null && obj.IsNew)
        _innerRule.Execute(context.GetChainedContext(_innerRule));
    }
  }

Turntwo replied on Thursday, August 26, 2010

Thanks, I missed that _innerRule was IBusinessRule - which allows calling the Execute.  I knew I was doing something wrong. 

Copyright (c) Marimer LLC