Chained Rules & RuleContextModes

Chained Rules & RuleContextModes

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


Peran posted on Thursday, December 22, 2011

Hi Jonny,

I have been playing with your recent business rule changes (CanRunAsAffectedProperty etc) and noticed different behavior if a rule is run as an inner rule.

The standard way of running inner rules, within a gateway rule, bypasses any checks on the RuleContext.ExecuteContext

 

    if (MyCondition)

    {
        var chainedContext = context.GetChainedContext(thisMyInnerRule);
        this.MyInnerRule.Execute(chainedContext);
    }

 

If my inner rule has CanRunAsAffectedProperty = false, I would expect it *not* to run if it is an affected property regardless of it being part of a chain or not.  Currently the rule runs if it is an inner rule.

I propose this addition to RuleContext:

 

        public void ExecuteChainedRule(Csla.Rules.IBusinessRule chainedRule)

        {
            var chainedContext = GetChainedContext(chainedRule);
            if (Csla.Rules.BusinessRules.CanRunRule(chainedRule, chainedContext.ExecuteContext))
                chainedRule.Execute(chainedContext);
        }

Which would also require Csla.Rules.BusinessRules.CanRunRule to be changed from private to internal.

And my gateway rule could be written as:

 

    if (MyCondition)

    {
        context.ExecuteChainedRule(this.MyInnerRule);
    }

 

I think this also simplifies the syntax for running chained rules.

Let me know what you think.

Cheers

Peran

 

JonnyBee replied on Friday, December 23, 2011

Hi Peran,

Great idea and added to Bugtracker: http://www.lhotka.net/cslabugs/edit_bug.aspx?id=1003

I wonder if we should just call the method RuleContext.ExecuteRule

The Using CSLA 4 ebook also call these outer and inner rules but to a programmer it would simply be on or more (inner) rules to be executed from the this  rule's context.

What do you think?

Thanks,

Peran replied on Thursday, December 29, 2011

'ExecuteRule' method name works for me.  I added the word 'Chained' as it was replacing the call to 'Get*Chained*Context'.

 

Regards

 

Peran

 

I renamed the parameter innerRule to go with the documentation.

JonnyBee replied on Thursday, December 29, 2011

Great!  already checked in to trunk.

Copyright (c) Marimer LLC