Skip to content

Nested Execution Order

Arif Yayalar (@ayayalar) edited this page Dec 31, 2019 · 3 revisions

Nested Rules can specify ExecutionOrder, which is applied at the nested level only.

Must be set in the Initialize, or in the InitializeAsync method.
Parallel rules cannot have both IsParallel and ExecutionOrder specified. Otherwise, the parallel option is ignored.

In the proceeding example, there are four rules (two of them are nested).

Example
class Rule1 : Rule<Order>
{
    public override void Initialize()
    {
        Configuration.ExecutionOrder = 2;
        AddRules(new Rule1_Nested(), new Rule2_Nested());
    }

    public override IRuleResult Invoke() => null;
}
class Rule1_Nested : Rule<Order>
{
    public override void Initialize() => Configuration.ExecutionOrder = 1;
    public override IRuleResult Invoke() => null;
}
class Rule2 : Rule<Order>
{
    public override void Initialize() => Configuration.ExecutionOrder = 1;
    public override IRuleResult Invoke() => null;
}
class Rule2_Nested : Rule<Order>
{
    public override void Initialize() => Configuration.ExecutionOrder = 2;
    public override IRuleResult Invoke() => null;
}
// Execute the rules
var re = RuleEngine<Order>.GetInstance(new Order())
    .ApplyRules(new Rule1(), new Rule2())
    .Execute();

The trace from the example above. Following the ExecutionOrder defined in the rules.

Information: Executing Rule2 - BeforeInvoke
Information: Executing Rule2 - Invoke
Information: Executing Rule2 - AfterInvoke

Information: Executing Rule1_Nested - BeforeInvoke
Information: Executing Rule1_Nested - Invoke
Information: Executing Rule1_Nested - AfterInvoke

Information: Executing Rule2_Nested - BeforeInvoke
Information: Executing Rule2_Nested - Invoke
Information: Executing Rule2_Nested - AfterInvoke

Information: Executing Rule1 - BeforeInvoke
Information: Executing Rule1 - Invoke
Information: Executing Rule1 - AfterInvoke