Skip to content

Synchronous Rules

Arif Yayalar (@ayayalar) edited this page Aug 24, 2018 · 1 revision

Synchronous rules executed serially in that same order provided to the rule engine unless the ExecutionOrder property is specified.

Example
class QualifiesForFreeShipping: Rule<Order>
{   
    public override IRuleResult Invoke()
    {
        // Order instance accessible through the Model property.
        if (Model.Amount > 50.0m) Model.FreeShipping = true;            
        
        return null;
    }
}
Order order = new Order { Id = 1, Total = 79.99 };

// Pass order instance to RuleEngine.
var ruleResults = RuleEngine<Order>.GetInstance(order)
    .ApplyRules(new QualifiesForFreeShipping())
    .Execute()

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.