Aggregate
ELI5 — The Vibe Check
An aggregate is a cluster of domain objects that are treated as one unit for data changes. The 'Order' aggregate contains OrderLines and the ShippingAddress — you can only modify them through the Order. It's the bodyguard that protects consistency.
Real Talk
A DDD tactical pattern defining a cluster of associated objects treated as a single unit for data changes, with one entity serving as the aggregate root. All modifications go through the root, which enforces invariants and consistency boundaries. Aggregates are the transactional consistency boundary in domain-driven design.
Show Me The Code
class Order { // Aggregate Root
private items: OrderItem[] = [];
addItem(product: Product, qty: number) {
if (this.items.length >= 50) throw new Error('Max items');
this.items.push(new OrderItem(product, qty));
}
get total() {
return this.items.reduce((sum, i) => sum + i.subtotal, 0);
}
}
When You'll Hear This
"Only modify order items through the Order aggregate — it enforces the 50-item limit." / "Keep aggregates small. If your aggregate loads 10 tables, it's probably too big."
Related Terms
Bounded Context
A bounded context is a boundary where a particular model and language applies. 'Customer' means one thing in Sales and something different in Shipping.
Domain-Driven Design (DDD)
DDD says your code should speak the same language as the business.
Entity
An entity is a domain object with a unique identity that persists over time.
Repository Pattern
Repository Pattern puts a layer between your business logic and your database, so your business code never writes SQL directly.