plugins/nw/skills/nw-domain-driven-design/SKILL.md
Strategic and tactical DDD patterns, bounded context discovery, context mapping, aggregate design rules, and decision frameworks for when to apply DDD
npx skillsauth add nwave-ai/nwave nw-domain-driven-designInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
DDD addresses domain complexity (business rules, language, boundaries), not technical complexity (scaling, performance). Apply selectively.
| Domain Type (Cynefin) | DDD Investment | Approach | |------------------------|----------------|----------| | Clear/Simple | None -- use CRUD | Standard patterns, no modeling overhead | | Complicated | Tactical only | Expert analysis, pragmatic patterns suffice | | Complex | Full strategic + tactical | Iterative modeling, bounded contexts, continuous refinement | | Chaotic | Stabilize first | Emergency patches, then apply DDD incrementally |
| Criterion | Core | Supporting | Generic | |-----------|------|------------|---------| | Competitive advantage | Yes | No | No | | Unique to organization | Yes | Partially | No | | Build or buy | Build | Build (simplified) | Buy/integrate | | DDD investment | Full strategic + tactical | Pragmatic tactical | Minimal/none | | Developer allocation | Senior/best talent | Mid-level | Junior/integration |
A bounded context is the boundary within which a particular domain model is defined and applicable. Different from subdomains: a bounded context is a software boundary; a subdomain is a problem-space concept. They should align but are not the same.
Discovery techniques:
Common mistakes:
Nine patterns organized by team relationship type.
| Pattern | Relationship | When to Use | |---------|-------------|-------------| | Partnership | Mutually dependent | Teams must deliver together; true reciprocal dependency | | Shared Kernel | Shared model subset | Close coordination around common concepts. Keep small | | Customer-Supplier | Upstream-downstream | Asymmetric dependencies requiring structured negotiation | | Conformist | Downstream adopts upstream | Integration simplicity outweighs design freedom | | Anti-Corruption Layer | Isolating translation | Protecting domain from poor upstream models; legacy integration | | Open Host Service | Standardized API | Supporting multiple downstream teams | | Published Language | Shared schema/format | Standardized formats for inter-context translation (iCalendar, vCard) | | Separate Ways | No connection | Contexts have no meaningful interdependencies | | Big Ball of Mud | Recognition only | Demarcate and quarantine legacy/poorly-structured systems |
Relationship categories: Mutually Dependent (Partnership) | Upstream/Downstream (Customer-Supplier, Conformist, ACL) | Free (Separate Ways)
Evolution: context maps evolve during refactoring. A Conformist relationship can be refactored to Customer-Supplier with an ACL. Draw context maps before writing code.
A common, rigorous vocabulary shared between developers and domain experts, scoped per bounded context.
Rules:
Code must reflect the language: class names, method names, variable names use domain terms. When the language changes, the code changes.
ProductId not Product. Prevents accidental cross-aggregate transactionsCommon mistakes: including objects for compositional convenience | "god aggregates" | direct object references | requiring immediate consistency where eventual suffices
| Criterion | Entity | Value Object | |-----------|--------|--------------| | Identity matters | Yes (tracked over time) | No (interchangeable if same values) | | Mutability | Mutable (state changes) | Immutable (replace, do not modify) | | Equality | By identity (ID) | By attribute values (structural) | | Examples | Customer, Order, Account | Money, Address, DateRange, Email |
Self-validating value objects: the only way for a value object to exist is to be valid. Validate in constructor; reject invalid input. Eliminates scattered validation logic.
Context sensitivity: whether something is Entity or Value Object depends on the bounded context. Address is a VO in e-commerce but an Entity in utility billing.
Represent something that happened. Named in past tense: OrderPlaced, UserRegistered, PaymentProcessed.
One repository per aggregate (not per entity). Interface in domain layer, implementation in infrastructure. Never expose persistence details to the domain.
Stateless operations spanning multiple aggregates. Named using ubiquitous language. Operate on domain types only.
Danger: overuse strips entities of behavior, creating anemic domain models.
| Aspect | Domain Service | Application Service | |--------|---------------|-------------------| | Contains | Domain logic | Orchestration logic | | Dependencies | Domain types only | Domain objects + infrastructure ports | | Layer | Domain model | Application | | Example | TransferFunds, CalculateDiscount | PlaceOrderHandler, RegisterUserUseCase |
Natural fit: domain in center (no infrastructure dependencies), ports as use cases, adapters for infrastructure. ACL at adapter boundaries. Each bounded context = a separate hexagon with own ports and adapters.
Command side uses full tactical DDD toolkit. Query side bypasses domain model entirely -- read models are denormalized views. CQRS is NOT event sourcing (they are independent patterns).
| CQRS Adds Value | CQRS Adds Complexity Without Value | |-----------------|-----------------------------------| | Complex domain with rich business rules | Simple CRUD applications | | Different read/write patterns | Minimal business logic | | Scale reads/writes independently | Small team without distributed experience | | Event-driven or event-sourced systems | Strong immediate consistency required everywhere |
Store domain events instead of current state. State = fold(initialState, events). Apply selectively to core domains only -- "not a top-level architecture" (Greg Young). Use for: audit trails, temporal queries, complex business domains.
Bounded contexts provide decomposition tool, but the mapping is not one-to-one. A bounded context may contain multiple services; a microservice may handle a focused subdomain within a larger context.
Decomposition criteria: subdomain boundaries (primary) | single responsibility | common closure | team autonomy | loose coupling
Cross-context operations: Saga pattern with compensating transactions. Choreography (decentralized, simple workflows) vs. Orchestration (centralized, complex workflows).
| Anti-Pattern | Signal | Fix | |-------------|--------|-----| | Anemic Domain Model | Entities = data only, logic in services | Move business rules into entities/aggregates | | God Aggregate | Too many entities/VOs, concurrency contention | Split by true invariant boundaries | | Domain Logic in App Layer | Business "if" statements in services | Push logic down to domain objects | | Primitive Obsession | Raw strings for domain concepts | Create value objects for every domain concept | | Missing Boundaries | Single unified model for entire system | Identify bounded contexts via language divergence | | Events as RPCs | Events depend on handler response | Events represent facts, fire-and-forget | | Database-Driven Design | Entities mirror tables | Model domain first, map to persistence second |
Three levels: Big Picture (25-30 people, entire business line) | Process Modeling (structured workflow grammar) | Design Level (DDD vocabulary, 1:1 to code).
Key artifacts: Domain Events (orange, past tense) | Commands (blue, imperative) | Aggregates (yellow, consistency boundaries) | Policies (lilac, automation rules) | Read Models (green, decision info) | External Systems (pink) | Hotspots (red, problems/questions).
Start with chaotic event exploration, enforce timeline, add commands/actors, identify aggregates/policies, mark hotspots. Iterate between divergent and convergent phases.
testing
Runs feature-scoped mutation testing to validate test suite quality. Use after implementation to verify tests catch real bugs (kill rate >= 80%).
development
Canonical AT completeness gate — research-anchored 7-category taxonomy (C1-C7) + 15-item mechanical checklist. Paradigm-neutral. Drives acceptance-designer reviewer verdict deterministically.
development
Canonical AT completeness gate — research-anchored 7-category taxonomy (C1-C7) + 15-item mechanical checklist. Paradigm-neutral. Drives acceptance-designer reviewer verdict deterministically.
testing
Methodology for minimizing test count while maximizing behavioral coverage - behavior definition, anti-pattern catalog, consolidation patterns, stopping criterion, coverage-preserving validation