Book domain driven design full

From yanacuti1121/Yana-AI · View interactive version

Book domain driven design full is an AI agent skill from the GitHub repository yanacuti1121/Yana-AI. Domain-Driven Design (Eric Evans) — Full rules — comprehensive mandatory coding standards. Use when asked to apply Domain-Driven Design principles or review code against Domain-Driven Design standards.

How to use this skill

Skill file

OBEY Domain-Driven Design by Eric Evans

Purpose

This repository follows Domain-Driven Design.

All code generation, modification, and review must optimize for:

This file is a binding engineering policy: MUST is binding, SHOULD is a strong default, and MUST NOT is forbidden.


Primary Directive

When uncertain, prefer the option that makes the domain model clearer.

Do not optimize primarily for:

The model must serve the business meaning first.


What DDD Means in This Repository

DDD here does not mean:

DDD here does mean:


Knowledge Crunching and Deep Models

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Treat the model as discovered, not invented from technical structure.
  2. Before adding abstractions, identify what domain experts would call the concept.
  3. When requirements are ambiguous, look for missing domain distinctions instead of forcing generic names.
  4. Let awkward code, contradictory language, and repeated conditionals trigger deeper modeling.
  5. Update names and boundaries when new domain insight appears.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Model-Driven Design

Rules (MUST unless marked SHOULD or MUST NOT)

  1. The implemented design must reflect the model used in discussion.
  2. If the model cannot guide code, refine the model or the code until they align.
  3. Domain objects must represent behavior and meaning, not just persistence state.
  4. Keep modelers close to implementation. Do not separate analysis from coding so far that the model becomes theoretical.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Breakthrough and Deeper Insight

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Expect useful models to change after deeper insight.
  2. Treat a breakthrough model as a candidate for deliberate refactoring, not as churn.
  3. When a better model appears, compare its explanatory power against migration cost.
  4. Preserve working behavior while moving toward the deeper model in safe steps.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Making Implicit Concepts Explicit

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Listen for domain language that is not represented in code.
  2. Scrutinize awkward APIs, repeated branches, and contradictory names.
  3. Read domain references, policies, regulations, and prior art when available.
  4. Try multiple model shapes before settling on one for complex concepts.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Ubiquitous Language

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Use the exact business terms used by domain experts inside a bounded context.
  2. One concept must have one name inside a bounded context.
  3. One name must not mean different concepts inside a bounded context.
  4. Method names, test names, and modules must use the same vocabulary as the domain.
  5. Rename code when the domain understanding improves.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Communication Artifacts

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Documents and diagrams must support the ubiquitous language.
  2. Written design material must be short enough to stay maintained.
  3. Executable tests are preferred for rules that can be verified.
  4. Diagrams should emphasize boundaries, relationships, invariants, and lifecycle over class inventory.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Scenario Walkthroughs

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Validate the model by walking through real application scenarios.
  2. Use scenarios to test whether entities, value objects, aggregates, repositories, and factories collaborate naturally.
  3. When a scenario feels procedural or awkward, look for missing model concepts or wrong boundaries.
  4. Revisit aggregate and module boundaries after scenario walkthroughs reveal pressure.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Layered Architecture and Smart UI

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Keep the domain layer as the place where the model and business rules live.
  2. Separate presentation, application coordination, domain behavior, and infrastructure when the domain is complex enough to need model-driven design.
  3. Let application code coordinate tasks without owning domain decisions.
  4. Keep infrastructure services and framework concerns outside domain objects.
  5. Use Smart UI only for simple applications where rich domain abstraction, reuse, integration, and deep business rules are not important.

Anti-patterns (MUST NOT)


Bounded Contexts

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Every substantial domain area must belong to a clearly identified bounded context.
  2. A model is valid only inside its own bounded context.
  3. Concepts from another context must not be imported directly as if they were native.
  4. Translation across contexts must be explicit.
  5. Shared models across multiple contexts are forbidden unless intentionally governed as a shared kernel.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Strategic Design

Core Domain

  1. Invest the most care in the core domain.
  2. Protect the core domain from foreign models, vendor schemas, and generic abstractions.
  3. Keep the core domain expressive even if supporting areas are simpler.

Supporting and Generic Subdomains

  1. Do not over-model commodity concerns.
  2. Use simpler models where business complexity is low.
  3. Save the richest modeling effort for the parts that matter strategically.

Context Mapping

  1. Integration relationships must be visible in code.
  2. Ownership of translation must be explicit.
  3. Upstream and downstream influence must be reflected in adapters and contracts.

Anti-patterns (MUST NOT)


Model Integrity Patterns

Continuous Integration Within a Context

  1. A bounded context must keep one internally consistent model.
  2. Team members working in the same context must integrate terminology and model changes continuously.
  3. Conflicting meanings inside one context must be resolved quickly through naming, tests, and refactoring.

Context Relationships

Use context relationship patterns intentionally:

