
An Aggregate Is a Consistency Boundary, Not a Big Object Graph
- كاتب موثّق
محتويات المقال
Customer aggregate: profile, addresses and every order the customer ever placed, with their lines. For a loyal customer that's thousands of objects in memory to add one order. Then the second complaint arrives: when support updates a customer's phone number while she's checking out, one of the two saves fails with a version conflict. Nothing about a phone number affects an order, yet both changes go through the same root, so they collide.
The common misconception
customer.Orders reads naturally. Vernon's essay opens with exactly this question, "Is an aggregate just a way to cluster a graph of closely related objects under a common parent?", and its fictional team falls into the trap by putting weight on the words "Products have".
What actually happens
Aggregates exist to protect invariants
Big aggregates fail in two ways
Small, by identity, eventually consistent
The fix: one aggregate per consistency rule
// "A customer has orders", so the aggregate holds all of thempublic sealed class Customer{ public CustomerId Id { get; init; } public string Phone { get; private set; } = ""; public List<Address> Addresses { get; } = []; public List<Order> Orders { get; } = []; // every order, for years public int Version { get; private set; } // checked on every save public void PlaceOrder(Order order) { Orders.Add(order); Version++; } public void ChangePhone(string phone) { Phone = phone; Version++; }}// Checkout loads years of orders to add one, and a phone change// at the same moment makes one of the two saves failnamespace Sales;// Order is its own aggregate and guards its own rulespublic sealed class Order{ private readonly List<OrderLine> _lines; public OrderId Id { get; } = OrderId.New(); public CustomerId CustomerId { get; } // reference by identity public IReadOnlyList<OrderLine> Lines => _lines; public Order(CustomerId customerId, List<OrderLine> lines) { if (lines.Count == 0) throw new ArgumentException("An order needs lines"); CustomerId = customerId; _lines = [.. lines]; }}// Customer keeps only what must stay consistent with itpublic sealed class Customer{ public CustomerId Id { get; init; } public string Phone { get; private set; } = ""; public void ChangePhone(string phone) => Phone = phone;}Order and saves it, without loading the customer at all. A phone change and a checkout touch different aggregates, so they no longer collide. Rules that span both, such as awarding loyalty points, react to an order-placed event in their own transaction. Screens that need a customer with their order history get it from a query, not by loading aggregates, and Vernon suggests CQRS when those queries get expensive.
How to apply it in your project
List the true invariants For your largest aggregate, write down the rules that must hold every time a change commits. Ask domain experts which ones are real and which a developer invented. Draw a boundary around each rule's data Keep together only the data one rule needs to check. Everything else becomes its own aggregate, often a single entity. Replace object references with IDs Store CustomerId, notCustomer. Let the application service load what a command needs, and serve screens from queries instead of aggregate graphs.Move cross-aggregate rules to events When one change must trigger another, raise a domain event and handle it in a separate transaction, and agree with the business how long the delay may be.
Key takeaways
An aggregate is a transactional consistency boundary, not a container for everything related. Only data that must be consistent at commit time belongs inside; many aggregates are a single entity. Big aggregates cause false conflicts between unrelated edits and load far more than each change needs. Reference other aggregates by identity, and keep them in step with domain events and eventual consistency. Ask whose job consistency is: the current user's job means one transaction, anyone else's can wait.
📚 المصادر
DDD_Aggregate — Martin Fowler, bliki (2013) Domain-Driven Design Reference — Eric Evans, Domain Language (2015) Effective Aggregate Design, Part I: Modeling a Single Aggregate — Vaughn Vernon (2011) Effective Aggregate Design, Part II: Making Aggregates Work Together — Vaughn Vernon (2011) Use tactical DDD to design microservices — Azure Architecture Center, Microsoft Learn
التعليقات (0)