/ddd-skills:ddd-architecture-layeredfrom ddd-skills
Guides transformation from traditional 3-layer to DDD 4-layer architecture (Interface/Application/Domain/Infrastructure) with directory structure, dependency inversion, ArchUnit validation, and Spring Boot integration. For small to medium teams adopting DDD.
Ships in ddd-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
DDD Layered Architecture 是 DDD 生态中最简的入门架构。它将传统三层(Controller/Service/DAO)重构为四层(Interface/Application/Domain/Infrastructure),通过依赖倒置使领域层成为系统核心。
| 用户类型 | 特征 | 推荐入口 |
|---|---|---|
| DDD 初学者 | 了解 MVC 三层,想入门 DDD | 先读 Core Principles → 看 06 单体简单示例 → 跟随 Implementation Phases |
| 架构师 | 负责项目架构选型和技术规划 | 先读 When to Use 边界 → 对照 06-12 规模谱系选型 → 看 Evolution 演进路径 |
| 高级开发者 | 有三层开发经验,需要落地分层 | 直接看 Directory Structure + Writing Rules → 使用 07/08 示例启动项目 |
| 技术负责人 | 评估团队引入 DDD 的可行性和成本 | 先读 When to Use + 场景边界 → 评估 Implementation Phases 时间 → 参考 09-12 微服务规划 |
Step 1: awesome (入门) → 了解 DDD 概念
Step 2: selector (架构选型) → 确认分层架构适合
Step 3: layered (分层架构) ★ ← 你在这里
Step 4: domain-designer (领域设计) → 设计领域模型
Step 5: code-reviewer (审查) → 分层合规检查
| # | 场景 | 说明 |
|---|---|---|
| 1 | 团队 < 10 人,DDD 刚起步 | 学习曲线低,分层架构是最简入口 |
| 2 | 中等复杂业务,有明确领域模型 | 聚合根清晰,充血模型收益大 |
| 3 | 已有三层项目逐步演进 | 三层→四层渐进迁移,不颠覆原架构 |
| 4 | Spring Boot/MyBatis 技术栈 | 国内主流技术栈,示例可直接复用 |
| # | 场景 | 条件 |
|---|---|---|
| 1 | 团队 10-20 人 | 需先评估是否要物理模块隔离 → 见 08 多模块方案 |
| 2 | 多聚合根复杂业务 | 需确认聚合边界清晰 → 先做事件风暴再落地分层 |
| 3 | 已有微服务拆分意向 | 先按分层架构落地单服务 → 再参考 09-12 微服务方案拆分 |
| 4 | 非 Spring Boot 技术栈 | DDD 分层思想通用,但目录结构和代码示例需自行适配 |
| # | 场景 | 替代方案 |
|---|---|---|
| 1 | 纯 CRUD 无业务规则 | 直接用 Spring Boot 三层 + MyBatis-Plus 更省成本 |
| 2 | 多入口系统(REST+CLI+MQ) | 参考 Hexagonal Architecture(六边形架构)更合适 |
| 3 | 需要物理模块隔离 | 使用 COLA 架构或微服务方案(见 09-12 示例) |
| 4 | 高频基础设施替换 | 基础设施频繁变化时,推荐 Clean Architecture |
使用前提:有稳定领域专家 + 团队愿意切换到充血模型。
三层架构: DDD 四层:
Controller Interface (用户接口层)
↓ ↓
Service Application (应用层) — 纯编排
↓ ↓
DAO Domain (领域层) ★ — 核心业务
↓
Infrastructure (基础设施层)
| # | 原则 | 说明 |
|---|---|---|
| 1 | Domain 零依赖 | 不 import Spring/JPA/MyBatis |
| 2 | 依赖倒置 | Infra 实现 Domain 定义的接口 |
| 3 | Application 薄层 | 只编排,不放业务 if/else |
| 4 | Interface 协议转换 | 只做 DTO 转换,不含业务 |
{project}/
├── {project}-interface/ # 用户接口层
│ ├── controller/ # REST 控制器
│ ├── dto/ # 请求/响应 DTO
│ │ ├── request/
│ │ └── response/
│ ├── converter/ # DTO ↔ Command 转换
│ └── advice/ # 全局异常处理
├── {project}-application/ # 应用层
│ ├── service/ # 应用服务(纯编排)
│ ├── command/ # 命令对象(写)
│ ├── query/ # 查询对象(读)
│ ├── assembler/ # DO ↔ DTO 组装
│ └── event/ # 事件处理
├── {project}-domain/ # 领域层 ★(零依赖)
│ ├── {aggregate}/ # 按聚合分包
│ │ ├── entity/ # 实体+聚合根(充血)
│ │ ├── valueobject/ # 值对象(不可变)
│ │ ├── service/ # 领域服务
│ │ ├── repository/ # 仓储接口(只定义)
│ │ └── event/ # 领域事件
│ ├── factory/ # 领域工厂
│ └── shared/ # 共享值对象
└── {project}-infrastructure/ # 基础设施层
├── repository/ # 仓储实现
├── persistence/ # PO 实体+映射
├── messaging/ # 消息队列
├── external/ # 外部服务
└── config/ # 配置
依赖方向:Interface → Application → Domain ← Infrastructure
| 层 | 可依赖 | 不可依赖 |
|---|---|---|
| Interface | Application | Domain、Infrastructure |
| Application | Domain | Interface、Infrastructure |
| Domain | JDK 原生类型 | 所有其他层 + 框架 |
| Infrastructure | Domain | Interface、Application |
代码命名:
Order(业务名,非 OrderEntity)Money、Email(不可变,无 setter)OrderRepository(在 Domain)JpaOrderRepository(在 Infra)OrderPlaced(过去式)禁止事项:
Phase 1: 识别聚合(1-2天)
→ 事件风暴 → 聚合根 → 不变式 → 通用语言表
Phase 2: 搭建分层骨架(1天)
→ 多模块 pom.xml → 基类(Entity/VO/AggregateRoot)
Phase 3: 实现领域层(2-5天)
→ 充血模型 → Repository 接口 → 领域服务 → 领域事件
Phase 4: 实现基础设施层(2-3天)
→ Repository 实现 → PO↔DO 映射 → DB 配置
Phase 5: 实现应用层(1-2天)
→ AppService 编排 → Command/Query → 事务管理
Phase 6: 实现接口层(1-2天)
→ Controller → DTO → 参数校验 → 异常处理
Phase 7: 审查验证(0.5天)
→ ddd-code-reviewer → ArchUnit → Domain 测试 ≥ 80%
Phase 1: 传统三层 ← 当前状态
Phase 2: 四层基础 ← 抽取 Domain + Infra
Phase 3: 充血模型 ← 实体含业务方法
Phase 4: 领域事件+CQRS ← L1 模型分离
Phase 5: 升级架构 ← 根据需要选 Hexagonal/Clean/COLA
15 个常见陷阱详见 references/09-gotchas.md。
15 个常见问题详见 references/10-faq.md。
分层架构, DDD 四层, 传统分层, 三层变四层, DDD 分层, layered architecture, DDD layered, three tier to DDD four layer, 依赖倒置, 充血模型, 贫血模型, 仓储模式, Repository 模式, Spring Boot DDD, MyBatis DDD, 目录结构 DDD, ArchUnit 分层检查, 依赖方向验证
| 文件 | 内容 |
|---|---|
| references/01-domain-layer.md | 领域层详解:实体、值对象、聚合、领域服务 |
| references/02-application-layer.md | 应用层详解:AppService、Command/Query、事务 |
| references/03-interface-layer.md | 接口层详解:Controller、DTO、异常处理 |
| references/04-infrastructure-layer.md | 基础设施层详解:Repository 实现、PO 映射 |
| references/05-dependency-rules.md | 依赖方向规则与层间通信约定 |
| references/06-migration-guide.md | 三层→四层渐进式迁移指南 |
| references/07-archunit-config.md | ArchUnit 自动化验证配置 |
| references/08-cqrs-integration.md | CQRS 轻量集成(L1 模型分离) |
| references/09-gotchas.md | 15 个常见陷阱 |
| references/10-faq.md | 15 个常见问题 |
| 文件 | 内容 |
|---|---|
| examples/spring-boot-order-example.md | Spring Boot 订单系统完整示例 |
| examples/ddd4j-springboot-practice.md | DDD4J Spring Boot 实战(Nova Coffee) |
| examples/partme-91-code-example.md | 在线请假系统完整代码 |
| examples/ddd4j-spring-boot-guide.md | Spring Boot DDD 分层实操指南 |
| examples/05-archunit-layered-config.md | ArchUnit 分层验证配置 |
| examples/06-monolith-simple.md | 单体简单项目:单模块四层分包 |
| examples/07-monolith-complex.md | 单体复杂项目:多聚合根 + 跨聚合编排 |
| examples/08-monolith-multi-module.md | 单体多模块:四层 Maven 模块隔离 |
| examples/09-microservice-simple.md | 微服务简单:每服务一个 DDD 分层单体 |
| examples/10-microservice-complex.md | 微服务复杂:事件驱动 + Saga 编排 |
| examples/11-microservice-multi-module.md | 微服务多模块:每服务四层子模块 |
| examples/12-microservice-complex-multi.md | 微服务复杂多模块:Shared Kernel + CQRS |
当使用本 Skill 时,提供以下产出:
agents/openai.yaml (opens in a new tab)examples/01-ddd4j-spring-boot-guide.md (opens in a new tab)examples/02-ddd4j-springboot-practice.md (opens in a new tab)examples/03-partme-91-code-example.md (opens in a new tab)examples/04-spring-boot-order-example.md (opens in a new tab)examples/05-archunit-layered-config.md (opens in a new tab)examples/06-monolith-simple.md (opens in a new tab)examples/07-monolith-complex.md (opens in a new tab)examples/08-monolith-multi-module.md (opens in a new tab)examples/09-microservice-simple.md (opens in a new tab)examples/10-microservice-complex.md (opens in a new tab)examples/11-microservice-multi-module.md (opens in a new tab)examples/12-microservice-complex-multi.md (opens in a new tab)references/01-domain-layer.md (opens in a new tab)references/02-application-layer.md (opens in a new tab)references/03-interface-layer.md (opens in a new tab)references/04-infrastructure-layer.md (opens in a new tab)references/05-dependency-rules.md (opens in a new tab)references/06-migration-guide.md (opens in a new tab)references/07-archunit-config.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Generated from the plugin's indexed repository (full-stack-skills/ddd-skills) — no native marketplace selected
npx claudepluginhub p/full-statck-skills-ddd-skills16 skills in this plugin
/ddd-api-designerAPI design from domain model — CQRS command/query separation, REST API endpoint design, data object conversion chain (PO→DO→DTO→VO), unified response format, OpenAPI/Swagger generation, BFF pattern, API versioning, and security design. Use when user asks about API design, REST API, OpenAPI, BFF, DTO design, 接口设计, or data object conversion./ddd-architecture-awesomeProvides comprehensive guidance for Domain-Driven Design (DDD) concepts, strategic design, tactical design, and architecture patterns. Includes DDD applicability assessment, Pattern Boundaries table (what each pattern is and is NOT), Complexity Ladder (when to adopt each level), Core Concepts quick reference with Entity/ValueObject/Aggregate patterns, full architecture landscape overview, anti-patterns with fixes, multi-language reference implementations, and complete primary source references. Use when the user asks about DDD basics, needs to understand DDD concepts, evaluates DDD applicability, or wants an introduction to domain-driven design./ddd-architecture-cleanComprehensive guidance for Clean Architecture (整洁架构) — Robert C. Martin's Clean Architecture with Enterprise Business Rules, Use Cases, Interface Adapters, and Frameworks layers. Covers core entities, use case interactors, dependency rules, and full implementation steps with Java/Spring Boot examples. Use when user asks about Clean Architecture, 整洁架构, Robert Martin, Uncle Bob, use case driven architecture, or needs to implement DDD with clean architecture./ddd-architecture-colaComprehensive guidance for COLA v5 Architecture (菱形架构) — Alibaba's COLA framework with adapter/application/domain/infrastructure layers. Covers full project scaffolding, architecture validation (cola-creator + cola-validator combined), dependency rule checking with ArchUnit, CQRS integration, and multi-module Maven/Gradle project generation. Use when user asks about COLA architecture, 菱形架构, cola-creator, cola-validator, 创建 COLA 项目, or needs Alibaba DDD framework./ddd-architecture-docDDD architecture documentation generation — C4 model diagrams (Context/Container/Component/Code), ADR (Architecture Decision Records), domain model documentation, API documentation, architecture decision log, and team communication templates. Use when user asks about architecture documentation, 架构文档, ADR, C4 diagram, domain model doc, 架构决策记录, or needs to document DDD architecture for team./ddd-architecture-evaluatorDDD architecture evaluation and evolution — DDD maturity 5-level model (AdHoc/Aware/Applied/Scaled/Optimized), architecture fitness assessment (business alignment/team fit/tech fit/evolution capability), technical debt quantification with scoring, architecture evolution roadmap (4 phases), and migration risk assessment. Use when user asks about architecture evaluation, 架构评估, DDD maturity, 技术债务, architecture evolution, 架构演进, or needs to assess DDD implementation health.Other plugins' skills for the same job