我正在寻找一种方法来创建一个可扩展的销售订单项目,以便轻松附加新的业务规则。
public class OrderLine
{
public int OrderId { get; set; }
public int Line { get; set; }
public string Product { get; set; }
public string Description { get; set; }
public decimal Quantity { get; set; }
public int LeadTimeDays { get; set; }
public DateTime ShipDate { get; set; }
}
创建业务规则以检查订单行是否有效的最佳实践是什么?而且,有没有一种简单的方法来应用多个规则而不为每个规则添加检查方法?
public static class OrderLineChecks
{
public static void CheckLeadTime(this OrderLine orderLine)
{
if( (orderLine.ShipDate - DateTime.Today).TotalDays < orderLine.LeadTimeDays )
throw new Exception("Order is within lead time.");
}
public static void CheckShipDateError(this OrderLine orderLine)
{
if(orderLine.ShipDate < DateTime.Today)
throw new Exception("Ship date cannot be before today.");
}
public static void ShouldBeOrderedInPairs(this OrderLine orderLine)
{
if(orderLine.Description.Contains("pair") && (orderLine.Quantity % 2 !=0))
throw new Exception("Quantities must be even numbers.");
}
public static NextFutureRuleHere(...)
{
}
}
谢谢你的建议。