1

在为订单建模时,在订单行中拥有产品属性是否有帮助,还是只需要产品名称?

命令:

public class Order 
{
    public Guid Id {get; set;}
    public DateTime OrderDate {get; set;}
    public Decimal Total {get;set;}
}

订单线:

public class Orderline
{
    public Guid Id {get; set;}
    public Order Order {get; set;}
    public Product Product {get; set;}
    public int Quantity {get; set;}
    public Decimal Price {get; set;}
}

有了这个,如果我删除一个产品,它要么需要级联删除订单行和订单(这真的很糟糕),要么我在产品上soft delete设置一个IsDeleted标志。

当然,在域模型中存在关系可能会有所帮助;因此,我可以从订单中访问它的 Orderlines,然后进入每行的产品并访问产品的所有属性。但是由于 Orderline 具有价格属性,因此这种对产品的依赖似乎弊大于利。如果我更改 Orderline 使其只有产品的名称(在下面的类中,我已将类型从更改ProductString,那么我可以删除产品而不影响它的任何记录:

public class Orderline
{
    public Guid Id {get; set;}
    public Order Order {get; set;}
    public string Product {get; set;}
    public int Quantity {get; set;}
    public Decimal Price {get; set;}
}
4

3 回答 3

2

我建议您只需将产品信息复制到订单行。产品有随时间变化的趋势,您真正想要的是客户在特定时间购买的产品的快照。

于 2013-03-29T19:20:35.690 回答
0

这真的取决于你的情况。如果订单被删除,则需要删除订单行,那么通过引用是正确的。我会通过参考来做。它简单快捷,而不是通过额外的步骤通过 ordername 获取订单。我什至不知道 orderline 与 order 有何不同。我认为应该只有一个命令,浓缩。如果东西保存在sql中,做一个外键标识,如果一切都在c#中完成,引用就可以了。

于 2013-03-29T19:15:15.103 回答
0

If memory serves, well-established sales systems like Dynamics GP do both. You need to know what the state of the product was at the time of order; but it's also useful to be able to link a line item to the originating product. For instance, with any given product you might at some time need to compile a list of customers who purchased the product. Or, if the name of a product changes, you may need to be able to determine, for purposes of fulfillment, what the product "is."

That said, what the product "is" shouldn't change. And products which have been ordered should generally never be deleted. But, you ideally also know the exact phrasing, description, pricing, etc. that were on a customer's invoice.

In code, it's a matter of preference, as long as you fulfill the best practices under the hood. I'd suggest having two full Product objects on each OrderLine:

public class Orderline
{
    public Guid Id {get; set;}
    public Order Order {get; set;}
    public Product Product {get; set;}
    public Product OriginalProduct {get; set;}  // or "OriginatingProduct" or whatever
    public int Quantity {get; set;}
    public Decimal Price {get; set;}
}
于 2013-03-29T19:32:04.777 回答