NRules SimpleRule的代碼定義下列規則:如何做到最佳寫入規則定義NRules
public class PreferredCustomerDiscountRule : Rule
{
public override void Define()
{
Customer customer = null;
IEnumerable<Order> orders = null;
When()
.Match<Customer>(() => customer, c => c.IsPreferred)
.Collect<Order>(() => orders,
o => o.Customer == customer,
o => o.IsOpen,
o => !o.IsDiscounted);
Then()
.Do(ctx => ApplyDiscount(orders, 10.0))
.Do(ctx => LogOrders(orders))
.Do(ctx => orders.ToList().ForEach(ctx.Update));
}
...
}
我很奇怪,爲什麼條件是隻使用& &運營商即代替單獨pareameters將以下具有相同的效果?
public class PreferredCustomerDiscountRule : Rule
{
public override void Define()
{
Customer customer = null;
IEnumerable<Order> orders = null;
When()
.Match<Customer>(() => customer, c => c.IsPreferred)
.Collect<Order>(() => orders,
o => o.Customer == customer && o.IsOpen && !o.IsDiscounted);
Then()
.Do(ctx => ApplyDiscount(orders, 10.0))
.Do(ctx => LogOrders(orders))
.Do(ctx => orders.ToList().ForEach(ctx.Update));
}
...
}