Skip to content

Reactive Rules

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

A Reactive rule can react to another rule. Set the IsReactive flag to true, specify a rule to react to using ObserveRule property.

  • A Constraint can be defined in the reactive rule to add a condition whether the rule should be invoked or not.*
Must be set in the Initialize method.
Example

In the following example UpdatePromoCode is a Reactive Rule. It gets invoked only after the UpdateShipping rule gets invoked.

class UpdateShipping : Rule<Order>
{
    public override IRuleResult Invoke()
    {
        Model.FreeShipping = true;
        return null;
    }
}
class UpdatePromoCode : Rule<Order>
{
    public override void Initialize()
    {
        // Flag rule to be reactive
        IsReactive = true;

        // Specify rule to react to
        ObserveRule<UpdateShipping>();
    }

    public override IRuleResult Invoke()
    {
        Model.PromoCode = "156485";
        return null;
    }
}

Invoke Rule(s)

var ruleResults = await RuleEngine<Order>.GetInstance(order)
    .ApplyRules(new UpdateShipping(), new UpdatePromoCode())
    .Execute()