BusinessRule supports both evaluation and validation. It consins of two lambdas:
  • formula - a recipe how to caluclate a value
  • target - target property where to place the result or with what compare the value.

[Test]
public void BusinessRuleEvaluate()
{
    var e = new Entity();
    e.Extensions.CreateBusinessRule(en => en.Number2 + 5, en => en.Number)
        .Start();

    e.Number2 = 5;

    e.Number.ShouldEqual(10);
}

The code above creates a rule which will set Number to Number2+5 everytime the Number2 changes.

[Test]
public void BusinessRuleValidation()
{
    var e = new Entity{Number = 10};
    e.Extensions.CreateBusinessRule(en => en.Number2 + 5, en => en.Number)
        .WithValidation()
        .Start();

    e.Extensions.BrokenRules.Count.ShouldEqual(1);
    e.Number2 = 5;

    e.Extensions.BrokenRules.Count.ShouldEqual(0);
}
Here the rule checks if the Number is equal to Number2 + 5 and creates a BrokenRule if needed.

Last edited Oct 14, 2010 at 8:33 PM by michalmac, version 3

Comments

No comments yet.