Skip to content

Aggregate

Spicy — senior dev territoryArchitecture

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.