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.
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.
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.
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With