Tactical Domain-Driven Design — stack-agnostic and canon-anchored (Evans/Vernon). Use when modeling a domain: creating or reviewing entities, aggregates, value objects, factories, domain events, domain errors or validators; naming domain concepts; deciding where a business rule or invariant lives; designing how aggregates are created, hydrated, validated and persisted; or defining what a module/bounded context owns — in any backend language or framework. The concrete realization (base types, ...
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Ddd?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/jorgecostamacia-ddd-code-harness)
---
name: ddd
description: >-
Tactical Domain-Driven Design — stack-agnostic and canon-anchored
(Evans/Vernon). Use when modeling a domain: creating or reviewing entities,
aggregates, value objects, factories, domain events, domain errors or
validators; naming domain concepts; deciding where a business rule or
invariant lives; designing how aggregates are created, hydrated, validated
and persisted; or defining what a module/bounded context owns — in any
backend language or framework. The concrete realization (base types,
factories, mapping, error vehicle) lives in the stack skills.
---
# Domain-Driven Design (tactical)
Canon-anchored (Evans' *Domain-Driven Design*, Vernon's *Implementing DDD*): sections marked **[canon]** state the standard; **[our convention]** marks deliberate additions, each with its why. The concrete realization (base classes, validation library, error vehicle) belongs to the stack skill. **A rule changes only by revision. If a rule does not fit the reality of a project, stop and tell the person which rule and why. Do not bend or skip it on your own.**
## Ubiquitous language — [canon]
- Name the domain concepts — modules, aggregates, properties, events — in the **business's own language**: the words that the domain experts use. Technical suffixes stay conventional English (`Validator`, `Service`, `Handler`, `Event`).
- One concept, one name, everywhere: code, contracts, storage, logs, conversation. If the business renames a concept, the code follows.
- If you can't name it in domain terms, you don't understand it yet — ask first, model later.
## Entities — [canon]
- Its **identity** defines an entity, not its attributes. It stays the same entity through every change of state. Two entities are the same when their identities are the same, whatever their other values.
- The entity gets its identity once, and the identity never changes.
- State changes through **behavior that keeps the invariants** — not through setters that let a caller break them.
## Aggregates — [canon]
- An aggregate is the **consistency boundary**: loaded, validated and persisted as a unit; the thing whose invariants must hold at every save.
- Keep them **small**: if two clusters of fields change for different business reasons, that's two aggregates. Reference other aggregates by identity, not by object graph.
- The **root** is the one entity that outsiders hold. Code reaches the entities inside through it, and code outside loads, saves and references only the root.
- Aggregates announce what happened through **domain events** (see below); no events escape a failed operation.
## Creation & hydration — [canon: the Factory pattern]
- **Creation goes through a factory** (`Create`…) that converts the input and **validates before returning** — an object cannot be *created* invalid. Factories are DDD's answer to guarded construction; Evans keeps a plain constructor for the simple cases. **[our convention]** Here it is *always* the factory, so every type has the same surface.
- **Converting is not normalizing** — [our convention]: a factory converts the input to **the type's own representation**: for example, a trimmed text, an instant in UTC, or a date at the store's precision. That representation is what the type *is*, not a rule of the domain. The factory **never coerces** an invalid value into a valid one. A layer whose types represent the source's values as they came keeps every value as it came: there the conversion is a pass-through.
- **Hydration is a separate path**: a plain constructor reserved for **reconstitution** — infrastructure (ORM, deserializer) restoring already-validated state. Evans makes a clear difference between creation and reconstitution, and only says that reconstitution handles a violated invariant *differently*. **[our convention]** Hydration is *recovery*, not *entry*, and does not validate again.
## Value objects — [canon]
- **Every attribute of an entity is a value object** — [our convention]. The canonical case is a primitive that carries a rule (email, money, quantity, identifier): the rule belongs to the type, not to the callers. Wrap a field with no rule today too. The type names the concept in the ubiquitous language, and it gives the rule a home on the day the rule appears. The identity is the one named exception: it may stay a primitive where the persistence technology makes a wrapped key costly.
- Immutable; equality **by value within the type** (two Emails with equal value are equal; an Email never equals a Name with the same string).
- Conversion is one-directional: VO → primitive is cheap; **primitive → VO only through the factory**.
- **A value object never coerces and carries no defaults** — [our convention]. It rejects an out-of-range value and does not pull it into range: a page size of 5000 that silently becomes 1000 hides the caller's mistake. A default is policy, so it belongs at the call site, where two callers can choose differently.
## Domain services — [canon]
- A **domain service** holds a business operation that belongs to no entity or value object — typically a rule that spans several aggregates. Its name comes from the ubiquitous language, it holds no state, and it lives in Domain. That is the Domain of its own context, or of the **shared kernel** when several contexts apply the same rule (a strategic pattern: see `clean-architecture`).
- **Rare by design**: if the rule fits an aggregate, it goes in the aggregate. A model whose rules have drifted into services is anemic.
- **Not an application service**: the application service orchestrates a use case (load, validate, execute, persist, publish) and holds no business rules. The domain service holds a rule and orchestrates nothing.
## Validation — two levels
**Level 1 — intrinsic validity (invariants): the type owns it — [canon]**
- *"Is this object correct in itself?"* Rules answerable by looking only at the object's values — **pure**: no services, no IO.
- Enforced by the **creation factory**: it runs the type's validator before returning — **nothing invalid escapes it**. A stack may add a conversion factory that does not validate, to build a whole from its parts and then validate the whole once. Its result is a part, not a created object: the whole's creation factory validates it. (Instance-based validators may build-then-validate internally; the guarantee is at the factory boundary, not the micro-order inside it.) Being pure, the validator needs no injection.
- Structural, so it holds on **every** path — HTTP, bus, backfill — nobody has to remember it.
**Level 2 — use-case validity (preconditions) — [canon-adjacent]**
- *"Is this correct object admissible **here, now**?"* Rules that need services or state: uniqueness against a store, referenced things existing, business-state conflicts. Not properties of the object — properties of the **system state at a moment** (the same email is valid today and a duplicate tomorrow).
- **[our convention]** They live in **Application**, with injected dependencies, and reject with **domain errors** ("already exists"…). The canon puts them in a domain service (Vernon) or a Specification (Evans). We keep them in the use case, next to the load or insert that they guard. We extract a domain service only when the same check repeats. Where a data constraint backs the rule (a unique index), the application check gives the friendly error and the constraint gives the concurrency-proof guarantee. A uniqueness rule is a reason for an index; any other index needs a measured plan.
- The test: **if the rule needs a service, it is level 2**. A type's validator must stay pure.
**How the rules are held — [our convention]**
- Define each rule **once**, in the type's own **composable validator**. The factory calls it (level 1). Boundary validators (request, aggregate) **compose** the validators of their parts and return the **complete list of failures, per field, in one pass**. Nothing invalid goes past the boundary, and errors never arrive one construction at a time.
- **[our convention] A content rule lives once, in the layer that owns the content.** In a chain of layers that each store what reaches them, one layer owns the content and holds its rules. A layer that only stores what arrives validates only what its own infrastructure needs (the identity, its own metadata) and keeps the content as it came. The two-level table says what *kind* of rule something is; this says *which layer* holds it.
- Multiple checkpoints, one source of rules — defense in depth, not duplication. *How* rules compose (declared constraints with collected violations, external composable validators…) is the **stack skill's decision**. Judge designs against these principles, not against a particular mechanism.
## Data access — [canon: the repository role]
- **Each aggregate has its own data accessor**: one dedicated access point for each aggregate, **internal to its module**. No module reaches into the storage of another aggregate, and no shared god-context shows every table to everyone. That boundary is what the repository pattern actually protects.
- The **stack skill decides** what the accessor is: a repository class (port in Domain, adapter in Infrastructure), or a narrow per-aggregate ORM context.
## Domain events — [canon]
- Named **past tense**, after the business fact: `<X>Created`, `<X>Loaded`, `<X>Deleted`. An event states something that **already happened** — subscribers react, they can't veto.
- Microsoft's microservices guidance separates **domain events** (inside the model) from **integration events** (the contract that crosses contexts), with an explicit mapping between the two. Vernon instead publishes the domain event itself through messaging, as part of the context's published language, and lets each consumer translate on its own side. **[our convention, with Vernon]** The context's published events *are* its domain events. The operation records on the root the event that its contract declares, so the model announces exactly what the outside sees. The root's own method or the use case builds it, as the stack skill decides. There is no second type, no mapping, and no in-process dispatcher that only copies fields. A separate internal event is justified only when the model needs to raise something it does not publish, or the two shapes diverge.
- Collection/dispatch mechanics (how aggregates accumulate events, when they're published) → the stack skill.
## Domain errors — [canon-adjacent]
- A domain error states a **business problem**: not found, already exists, rule violated. It **has a type and a fixed code that identifies it**. With them, you can trace its occurrences across logs and API responses, whatever the message text.
- Infrastructure failures (timeouts, connectivity) are **not** domain errors. They surface separately, and resilience policy (retries, redelivery) handles them. A retry of a business error is useless.
- *How* errors travel — an exception family per concept, a Result/Either type, error returns — is the **stack skill's decision**.
## Bounded contexts — [canon]
- A bounded context owns its model: its aggregates, its language, its rules. Other contexts integrate through its **published contracts** (events, commands), never by reaching into its internals. (Evans' MODULE is a smaller thing — a package inside one model; here "module" is the runtime's name for a bounded context.)
- The same business word may mean different things in different contexts — that's two concepts; don't force one shared model.