Skip to content

DDD — Tutorial

This is the tutorial part of the DDD — Domain Model guide. See also Short info and samples.

The last seat · 55 seconds · English narration and captions · Sample created 26 October 2026.

Follow one seat-allocation decision: choose an aggregate, state its invariant, accept a request that fits, and refuse one that exceeds capacity.

Captions are included in the picture. You can also read the descriptive transcript, download the MP4, or download captions. The film illustrates the model; its final question introduces the implementation work needed to handle concurrent requests safely.

Choose one business operation and name a person who can explain its rules. You can follow the example without knowing the DSL.

Imagine a traveler asking for the last seat on a flight. The interesting question is whether the reservation may be confirmed. Naming a Booking object is only a beginning: the model must explain the information needed to make that decision and the condition that must remain true afterwards. Ask the domain expert to describe both the normal operation and a case they would refuse.

An entity has an identity that survives changes to its properties. A value object represents a value, such as a monetary amount and currency, and is compared by that value. An aggregate identifies state whose rules must be maintained together through its root. These concepts help express behavior; they are not instructions to put every related object into one large structure.

Keep a rule beside the state that can enforce it. For seat allocation, the Flight or a dedicated seat-inventory aggregate may own remaining capacity. A Booking can record the requested reservation without independently claiming authority over every seat. If two aggregates must cooperate, name the coordination problem explicitly. A diagram containing both objects does not solve a concurrent reservation race.

Model an event only when the business recognizes a fact worth naming, such as SeatReserved. Decide what that fact means before choosing a message broker. Then connect the model to a requirement describing the externally visible promise and to storage choices that support it. The resulting chain gives a reviewer a route from business meaning to implementation evidence.

The last seat: where a business rule lives. A flight starts with two seats and one confirmed reservation. A request for one more seat fits. Reset to the original state: a request for two more seats is refused, and the confirmed count stays at one.

Pause and predict: If two travelers ask for the final seat at once, which boundary must decide?

Watch the short lesson above, or use its descriptive transcript. The inventory aggregate names the consistency boundary; the real implementation still needs atomic enforcement and tests.

There is no model command — aggregates are authored as plain text .domain files under .contextbrew/ddd/ (or on the Studio canvas). Create .contextbrew/ddd/order.domain:

AGGREGATE Order {
PROPERTY id: UUID [pk]
PROPERTY customerId: UUID [required]
PROPERTY status: string [default="pending"]
PROPERTY total: number [default="0"]
PROPERTY items: string
INVARIANT "total-matches-items"
EXPRESSION "total == sum(items.price)"
INVARIANT "no-post-completion-changes"
EXPRESSION "status != 'completed' WHEN items IS CHANGED"
}

Then validate it — exit 1 on any parse or validation error:

Terminal window
ctxbrew ddd validate .contextbrew/ddd/order.domain

Open the DDD canvas in Context Studio (the web UI). The canvas uses Svelte Flow for a visual node-graph editor:

  1. Drag an Aggregate node onto the canvas
  2. Click the node to add properties, invariants, and events
  3. Draw Association edges between related aggregates
  4. The model is saved automatically

Invariants are the most important part of the DDD model. They express business rules that ContextBrew turns into test cases:

AGGREGATE Order {
INVARIANT "no-cancel-after-shipment"
EXPRESSION "status != 'cancelled' WHEN status IN ('shipped')"
}

Add them to the aggregate block in the .domain file and re-validate. ContextBrew parses each invariant and generates:

  • Unit tests that verify the invariant holds under various conditions
  • Negative tests that verify the invariant is enforced (the operation fails)
  • Gherkin scenarios describing the invariant in Given/When/Then format

Events capture state transitions in the domain:

AGGREGATE Order {
DOMAIN_EVENT OrderPlaced {
PROPERTY orderId: UUID
PROPERTY placedAt: Date
PROPERTY total: number
}
}

Events are used to generate event handler stubs and integration test scaffolding.

Terminal window
ctxbrew ddd validate <file> # Parse the file; exit 1 on any error
ctxbrew ddd stats <file> # Collection sizes (aggregates, events…)
ctxbrew ddd export <file> # Canonical DSL (or --format json|yaml)
ctxbrew ddd import <file> # Canonicalization pass
ctxbrew ddd diff <before> <after> # Semantic diff (report only)
ctxbrew schema ddd # Print the normative JSON Schema

DDD aggregates can come from existing code, not just hand-authoring: ctxbrew init --from-code . reverse-engineers a hypothesis DDD spec from a TypeScript codebase (promote it with ctxbrew init --promote), and TypeORM/NestJS or Spring/JPA entities import as aggregate proposals — marked and reviewable, never merged silently. On the way out, DDD exports Gherkin, an architecture doc, and a Mermaid class diagram, alongside the canonical DSL/JSON/YAML. Details: Import & Export.

Beyond the DSL: the Domain Design workspace

Section titled “Beyond the DSL: the Domain Design workspace”

The web app’s Domain Design workspace covers the full DDD loop — discovery (Event Storming, story maps), strategic design (subdomains, bounded contexts, context map, ubiquitous dictionary) and tactical design (aggregates, behavior, services). Unassigned aggregates live in the default TBD · To be defined context until you classify them. See docs/design/SPEC-DOMAIN-WORKSPACE-V2-2026-09-01.md.