Part 8 · 1 chapters · ~10 min

Domain-Driven Design

Ubiquitous language, bounded contexts and context maps, aggregates as consistency boundaries, entities, value objects and domain events, and strategic design that finds the core domain first, with a wallet aggregate enforcing its invariant.

10

Domain-Driven Design

Evans' book is long and the strategic half matters most. Read the parts on bounded contexts, context maps and the core domain first, then the tactical patterns. Vernon's Implementing Domain-Driven Design and the shorter Domain-Driven Design Distilled are more practical entry points.

code
// an aggregate: the wallet enforces its own invariant; nothing else edits balances directly
class Wallet {                                             // aggregate root
  private constructor(readonly id: WalletId, private available: Money, private holds: Map<HoldId, Money>) {}

  placeHold(id: HoldId, amount: Money): DomainEvent[] {
    if (amount.greaterThan(this.available)) throw new InsufficientFunds(this.id, amount);   // invariant
    this.available = this.available.minus(amount);
    this.holds.set(id, amount);
    return [{ type: 'HoldPlaced', walletId: this.id, holdId: id, amount }];
  }
  captureHold(id: HoldId): DomainEvent[] { /* … */ }
  releaseHold(id: HoldId): DomainEvent[] { /* … */ }
}
// Money is a value object: integer minor units + currency, immutable, compared by value
// another aggregate (Transfer) references the wallet by WalletId only and reacts to its events
where it has aged
The 2003 examples assume object-relational mapping and a single large application. The strategic ideas map well onto microservices (a bounded context per service is a common heuristic) and onto frontend modules (the feature-sliced structure of the Architecture course). The tactical patterns are often over-applied. Use them where the invariants are rich, such as wallets, ledgers and limits.
DOMAIN-DRIVEN DESIGN
Eric Evans, 2003; Implementing DDD, Vaughn Vernon, 2013: model the domain, draw the boundaries
swipe the figure sideways, or tap expand for full screen
1/6
Ubiquitous language
Ubiquitous language: the team agrees precise terms with domain experts and uses them everywhere: in conversation, documents and code. Ambiguity in the language becomes bugs in the software; renaming code to match the language is a legitimate refactor.