Aggregate Root

The single entry object for a group of related objects, guarding their rules.

What is Aggregate Root?

An aggregate root is the one object in a group of related objects that outside code must go through. To add a line to an order, you call order.AddLine(...), never edit the lines directly. That keeps the group's rules intact.

public class Order
{
    private readonly List<OrderLine> lines = new();

    public void AddLine(string product, int quantity)
    {
        if (lines.Count >= 50) throw new InvalidOperationException("Order is full");
        lines.Add(new OrderLine(product, quantity));
    }
}

Level: Master · More OOP terms at this level

Updated