Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing entities via aggregate root: simple example?

Can you show simple example of accessing the contents of an entity in an aggregate via ita aggregate root? I am not clear on how you would represent the Aggregate permissions to reflect these concepts. tia.

like image 881
zsharp Avatar asked Apr 03 '09 15:04

zsharp


People also ask

What is an aggregate root entity?

An aggregate is a collection of one or more related entities (and possibly value objects). Each aggregate has a single root entity, referred to as the aggregate root. The aggregate root is responsible for controlling access to all of the members of its aggregate.

What is an aggregate DDD example?

Aggregate is a pattern in Domain-Driven Design. A DDD aggregate is a cluster of domain objects that can be treated as a single unit. An example may be an order and its line-items, these will be separate objects, but it's useful to treat the order (together with its line items) as a single aggregate.

Can aggregate roots share entities?

As you rightly pointed out, Entity instances shouldn't be shared between aggregates, as one aggregate wouldn't be aware of changes to the entity made through another aggregate and couldn't enforce its invariants.


1 Answers

You typically would encapsulate this in commands that the Aggregate exposes on its contract.

For example, with an Order Aggregate, you might add OrderLines using data obtained from your GUI.

// 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);
    }
}
like image 98
Michael Hart Avatar answered Sep 24 '22 06:09

Michael Hart