Context Transformations

  1. Move from Separate Ways to Shared Kernel only when the overlap is small, valuable, and worth coordination.
  2. Move from Shared Kernel to Continuous Integration only when teams are ready to share one model frequently.
  3. Phase out legacy systems by protecting the new model and replacing responsibilities incrementally through translations.
  4. Evolve Open Host Service toward Published Language when interchange stability is needed beyond one service.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Distillation

Core Domain

  1. Identify the part of the model that creates strategic advantage.
  2. Put the strongest modeling effort and cleanest design into that core.
  3. Do not bury the core under generic mechanisms, infrastructure, or broad shared abstractions.

Distillation Patterns

Use these patterns when they clarify priority and investment:

Required behavior (MUST)

Anti-patterns (MUST NOT)


Large-Scale Structure

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Large-scale structure should help people understand the system, not freeze it.
  2. Let structure evolve as the model evolves.
  3. Use a guiding structure only when it reduces cognitive load across contexts.
  4. Keep the structure minimally restrictive.

Patterns

Use these patterns deliberately:

Required behavior (MUST)

Anti-patterns (MUST NOT)


Strategic Decision Making

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Assess the current model and context map before prescribing a strategic structure.
  2. Decide who owns strategic design choices explicitly.
  3. Let application development inform strategy; do not impose strategy detached from implementation feedback.
  4. Architecture teams must stay customer-focused and model-focused, not framework-focused.
  5. Strategic decisions must remain revisable as domain understanding changes.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Entities

Use entities when

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Entities must have explicit identity.
  2. Entities must protect their own valid state transitions.
  3. Entities must expose intention-revealing behavior, not arbitrary state changes.
  4. Entities must not be treated as passive records in behavior-rich domains.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Value Objects

Use value objects when

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Value objects must be immutable by default.
  2. Construction must guarantee validity.
  3. Equality must be by value, not by identity.
  4. Validation for the concept should live inside the value object.
  5. Replace primitive obsession aggressively where the concept matters.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Associations and Modules

Associations

  1. Model associations only when they support behavior or meaning.
  2. Prefer simpler, more navigable associations over fully connected object graphs.
  3. Reduce bidirectional associations unless the domain requires them.
  4. Reference other aggregates by identity unless direct object traversal is part of an invariant boundary.

Modules

  1. Modules must communicate domain concepts and bounded context ownership.
  2. Organize modules around model meaning, not only technical layers.
  3. Keep related concepts together when they change together.
  4. Avoid infrastructure-driven packaging that hides the domain.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Aggregates

Purpose

Aggregates are consistency boundaries, not just object graphs.

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Design aggregates around invariants that must be consistent immediately.
  2. Keep aggregates as small as possible.
  3. All modifications that affect aggregate invariants must go through the aggregate root.
  4. Reference other aggregates by identity unless stronger consistency is truly required.
  5. Keep transactional boundaries aligned with invariants; do not expand transactions across aggregates merely for convenience.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Domain Services

Use a domain service only when

Rules (MUST unless marked SHOULD or MUST NOT)

  1. A domain service must express a domain concept, not a technical convenience.
  2. If behavior clearly belongs to an entity or value object, keep it there.
  3. Do not move behavior into services merely to keep entities thin.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Explicit Concepts and Specifications

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Make implicit constraints explicit in the model.
  2. Model domain processes as domain objects when the process has business meaning.
  3. Use specifications for named, combinable business rules that answer whether something satisfies a criterion.
  4. Keep specifications in domain language, not query language.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Repositories

Purpose

Repositories provide access to aggregates as part of the model.

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Repositories exist for aggregate roots, not for every table.
  2. Repository interfaces must be defined by the domain or application model that uses them.
  3. Repositories must return domain objects or domain-oriented results.
  4. Repository contracts must reflect intent where useful.
  5. Repositories must not become universal query utilities.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Factories

Use factories when

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Factories must create valid objects.
  2. Factories must encode domain creation rules, not technical object assembly.
  3. Clients and mappers must not contain business construction logic.
  4. Choose the factory site where creation ownership fits the model.
  5. Use constructors directly when creation is simple, intention-revealing, and does not expose complex invariants.
  6. Treat reconstitution from storage separately from new-object creation.

Anti-patterns (MUST NOT)

Application Layer

Purpose

The application layer coordinates application tasks.

It does not replace the domain model.

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Application services load aggregates, call domain behavior, persist results, and coordinate side effects.
  2. Application services must not hold core business invariants that belong in the domain.
  3. Application services must speak the ubiquitous language.
  4. Application services may coordinate transactions and integration publication, but should not become procedural god classes.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Infrastructure

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Infrastructure is subordinate to the model.
  2. Object-relational mappings, serializers, external technical clients, delivery mechanisms, messaging details, caches, and framework types must stay out of the domain model.
  3. Infrastructure must adapt to the model, not the reverse.
  4. Persistence shape must not define the domain shape.

Anti-patterns (MUST NOT)


