DDD Building Blocks
Entidad · Objeto de Valor · Agregado
Escrito por Darjus Kazakevic · revisado el
Put the business rules in the model, not in the controllers.
Domain-Driven Design (DDD) has two halves. The strategic half draws boundaries between models. The tactical half, these building blocks, is what you write inside one boundary: entities, value objects, aggregates, domain events and domain services.
Each block answers one question. Does this thing have an identity? Can it change? What must always be true together? The answers move business rules out of controllers and into the model. The cost is more types and more design work, so use the blocks where the rules are rich.
En esencia — Model the business as objects that hold both data and the rules that protect it, so the code uses the business's words and invalid states are hard to create.
Cuándo recurrir a él
Recurre a él cuando
- The business has real rules and invariants, not only data entry
- A core subdomain that will keep changing for years
- You use Hexagonal or Clean architecture and need to fill the core
Reconsidéralo cuando
- Simple CRUD screens, where a Transaction Script is cheaper
- Reports and lists: query a flat read model instead
- Short-lived code that will not live long enough to repay the effort
The building blocks
- Entity
- An object defined by its identity, not by its fields. It changes over time but stays the same thing. Two customers with the same name are still two customers.
- Value Object
- An object defined only by its fields, with no identity: money, a date range, an address. It is immutable and checks itself when created. Most of a model should be values, not entities.
- Aggregate and its root
- A group of entities and values that change together, inside one transaction. Outside code holds only the root entity and calls its methods. The root checks the rules before it accepts any change.
- Domain Event
- A record that something important happened, named in the past tense. Other aggregates and services react to it later, without a direct call.
- Domain Service
- A business operation that does not belong to one entity, usually because it works on several. It is stateless and named with a business verb.
Rules for aggregate design
- Keep true invariants inside one aggregate
- An invariant is a rule that must be true at every moment. If two pieces of data must always agree, put them in one aggregate. If they may disagree for a few seconds, they can live apart.
- Keep aggregates small
- Aim for one root plus a few values. A large aggregate loads slowly and locks many rows on every write, so two users editing different order lines block each other.
- Refer to other aggregates by id
- Store a CustomerId, not a Customer object. An id keeps each aggregate loadable and saveable on its own, and stops one load from pulling a whole object graph into memory.
- Change one aggregate per transaction
- When an action must update other aggregates, raise a domain event and update them afterwards. Accept that they are eventually consistent: correct after a short delay.
Errores habituales
- An anemic model: classes with only getters and setters, and every rule in a service. You pay for DDD and get none of the benefit.
- Aggregates that grow too large. The symptom is slow loads and lock timeouts when two users edit the same order.
- Holding another aggregate by object reference, so one save quietly writes two aggregates in one transaction.
- Treating every noun as an entity. Most concepts are values; give them ids and you get equality bugs.