Skip to content

DDD — Domain Model

This guide is split into three parts, and you are on Short info. Continue with the tutorial or browse the samples.

DDD answers: what business concepts and rules must remain consistent?

Use it when the system has rules such as “a booking cannot exceed capacity” or “a shipped order cannot be cancelled.” Start with one aggregate, its vocabulary, and one invariant. Keep persistence and screen layout in their own models. A small CRUD feature may need only a simple data model until its business rules become more demanding.

  • Choosing an aggregate solely because several tables have foreign keys.
  • Giving Booking and Inventory competing authority over the remaining-seat count.
  • Treating a generated scaffold as evidence that a concurrent reservation is safe.

Domain-Driven Design was popularized by Eric Evans’s book and treats collaboration with domain experts as part of software design. Its vocabulary includes bounded contexts, entities, value objects, aggregates and a ubiquitous language. The ContextBrew DDD format concentrates on explicit domain structure, invariants and events; it is a representation of part of DDD, rather than the entire practice. See Eric Evans’s DDD resources for the underlying method.

An aggregate groups state that must obey a consistency boundary. Choose that boundary from the business transaction, then describe valid changes. A database foreign key or a box around related nouns is not enough to justify an aggregate. Review invariants with an example that should succeed and another that should be rejected. Generating a test scaffold records an obligation; a real implementation and test run supply evidence that it is met.

Use an ER diagram or DSM for storage structure, a transaction script for simple operations, or ESDM when event history and projections are central. A Context Map complements tactical aggregates with relationships between bounded contexts.

The DDD model contains four core element types:

  • Aggregates – the primary entities in your domain (e.g., Order, Customer, Booking)
  • Properties – typed fields on aggregates (e.g., status:OrderStatus, total:Decimal)
  • Invariants – business rules that must always hold (e.g., “status cannot transition from COMPLETED to CANCELLED”)
  • Domain Events – things that happen in the domain (e.g., OrderPlaced, PaymentReceived)

Aggregates can also have associations (relationships to other aggregates) and value objects (immutable composite types like Money or Address).

  • USM – Each aggregate links to the user stories that involve it
  • CBRL – Invariants are referenced by formal requirements via INVOLVES_AGGREGATE
  • SCM – Screens are linked to the aggregate they manage
  • DSM – Tables are auto-proposed from aggregates (invariants become CHECK constraints, properties become columns, associations become foreign keys)
  • USM Model to define user stories that connect to your aggregates
  • CBRL Model to write formal requirements referencing your aggregates
  • DSM Model to see how aggregates map to database schemas

The serialized DDD model (models/ddd.json inside a .ctxbrew archive) is validated by a normative JSON Schema generated from ContextBrew’s source code. Every field, its type, and the canonical schema URL are documented on the DDD file schema page — or print the schema anywhere with ctxbrew schema ddd.