ddd-code-reviewer
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDDD Code Reviewer
DDD Code Reviewer
DDD 代码审查 + 反模式检测 + 5 维度合规评分。验证代码是否遵循 DDD 最佳实践,输出结构化审查报告。
DDD code review + anti-pattern detection + five-dimension compliance scoring. Verify whether the code follows DDD best practices and produce a structured review report.
Workflow
Workflow
DDD 代码审查三步走:
Step 1: 代码扫描 — 反模式检测清单逐项检查 + 源代码扫描
Step 2: 评分计算 — 5 维度逐项评分(分层/领域/命名/结构/测试)
Step 3: 报告输出 — 结构化审查报告(评分 + 反模式列表 + 修复建议)每次审查必须走完三步,输出完整报告。只口述问题不输出文档,团队无法追踪改进。
DDD code reviews follow three steps:
Step 1: 代码扫描 — 反模式检测清单逐项检查 + 源代码扫描
Step 2: 评分计算 — 5 维度逐项评分(分层/领域/命名/结构/测试)
Step 3: 报告输出 — 结构化审查报告(评分 + 反模式列表 + 修复建议)Every review must complete all three steps and produce a full report. Merely describing issues without producing a document makes it impossible for the team to track improvements.
When to Use
When to Use
触发词(触发即用)
Trigger Terms (Use Immediately When Mentioned)
代码审查DDD review反模式检测anti-pattern贫血模型充血模型检查架构审查分层合规代码质量评分layering compliancerich domain checkreview my DDD code代码审查DDD review反模式检测anti-pattern贫血模型充血模型检查架构审查分层合规代码质量评分layering compliancerich domain checkreview my DDD code适用前提
Prerequisites
| 条件 | 说明 |
|---|---|
| 项目已采用 DDD 思想 | 非 DDD 项目无审查基础 |
| 有可审查的源代码 | 需提供代码路径或片段 |
| 期望发现反模式 | 不只是格式检查,更关注领域模型健康度 |
| Condition | Description |
|---|---|
| The project has adopted DDD concepts | A non-DDD project has no foundation for this review |
| Reviewable source code is available | A code path or snippet must be provided |
| The goal is to identify anti-patterns | The review goes beyond formatting checks and focuses on domain model health |
不适用场景
When Not to Use
| 跳过 | 改用 |
|---|---|
| 非 DDD 项目(纯 CRUD) | 标准代码审查(SonarQube / Checkstyle) |
| 项目尚未引入 DDD | |
| 刚写完第一个 DDD 代码 | 先写完再审查 |
| 需要架构选型 | |
| 需要架构健康度评估 | |
| Skip | Use Instead |
|---|---|
| Non-DDD project (pure CRUD) | Standard code review (SonarQube / Checkstyle) |
| The project has not yet adopted DDD | |
| The first DDD implementation has just been written | Finish writing it before the review |
| Architecture selection is needed | |
| An architecture health assessment is needed | |
Audience
Audience
This skill is designed for: Backend developers (implementing DDD architectures), Software architects (evaluating and selecting patterns), Tech leads (reviewing team implementations), and DDD beginners (learning domain-driven design fundamentals).
This skill is designed for: Backend developers (implementing DDD architectures), Software architects (evaluating and selecting patterns), Tech leads (reviewing team implementations), and DDD beginners (learning domain-driven design fundamentals).
Rules
Rules
- Every code review must include a 5-dimension scoring report.
- P0 anti-patterns (anemia model, layer violation, cross-aggregate ref) block merge.
- Domain layer must have zero framework dependencies — verified via ArchUnit.
- Review reports must include fix suggestions ordered by priority (P0→P1→P2).
- Every code review must include a five-dimension scoring report.
- P0 anti-patterns (anemic model, layer violation, cross-aggregate reference) block a merge.
- The Domain layer must have zero framework dependencies—verified via ArchUnit.
- Review reports must include fix suggestions ordered by priority (P0→P1→P2).
Anti-Pattern Checklist
Anti-Pattern Checklist
12 种 DDD 反模式,按 P0(阻塞合并)/P1(下个版本前修复)/P2(持续改进)分级。
- P0 — 必须修复: 贫血模型、上帝 Service、跨聚合直接引用、领域层框架依赖、循环依赖、Repository 返回非聚合根
- P1 — 应该修复: Controller 业务逻辑、Application Service 有 SQL、值对象可变
- P2 — 持续改进: 聚合过大、缺少领域事件、跨聚合事务
完整反模式速查表见 references/checklist.md,含 Java 代码示例和修复路径。
12 DDD anti-patterns, classified as P0 (blocks merge), P1 (must be fixed before the next release), and P2 (continuous improvement).
- P0 — Must Fix: Anemic model, God Service, direct cross-aggregate references, framework dependencies in the Domain layer, circular dependencies, Repository returning a non-aggregate root
- P1 — Should Fix: Business logic in Controller, SQL in Application Service, mutable value objects
- P2 — Continuous Improvement: Oversized aggregates, missing domain events, cross-aggregate transactions
See references/checklist.md for the complete anti-pattern quick-reference table, including Java code examples and remediation paths.
Layered Compliance Matrix
Layered Compliance Matrix
分层依赖检查规则(基于 ArchUnit):
┌─────────────────┬─────┬─────┬─────┬─────┐
│ 层 / 可依赖 │ Infra│ Dom │ App │ Adap│
├─────────────────┼─────┼─────┼─────┼─────┤
│ Infrastructure │ ✓ │ ✗ │ ✗ │ ✗ │
│ Domain │ ✗ │ ✓ │ ✗ │ ✗ │
│ Application │ ✓ │ ✓ │ ✓ │ ✗ │
│ Adapter │ ✓ │ ✓ │ ✓ │ ✓ │
└─────────────────┴─────┴─────┴─────┴─────┘Domain 层零依赖规则(P0):
- ✗
import org.springframework.stereotype.Service - ✗
import javax.persistence.Entity - ✗
import org.apache.ibatis.annotations.Mapper - ✓
import java.util.Optional - ✓
import java.math.BigDecimal
ArchUnit 完整配置见 references/archunit-config.md。
Layer dependency validation rules based on ArchUnit:
┌─────────────────┬─────┬─────┬─────┬─────┐
│ 层 / 可依赖 │ Infra│ Dom │ App │ Adap│
├─────────────────┼─────┼─────┼─────┼─────┤
│ Infrastructure │ ✓ │ ✗ │ ✗ │ ✗ │
│ Domain │ ✗ │ ✓ │ ✗ │ ✗ │
│ Application │ ✓ │ ✓ │ ✓ │ ✗ │
│ Adapter │ ✓ │ ✓ │ ✓ │ ✓ │
└─────────────────┴─────┴─────┴─────┴─────┘Domain-layer zero-dependency rule (P0):
- ✗
import org.springframework.stereotype.Service - ✗
import javax.persistence.Entity - ✗
import org.apache.ibatis.annotations.Mapper - ✓
import java.util.Optional - ✓
import java.math.BigDecimal
See references/archunit-config.md for the complete ArchUnit configuration.
Rich Domain Model Validation
Rich Domain Model Validation
Rich models encapsulate behavior in entities (pass); anemic models expose state via getters/setters (fail). Key: behavior in Entity vs behavior in Service.
完整代码示例和重构对比见 examples/rich-model-refactoring.md,含 3 个实战案例。
Rich models encapsulate behavior in entities (pass), while anemic models expose state through getters/setters (fail). Key: behavior in the Entity versus behavior in the Service.
See examples/rich-model-refactoring.md for complete code examples and refactoring comparisons, including three real-world cases.
Scoring System
Scoring System
5 维度评分
Five-Dimension Scoring
| 维度 | 权重 | 检查项 | 满分 |
|---|---|---|---|
| 1. 分层合规 | 30% | Domain 零依赖、依赖方向、App 无 SQL、Controller 无业务逻辑 | 30 |
| 2. 领域模型质量 | 30% | 充血模型覆盖率、值对象使用率、聚合设计、领域事件 | 30 |
| 3. 命名规范 | 15% | 聚合根 = 业务名称、Repository = 标准命名、事件 = 过去式 | 15 |
| 4. 代码结构 | 15% | 包按聚合组织、类大小、圈复杂度 | 15 |
| 5. 测试覆盖 | 10% | Domain 层测试覆盖、聚合根行为测试、事件验证 | 10 |
总分 = Σ(维度得分 × 权重)
| Dimension | Weight | Checks | Max Score |
|---|---|---|---|
| 1. Layered Compliance | 30% | Domain has zero dependencies, correct dependency direction, no SQL in App, no business logic in Controller | 30 |
| 2. Domain Model Quality | 30% | Rich-model coverage, value object usage, aggregate design, domain events | 30 |
| 3. Naming Conventions | 15% | Aggregate root = business name, Repository = standard naming, event = past tense | 15 |
| 4. Code Structure | 15% | Packages organized by aggregate, class size, cyclomatic complexity | 15 |
| 5. Test Coverage | 10% | Domain-layer test coverage, aggregate root behavior tests, event verification | 10 |
Total Score = Σ(dimension score × weight)
分数等级
Score Levels
| 范围 | 等级 | 含义 | 行动 |
|---|---|---|---|
| ≥ 85 | 🟢 A | 优秀 DDD 实践 | 可上生产 |
| 70-84 | 🟡 B | 基本合规,有改进空间 | 修 P1 问题 |
| 50-69 | 🟠 C | 存在明显反模式 | 规划重构 Sprint |
| < 50 | 🔴 D | 需要大面积重构 | 阻塞合并,必须先重构 |
评分细则和计算示例见 references/scoring-criteria.md、examples/scoring-example.md。
| Range | Grade | Meaning | Action |
|---|---|---|---|
| ≥ 85 | 🟢 A | Excellent DDD practices | Ready for production |
| 70–84 | 🟡 B | Basically compliant, with room for improvement | Fix P1 issues |
| 50–69 | 🟠 C | Obvious anti-patterns are present | Plan a refactoring Sprint |
| < 50 | 🔴 D | Substantial refactoring is required | Block the merge; refactor first |
See references/scoring-criteria.md and examples/scoring-example.md for detailed scoring criteria and calculation examples.
Review Report Template
Review Report Template
每次审查输出结构化报告,包含:
markdown
undefinedEvery review must output a structured report containing:
markdown
undefinedDDD Code Review Report
DDD Code Review Report
Overall Score: 78/100 (🟡 B)
Overall Score: 78/100 (🟡 B)
分层合规 (24/30)
分层合规 (24/30)
| 检查项 | 结果 | 说明 |
|---|---|---|
| Domain 零依赖 | ✅ | 无框架依赖 |
| App 层无 SQL | ❌ | OrderAppService L45 直接调用 Mapper |
| 依赖方向 | ✅ | 无反向依赖 |
| 检查项 | 结果 | 说明 |
|---|---|---|
| Domain 零依赖 | ✅ | 无框架依赖 |
| App 层无 SQL | ❌ | OrderAppService L45 直接调用 Mapper |
| 依赖方向 | ✅ | 无反向依赖 |
领域模型质量 (22/30)
领域模型质量 (22/30)
| 检查项 | 结果 | 说明 |
|---|---|---|
| 充血模型 | ⚠️ | User 实体仍为贫血模型 |
| 值对象 | ⚠️ | Money/Email 已用,Phone 仍为 String |
| 检查项 | 结果 | 说明 |
|---|---|---|
| 充血模型 | ⚠️ | User 实体仍为贫血模型 |
| 值对象 | ⚠️ | Money/Email 已用,Phone 仍为 String |
反模式清单
反模式清单
| 严重级别 | 反模式 | 位置 | 修复建议 |
|---|---|---|---|
| P0 | 上帝 Service | OrderService.java:45-320 | 拆分为 OrderPricingService + OrderFulfillmentService |
| 严重级别 | 反模式 | 位置 | 修复建议 |
|---|---|---|---|
| P0 | 上帝 Service | OrderService.java:45-320 | 拆分为 OrderPricingService + OrderFulfillmentService |
改进建议(按优先级)
改进建议(按优先级)
- [P0] 将 OrderAppService 中的 SQL 移到 Repository 实现
- [P1] 将 User 实体改为充血模型
- [P2] 为关键业务操作补充领域事件
完整模板见 [references/report-template.md](references/08-report-template.md)、示例见 [examples/review-report-example.md](examples/03-review-report-example.md)。- [P0] 将 OrderAppService 中的 SQL 移到 Repository 实现
- [P1] 将 User 实体改为充血模型
- [P2] 为关键业务操作补充领域事件
See [references/report-template.md](references/08-report-template.md) for the complete template and [examples/review-report-example.md](examples/03-review-report-example.md) for an example.Gotchas
Gotchas
常见审查陷阱见 references/gotchas.md。
See references/gotchas.md for common review pitfalls.
FAQ
FAQ
| 问题 | 回答 |
|---|---|
| 一次审查要多久? | 小项目(1-2 聚合)约 30-60 分钟;大项目按模块分次审查 |
| 评分是主观的吗? | 每个检查项有明确检测标准(见 references/scoring-criteria.md),可重复、可验证 |
| ArchUnit 检查必须自己写吗? | 直接使用 references/archunit-config.md 中的配置,复制到项目即可 |
| 找到了反模式,改不动怎么办? | 以 P0 优先修复。P1/P2 记入技术债务,制定偿还计划 |
| 审查频率建议? | 代码审查推荐 PR 合并前做。架构级审查推荐每季度一次 |
| 与非 DDD 架构的审查区别? | DDD 审查关注领域模型健康度,非 DDD 审查关注代码规范/性能/安全 |
| Question | Answer |
|---|---|
| How long does a review take? | Small projects (1–2 aggregates) take approximately 30–60 minutes; large projects should be reviewed module by module |
| Is the score subjective? | Every check has explicit detection criteria (see references/scoring-criteria.md), making the results repeatable and verifiable |
| Do I need to write the ArchUnit checks myself? | Directly use the configuration in references/archunit-config.md and copy it into the project |
| What if anti-patterns are found but the code cannot be changed? | Prioritize P0 fixes. Record P1/P2 issues as technical debt and establish a remediation plan |
| What is the recommended review frequency? | Perform code reviews before merging a PR. Perform architecture-level reviews quarterly |
| How does this differ from reviewing a non-DDD architecture? | DDD reviews focus on domain model health, while non-DDD reviews focus on code standards, performance, and security |
Security & Safety
Security & Safety
This skill is pure documentation. It contains no executable scripts, collects no user data, accesses no external services or networks.
This skill is pure documentation. It contains no executable scripts, collects no user data, and accesses no external services or networks.
Keywords
Keywords
DDD 代码审查、反模式检测、贫血模型、充血模型、上帝 Service、分层合规、依赖方向、ArchUnit、领域事件、值对象不可变、聚合设计、5 维度评分、代码质量门禁
DDD code review, anti-pattern detection, anemic model, rich model, God Service, layered compliance, dependency direction, ArchUnit, domain events, immutable value objects, aggregate design, five-dimension scoring, code quality gate
References
References
| 文件 | 用途 |
|---|---|
| references/checklist.md | 反模式速查表 — P0/P1/P2 分级 + 修复路径 |
| references/scoring-criteria.md | 5 维度评分细则 — 每项检查的权重和检测方法 |
| references/archunit-config.md | ArchUnit 完整配置 — Maven/Gradle 依赖 + 全套检查规则 |
| references/report-template.md | 审查报告模板 — Markdown + 快速摘要格式 |
| references/clean-ddd-hexagonal-layers.md | 四层架构结构详解 — Domain/App/Infra/Presentation |
| references/clean-ddd-hexagonal-tactical.md | DDD 战术模式参考 — Entity/VO/Aggregate/Repository |
| references/clean-ddd-hexagonal-testing.md | 测试模式 — 单元测试/集成测试/架构测试 |
| references/partme-15-boundaries.md | 微服务边界理论 — 逻辑边界/物理边界/代码边界 |
| references/gotchas.md | 审查常见陷阱 — 跨聚合引用、PO/DTO 混用等 |
| File | Purpose |
|---|---|
| references/checklist.md | Anti-pattern quick-reference table — P0/P1/P2 classification + remediation paths |
| references/scoring-criteria.md | Five-dimension scoring criteria — weights and detection methods for each check |
| references/archunit-config.md | Complete ArchUnit configuration — Maven/Gradle dependencies + all validation rules |
| references/report-template.md | Review report template — Markdown + quick-summary format |
| references/clean-ddd-hexagonal-layers.md | Detailed four-layer architecture structure — Domain/App/Infra/Presentation |
| references/clean-ddd-hexagonal-tactical.md | DDD tactical pattern reference — Entity/VO/Aggregate/Repository |
| references/clean-ddd-hexagonal-testing.md | Testing patterns — unit tests/integration tests/architecture tests |
| references/partme-15-boundaries.md | Microservice boundary theory — logical/physical/code boundaries |
| references/gotchas.md | Common review pitfalls — cross-aggregate references, PO/DTO mixing, etc. |
Examples
Examples
| 文件 | 用途 |
|---|---|
| examples/review-report-example.md | 完整审查报告示例(62/100 🟠 C 级) |
| examples/scoring-example.md | 评分计算全过程 + 修复 ROI 分析 |
| examples/rich-model-refactoring.md | 贫血→充血模型重构:3 个实战案例 |
| examples/anti-pattern-fix-guide.md | 反模式修复路径速查 |
| examples/archunit-compliance-test.md | ArchUnit 合规测试 — P0/P1/P2 门禁实现 |
| File | Purpose |
|---|---|
| examples/review-report-example.md | Complete review report example (62/100 🟠 C) |
| examples/scoring-example.md | End-to-end score calculation + remediation ROI analysis |
| examples/rich-model-refactoring.md | Anemic → rich model refactoring: three real-world cases |
| examples/anti-pattern-fix-guide.md | Anti-pattern remediation quick-reference |
| examples/archunit-compliance-test.md | ArchUnit compliance tests — P0/P1/P2 gate implementation |
Next Steps
Next Steps
审查完成后,根据结果进入后续步骤:
- 架构级别评估 →
ddd-architecture-evaluator - 修复架构目录结构 → 对应架构 Skill(/
ddd-architecture-layered/ddd-architecture-hexagonal等)ddd-architecture-cola - 提升测试覆盖率 →
ddd-testing-strategist - 输出架构文档 →
ddd-architecture-doc
After the review is complete, proceed to the appropriate next step based on the results:
- Architecture-level evaluation →
ddd-architecture-evaluator - Fix the architecture directory structure → the corresponding architecture Skill (/
ddd-architecture-layered/ddd-architecture-hexagonal, etc.)ddd-architecture-cola - Increase test coverage →
ddd-testing-strategist - Produce architecture documentation →
ddd-architecture-doc