Operations on entities within a aggregate root
Asked Answered
B

1

4

If i have designed an AR like the below, how do you think i should go about say updating a property in one of the order line objects ?

For Example how can i change the title for one of my order lines (example question)

This is the Order Aggregate Root

public class Order
{
    private readonly int id;
    private readonly Customer customer; // Customer is another Aggregate
    private readonly IList<OrderLine> orderLines;
    private readonly IOrderLineFactory orderLineFactory;

    public Order(int id, Customer customer, IOrderLineFactory orderLineFactory)
    {
        this.id = id;
        this.customer = customer;
        this.orderLines = new List<OrderLine>();
        this.orderLineFactory = orderLineFactory;
    }

    public void AddOrderLine(Item item, int quantity)
    {
        OrderLine orderLine = orderLineFactory.Create(this, item, quantity);
        orderLines.Add(orderLine);
    }
}
Bakke answered 30/7, 2012 at 17:39 Comment(0)
I
1
Order order = orderRepository.find(orderId);
order.changeTitle(orderLineId, "New title");

Where 'orderLineId' can be a line number or an index or something else as long as it is aggregate-root-specific (not a global id). Please see this answer to a similar question.

Indecision answered 30/7, 2012 at 19:18 Comment(3)
Would'nt this approach cause a bloat in the interface exposed by the AR? It Would need to expose the behavior of all its entities via its own interface. Also assume that orderline had a Set Of Tags which needed to be updated. Would we go along with something like order.changeTagTitle(orderLineId,tagId, "New title"); ?Bakke
@Sudarshan: yes, it will bloat the interface. But most of the time the aggregate is relatively small and the number of unrelated methods on your aggregate won't be very big. It is one of the downsides of DDD.Prolix
How does one design elegant Aggregate Root interfaces?, because if a AR even has just 2 entities with it and each of them need to expose 2 behaviors each the AR will have 4 behaviors to expose other than its ownBakke

© 2022 - 2024 — McMap. All rights reserved.