Skip to content

Proactive Rules

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

A Proactive rule can proact to another rule. Set the IsProactive flag to true, specify a rule to proact to using ObserveRule property.

  • A Constraint can be defined in the proactive 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 Proactive Rule. It only gets invoked before the UpdateShipping rule.

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 proactive
        IsProactive = true;

        // Specify rule to proact 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()