Skip to Content
Mengty Learns
Architecture

DDD Tactical Patterns: Entities, Value Objects, Aggregates

Model rich domain rules using Entities, Value Objects, Aggregates, Repositories, and Domain Events on the shared Order service.

explanationintermediateTested with typescript 7.0.2Updated Nov 27, 2026

TL;DR

Tactical DDD puts business invariants inside the domain model instead of anemic service scripts: Value Objects (Money) are immutable, Entities (Order) have identity, Aggregates guard consistency boundaries, Repositories persist whole aggregates, and Domain Events (OrderPlaced) announce state changes.

Intent

TL;DR

Enforce business invariants in one place inside an Aggregate Root (like Order) so invalid state—such as an empty order or a negative total—cannot be constructed anywhere in the codebase.

Problem

In an "anemic domain model", Order is just a plain data bag (interface Order { status: string; items: any[] }) and validation rules scatter across controllers, cron jobs, and services. One endpoint forgets to check items.length > 0 or recalculates totalCents differently, corrupting data in production.

External callers only talk to the Order Aggregate Root; OrderLines and Money live inside its boundary.

Solution

Tactical DDD gives you five building blocks that fit naturally inside Hexagonal or Clean Architecture:

  1. Value Object (Money): immutable, validated on creation (amountCents >= 0), compared by value.
  2. Entity (Order, OrderLine): has a unique id and mutable lifecycle (draft → placed → cancelled).
  3. Aggregate Root (Order): owns OrderLine[] and exposes intent-revealing methods (order.place()). No outside code mutates order.lines directly.
  4. Repository (OrderRepository): saves and loads the entire Order aggregate as one atomic unit.
  5. Domain Event (OrderPlaced): immutable record emitted by the aggregate when a meaningful business fact occurs.

Real-world analogy

▶ Flow: watch it work

Command → Aggregate Invariant → Domain Event

Overview of Command → Aggregate Invariant → Domain Event. 6 steps.
Overview6 steps in this flow

Press Start or tap any step dot to trace the path.

▸All steps (6)

Code

The shared Order example modeled as a Value Object (Money), Aggregate Root (Order), and Domain Event (OrderPlaced):

// TypeScript 7.0.2
export class Money {
  private constructor(
    public readonly cents: number,
    public readonly currency: 'USD' | 'EUR',
  ) {}

  static of(cents: number, currency: 'USD' | 'EUR' = 'USD'): Money {
    if (!Number.isInteger(cents) || cents < 0) throw new Error('Invalid Money');
    return new Money(cents, currency);
  }

  add(other: Money): Money {
    if (other.currency !== this.currency) throw new Error('Currency mismatch');
    return Money.of(this.cents + other.cents, this.currency);
  }
}

export interface OrderPlaced {
  readonly type: 'OrderPlaced';
  readonly orderId: string;
  readonly totalCents: number;
}

export class Order {
  private events: OrderPlaced[] = [];

  private constructor(
    public readonly id: string,
    private readonly items: ReadonlyArray<{ sku: string; price: Money; qty: number }>,
    private status: 'draft' | 'placed',
  ) {}

  static create(id: string, items: Array<{ sku: string; price: Money; qty: number }>): Order {
    if (items.length === 0) throw new Error('Order must have at least one item');
    return new Order(id, items, 'draft');
  }

  place(): OrderPlaced[] {
    if (this.status !== 'draft') throw new Error('Order already placed');
    this.status = 'placed';
    const total = this.items.reduce(
      (sum, item) => sum.add(Money.of(item.price.cents * item.qty, item.price.currency)),
      Money.of(0),
    );
    this.events.push({ type: 'OrderPlaced', orderId: this.id, totalCents: total.cents });
    return [...this.events];
  }
}

When to use / when to avoid

When to use
  • Your domain has real state-transition rules and invariants (orders, payments, inventory reservations, bookings).
  • You already use Hexagonal or Clean Architecture and want a rich domain core instead of anemic DTOs.
When to avoid
  • Simple CRUD forms or admin tables with no invariants—tactical DDD adds ceremony for zero payoff.
  • Reporting/analytics queries—bypass Aggregates and read directly with SQL.

How to implement

Extract Value Objects first

Replace raw number or string primitives (cents, email, currency) with self-validating Value Objects.

Keep Aggregates small

Include only the child entities needed to enforce an immediate invariant; reference other Aggregates by ID only.

Emit Domain Events on state transitions

Record events inside the Aggregate Root and publish them after the repository commits.

Pros / Cons

Pros
  • Invariants are impossible to bypass because constructors and methods guard every state transition.
  • Pure TypeScript domain classes test in microseconds with zero mocks.
Cons
  • Requires mapping between rich Aggregates and relational/SQL tables in the Repository.
  • Oversized Aggregates cause lock contention when multiple users edit unrelated child entities.

Relations

Check yourself

Check Yourself

1. Why should an Order Aggregate hold customerId: string instead of a full Customer object reference?

2. Which DDD building block is immutable, has no ID, and is compared by its property values?

Sources