문제

About Domain Driven Design, Order and OrderLines are always seen as an aggregate, where Order is the root. Normally, once an order is created, one cannot change it. In my case however, that is possible. Instead each order has a state determining whether the order can be changed or not.

In this case, are both Order and OrderLines their own “aggregate root”? I need to be able to update order lines, so I figure that they should have their own repository. But I do not want to retrieve order lines, and persist them without the order. So this indicates that there’s still an aggregate where Order is the root with a factory method to create order lines (Order.CreateOrderLine(quantity, text, …).

Another approach could be to update the Order when the order lines collection has been modified, and then call UpdateOrder(Order). I would need some way of detecting that only the collection should be updated, and no the Order itself (using Entity Framework). What do you think?

도움이 되었습니까?

해결책

Order lines shouldn't be an aggregate of it's own, and doesn't need it's own repository. Your aggregate should be setup something like this...

public class Order
{
    private List<OrderLine> _orderLines;
    private OrderState _orderState;

    public IEnumerable<OrderLine> OrderLines 
    {
        get { return _orderLines.AsReadOnly();}
    }

    public OrderState Status
    {
        get { return _orderState; }
    }

    public void DeleteOrderLine(Guid orderLineID)
    {
        if (Status.IsProcessed)
            throw new InvalidOperationException("You cannot delete items from a processed order");

        OrderLine lineToRemove = _orderLines.Find(ol => ol.Id == orderLineID);

        _orderLines.Remove(lineToRemove);
    }

    public void AddOrderLine(Product product, int quantity)
    {
        if (Status.IsProcessed)
            throw new InvalidOperationException("You cannot add items to a processed order");

        OrderLine line = new OrderLine(product.ProductID, (product.Price * quantity), quantity);

        _orderLines.Add(line);
    }
}

Entity framework has some built in features to detect changes to your object. This is explained here (conveniently with an order/order lines example): http://msdn.microsoft.com/en-us/library/dd456854.aspx

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top