【发布时间】:2010-08-02 09:59:21
【问题描述】:
我有几个业务类,如地址、产品、订单、订单行。在这种情况下,我有一个关于我的 Order 类以及如何在存储库中更新它的问题。
Order 类可以有多个 OrderLine。该类如下所示:
class Order {
private List<OrderLine> orderLines;
public Address CustomerAddress { get; set; }
public Address DeliveryAddress { get; set; }
public void Add(OrderLine line) {
// stuff to add order line.
}
public void Remove(int index) {
// stuff to remove order line by index.
}
}
当我需要获取订单时,我可以执行以下操作:
Order myOrder = Orders.Get(5); // Gets the order with ID 5 from repository.
现在,我可以根据需要添加或删除订单行并修改 CustomerAddress 或 DeliveryAddress。
当我想更新修改后的订单时,我可以这样做:
bool updated = order.Update();
在内部调用 Orders 存储库的 Update 方法,例如:
public bool Update() {
return Orders.Update(this); // Let repository handle updating of this order.
}
现在假设我之前使用 Orders.Get(5) 获得的订单有 10 个订单行,我修改了 1 个订单行,添加了 1 个订单行并删除了 1 个订单行。
假设我分配了不同的收货地址:
myOrder.DeliveryAddress = Addresses.Get(64); // Get address with ID 64 from repository and assign it to order.
Order Repository 的 Update 方法应该发生什么?
是否应该更新整个订单,包括所有订单行,删除已删除的订单行,保存添加的订单行并更新已更改的订单行?
我有点不知所措,因为当所有这些内部项目(地址和订单行)都必须更新、插入或删除时,它变得非常复杂。
有人有建议吗?
提前非常感谢你:)
【问题讨论】:
标签: c# object repository