Translation at Boundaries

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Translation is mandatory at context boundaries.
  2. Translation is usually mandatory between domain objects and transport or persistence representations.
  3. Anti-corruption layers must preserve the local model rather than mirror foreign models.
  4. Foreign terms must not silently invade the local ubiquitous language.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Supple Design

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Design interfaces that reveal intention in domain language.
  2. Prefer side-effect-free functions for calculations and queries.
  3. Make assertions and invariants explicit in the model.
  4. Shape objects around conceptual contours, not arbitrary technical convenience.
  5. Use standalone classes where a concept can be understood without unnecessary dependencies.
  6. Favor operations that are closed under meaningful domain types when that improves clarity.
  7. Use declarative design when it makes rules easier to read, combine, and verify.
  8. Combine specifications with AND, OR, or NOT only while each component meaning remains readable.
  9. Use subsumption when one specification or category includes another and that relationship matters.

Required behavior (MUST)

Anti-patterns (MUST NOT)


Analysis and Model Patterns

Analysis Patterns

  1. Use prior domain modeling knowledge when it fits the current domain.
  2. Do not force an analysis pattern when local language contradicts it.
  3. Adapt patterns to the bounded context rather than importing them wholesale.
  4. Search domain literature, prior art, and established formalisms when the team lacks concepts for a good model.
  5. Use exploration teams for hard modeling problems only when their findings are tested in code and returned to the main team.

Design Patterns in the Model

Use design patterns only when they express the domain model:

Required behavior (MUST)

Anti-patterns (MUST NOT)


Code Generation Rules

When generating code, always do the following in order.

1. Identify the domain concept first

Before writing code, identify:

Do not start from:

unless the task is purely infrastructural.

2. Prefer modeling over generic plumbing

If the domain contains real rules:

Do not default to procedural services operating on passive records.

3. Use primitives only when they truly carry no domain meaning

Wrap primitives when meaning, validation, unit semantics, or invariants matter.

4. Protect invariants at the model boundary

Do not rely on UI validation, application validation, or repository validation as the primary protection for business rules.

5. Keep the model persistence-ignorant

Do not shape types or boundaries primarily for object-relational mapping convenience.

6. Keep bounded contexts visible in structure

Prefer feature or context ownership in modules and packages.

Avoid architecture that hides business boundaries behind generic folders.

7. Translate foreign models explicitly

Whenever another context, system, transport layer, or persistence format is involved, create translation rather than leakage.

8. Look for implicit concepts

When conditionals, flags, validation blocks, or repeated calculations express business meaning, extract named concepts such as value objects, policies, specifications, or domain services.

9. Preserve strategic priorities

Give the core domain more modeling care than supporting or generic subdomains. Keep context relationships and distillation choices visible when they affect code.


Review Rules

When reviewing or modifying code, actively look for:

Language problems

Model problems

Boundary problems

Aggregate problems

Service problems

Infrastructure problems

Strategic problems


Testing Rules

Domain tests first

Prioritize tests for:

Rules (MUST unless marked SHOULD or MUST NOT)

  1. Tests must read in the ubiquitous language.
  2. Tests must verify allowed and forbidden state transitions.
  3. Tests must verify that invalid objects cannot be created through supported paths.
  4. Tests must verify translation behavior where anticorruption layers exist.
  5. Infrastructure tests must stay separate from domain tests.
  6. Tests for the core domain should read like executable examples of the model.
  7. Test context boundaries so translations preserve intended meaning.
  8. Test that one context does not silently break another context's assumptions.

Anti-patterns (MUST NOT)


Forbidden Patterns

Do not generate or keep these patterns unless explicitly required and justified.

Passive Domain Model

Smart UI

Persistence-Driven Design

Primitive Obsession

Shared Model Everything

God Services

Invalid Construction

Fake DDD

Context Map Blindness

Pattern-Driven Obscurity


Refactoring Rules

When changing existing code:

  1. Recover the ubiquitous language.
  2. Move business rules into entities, value objects, aggregates, or domain services where appropriate.
  3. Introduce value objects where primitives hide meaning.
  4. Redraw aggregate boundaries where invariants are unclear or transactional scope is too large.
  5. Separate bounded contexts that are currently bleeding together.
  6. Add translation layers where foreign models leak into the domain.
  7. Break up god services into focused application services plus richer domain behavior.
  8. Remove persistence and transport assumptions from the model.
  9. Extract explicit constraints, specifications, and policies from repeated conditionals.
  10. Clarify context relationships when integration code is ambiguous.
  11. Distill the core domain out of supporting mechanisms when strategic logic is buried.
  12. Preserve behavior while improving the model incrementally.

Do not rewrite everything at once unless explicitly required.


Output Expectations

When asked to implement a feature, default to producing:

When asked to review code:

When asked to modify code:


Review Checklist

Before finalizing any change, verify:

If any answer is no, revise before shipping.


Final Instruction

When uncertain, choose the option that:

  1. makes the domain language sharper
  2. protects invariants inside the model
  3. keeps bounded contexts explicit
  4. reduces primitive obsession
  5. keeps infrastructure subordinate to domain meaning

Reject changes that make the code more generic but the domain less clear.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.