Skip to content

Parallel Rules

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

Parallel rules derive from async rules, except they run in parallel. The order of execution is not guaranteed.

Parallel rules ignore the ExecutionOrder flag.
Must be set in the Initialize method.
Example
class QualifiesForFreeShipping: RuleAsync<Order>
{ 
    public override Task InitializeAsync()
    {
         IsParallel = true;
         return Task.FromResult<object>(null);
    }

    public async override Task<IRuleResult> InvokeAsync()
    {
        // Order instance accessible through the Model property.
        if (Model.Amount > 50.0m) Model.FreeShipping = true;
        
        return await Task.CompletedTask;
    }
}

Invoke Asynchronous/Parallel Rule(s)

var ruleResults = await RuleEngine<Order>.GetInstance(order)
    .ApplyRules(new QualifiesForFreeShipping())
    .ExecuteAsync()

Returning result from a rule is optional. Use it if additional output (e.g. error message, code, etc.) needed. In most cases the Model should be the input and the output of the rule engine.