ItzikSaban
ItzikSaban

Reputation: 296

Aggregate Roots

Consider the following structure: Customer->Orders->OrderLines->Quantity and Customer is the aggregate root.

Suppose we want to change the Quantity of one OrderLine, how would we do that? Will Customer have a method like this:

public ChangeQuantity(Order order, OrderLine orderLine, int quantity)
{
    order.OrderLines.First(...).Quantity = quantity;
}

or will the implementation be:

public ChangeQuantity(Order order, OrderLine orderLine, int quantity)
{
    order.ChangeQuantity(orderLine, quantity);
}

Upvotes: 1

Views: 246

Answers (2)

BrokenGlass
BrokenGlass

Reputation: 160992

Most definitely the latter. If you think about it the first approach violates the Law of Demeter - and that is really a core property of DDD.

But if you pass in the order and the orderline already, why doesn't the caller perform the method call?

Upvotes: 7

Domenic
Domenic

Reputation: 112917

You don't need all access to the non-root objects to go through the root object.

You just need the root object to be a consistency and persistence boundary.

So there's no reason to have either method, since your user can just go to the order line object directly:

OrderLine.Quantity = 5;

The fact that customer is an aggregate root simply means that there is no way to, for example, commit this change to the database without committing the entire customer to the database.

Upvotes: 7

Related Questions