Skip to content

Asynchronous Rules

Arif Yayalar (@ayayalar) edited this page Aug 24, 2018 · 2 revisions

Asynchronous rules executed serially in that same order provided to the rule engine unless the ExecutionOrder property is specified. It should be used with async programming model.

Example
class QualifiesForFreeShippingAsync: RuleAsync<Order>
{   
    public async override Task<IRuleResult> InvokeAsync()
    {
        // Order instance accessible through the Model property.
        if (Model.Amount > 50.0m) Model.FreeShipping = true;
        
        return await RuleResult.Nil();
    }
}

Invoke Asynchronous/Parallel Rule(s)

var ruleResults = await RuleEngine<Order>.GetInstance(order)
    .ApplyRules(new QualifiesForFreeShippingAsync())
    .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.