AgentSkillsCN

domain-driven-design

为领域驱动设计的架构与实施提供专家指导。当设计复杂业务系统、定义有界上下文、构建领域模型、在模块化单体与微服务之间抉择、实施聚合/实体/值对象,或当用户提到“DDD”、“领域驱动设计”、“有界上下文”、“聚合”、“领域模型”、“普遍语言”、“事件风暴”、“上下文映射”、“领域事件”、“贫血领域模型”、“战略设计”、“战术模式”或“领域建模”时,就使用此技能。有助于做出架构决策、识别子域、设计聚合,并避免常见的DDD陷阱。

SKILL.md
--- frontmatter
name: domain-driven-design
description: Expert guidance for Domain-Driven Design architecture and implementation. Use when designing complex business systems, defining bounded contexts, structuring domain models, choosing between modular monolith vs microservices, implementing aggregates/entities/value objects, or when users mention "DDD", "domain-driven design", "bounded context", "aggregate", "domain model", "ubiquitous language", "event storming", "context mapping", "domain events", "anemic domain model", strategic design, tactical patterns, or domain modeling. Helps make architectural decisions, identify subdomains, design aggregates, and avoid common DDD pitfalls.
spring-boot-version: "4.0"

Domain-Driven Design Skill

DDD manages complexity through alignment between software and business reality. Strategic design (boundaries, language, subdomains) provides more value than tactical patterns (aggregates, repositories).

When to Apply DDD

Apply DDD when:

  • Domain has intricate business rules
  • System is long-lived and high-value
  • Domain experts are available
  • Multiple teams/departments involved
  • Software represents competitive advantage

DDD is overkill when:

  • Simple CRUD applications
  • Tight deadlines, limited budgets
  • No domain experts available
  • Complexity is purely technical, not business

Core Workflow

  1. Domain Discovery → 2. Bounded Context Definition → 3. Context Mapping → 4. Architecture Selection → 5. Tactical Implementation

See WORKFLOW.md for detailed step-by-step instructions for each phase.

Quick Reference

Subdomain Types (Problem Space)

TypeInvestmentExample
CoreMaximum - competitive advantageRecommendation engine, trading logic
SupportingCustom but quality tradeoffs OKInventory management
GenericBuy/outsourceAuth, email, payments

Key Decision: Entity vs Value Object

  • Entity: Has identity, tracked through time, mutable → Customer, Order
  • Value Object: Defined by attributes, immutable, interchangeable → Money, Address, Email

Default to value objects. Only use entities when identity matters.

Aggregate Design Rules (Vaughn Vernon)

  1. Model true invariants in consistency boundaries
  2. Design small aggregates (~70% should be root + value objects only)
  3. Reference other aggregates by ID only
  4. Use eventual consistency outside the boundary

Architecture Decision

code
Start with modular monolith when:
├── Team < 20 developers
├── Domain boundaries unclear
├── Time-to-market critical
└── Strong consistency required

Consider microservices when:
├── Bounded contexts have distinct languages
├── Teams can own full contexts
├── Independent scaling required
└── DevOps maturity exists

Detailed References

Critical Reminders

  1. Ubiquitous language first - Code should read like business language
  2. Strategic before tactical - Understand boundaries before implementing patterns
  3. Apply tactical patterns selectively - Only in core domains where complexity warrants
  4. One aggregate per transaction - Cross-aggregate consistency via domain events
  5. Persistence ignorance - Domain layer has no infrastructure dependencies

Related Skills

NeedSkill
Data layer implementationspring-boot-data-ddd — JPA/JDBC aggregates, repositories, transactions
REST API layerspring-boot-web-api — Controllers, validation, exception handling
Module boundariesspring-boot-modulith — Module structure, event-driven communication
Testing patternsspring-boot-testing — Aggregate tests, module tests, Scenario API
Security for domainsspring-boot-security — Method-level authorization, role-based access