Skip to content

Constraining Rules

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

Constraint option checks whether the rule should be invoked or not.

Must be set in the Initialize, or the InitializeAsync method.
Example
class QualifiesForFreeShipping : Rule<Order>
{   
    // If the order amount is greater than $50, then the Invoke method is called.
    public override void Initialize() => 
        Configuration.Constraint = order => order.Total > 50m;
    
    public override IRuleResult Invoke() => Model.FreeShipping = true;
}
Model
class Order
{
    public int Id { get; set; }
    public decimal Total { get; set; }
    public bool FreeShipping { get; set; }
}