Domain Modeling

Entities, value objects, aggregates and services — plus the honest question of whether a rich domain model earns its cost over a transaction script for the system in front of you.

Domain Modeling
▶ lab

Getting the nouns and verbs the business actually uses into the code, so a requirement in their sentence maps to a change in one of yours.

Q · A requirement arrives in the business's words. How do I make the code contain those words, so translating it is not the expensive part?
Ubiquitous Language

One word for one concept, in conversation and in code — where the concept is real. The failure is three names for one thing across three modules, all of them defensible.

Q · When is it worth forcing the code to use the business's word, and when is a separate technical name the honest one?
Entities

Some things are the same thing after every one of their fields has changed. Order #123 is still order #123 — identity, not equality, is what defines them.

Q · Which of my types are the same thing over time even when their contents change, and what does that force me to design?
Value Objects

Things defined entirely by their value — Money, EmailAddress, Coordinates. The highest-value, lowest-cost idea in this module, and the one worth adopting even if you take nothing else.

Q · Which of my types have no identity at all, and what do I get by giving them a name instead of passing a number around?
Aggregates

A consistency boundary drawn around state that must change together. Powerful and easy to over-apply — most objects are not aggregates and should not be treated as one.

Q · Which pieces of state must be consistent with each other at every instant, and which are merely related?
The Aggregate Root

One door into the boundary. External changes go through the root so the invariant has exactly one place it can be checked — and exactly one place it can be bypassed.

Q · If a rule spans several objects, how do I make it impossible to change any of them without the rule being checked?
Domain Services

For the operations that genuinely belong to no single entity or value object. A small, useful category — and a dumping ground the moment it stops being small.

Q · This behaviour is domain logic and does not fit on any one object. Where does it go, and how do I stop that place becoming everything's home?
The Anemic Domain Model

Data objects with no behaviour, and all the logic in services. Widely called an anti-pattern, widely defended, and correct more often than either side admits.

Q · My entities are data holders and the rules live in services. Is that actually a problem, or only a problem in some systems?
Transaction Script

One procedure per operation, top to bottom, doing the whole job. Often exactly right — and the criteria for when it stops being right are knowable in advance.

Q · When is a straightforward procedure per use case the correct design, and what specifically tells me it has stopped being?
When Domain-Driven Design Does Not Pay
▶ lab

The machinery costs vocabulary, indirection and mapping on every change. It repays only at real domain complexity, with real access to someone who knows the domain.

Q · What has to be true about my system and my team before this module's heavier ideas are worth their cost?