ddd-code-reviewer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

DDD 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 compliance
rich domain check
review my DDD code
代码审查
DDD review
反模式检测
anti-pattern
贫血模型
充血模型检查
架构审查
分层合规
代码质量评分
layering compliance
rich domain check
review my DDD code

适用前提

Prerequisites

条件说明
项目已采用 DDD 思想非 DDD 项目无审查基础
有可审查的源代码需提供代码路径或片段
期望发现反模式不只是格式检查,更关注领域模型健康度
ConditionDescription
The project has adopted DDD conceptsA non-DDD project has no foundation for this review
Reviewable source code is availableA code path or snippet must be provided
The goal is to identify anti-patternsThe review goes beyond formatting checks and focuses on domain model health

不适用场景

When Not to Use

跳过改用
非 DDD 项目(纯 CRUD)标准代码审查(SonarQube / Checkstyle)
项目尚未引入 DDD
ddd-architecture-awesome
(先学习 DDD 概念)
刚写完第一个 DDD 代码先写完再审查
需要架构选型
ddd-architecture-selector
需要架构健康度评估
ddd-architecture-evaluator
SkipUse Instead
Non-DDD project (pure CRUD)Standard code review (SonarQube / Checkstyle)
The project has not yet adopted DDD
ddd-architecture-awesome
(learn DDD concepts first)
The first DDD implementation has just been writtenFinish writing it before the review
Architecture selection is needed
ddd-architecture-selector
An architecture health assessment is needed
ddd-architecture-evaluator

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

  1. Every code review must include a 5-dimension scoring report.
  2. P0 anti-patterns (anemia model, layer violation, cross-aggregate ref) block merge.
  3. Domain layer must have zero framework dependencies — verified via ArchUnit.
  4. Review reports must include fix suggestions ordered by priority (P0→P1→P2).
  1. Every code review must include a five-dimension scoring report.
  2. P0 anti-patterns (anemic model, layer violation, cross-aggregate reference) block a merge.
  3. The Domain layer must have zero framework dependencies—verified via ArchUnit.
  4. 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
总分 = Σ(维度得分 × 权重)
DimensionWeightChecksMax Score
1. Layered Compliance30%Domain has zero dependencies, correct dependency direction, no SQL in App, no business logic in Controller30
2. Domain Model Quality30%Rich-model coverage, value object usage, aggregate design, domain events30
3. Naming Conventions15%Aggregate root = business name, Repository = standard naming, event = past tense15
4. Code Structure15%Packages organized by aggregate, class size, cyclomatic complexity15
5. Test Coverage10%Domain-layer test coverage, aggregate root behavior tests, event verification10
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。
RangeGradeMeaningAction
≥ 85🟢 AExcellent DDD practicesReady for production
70–84🟡 BBasically compliant, with room for improvementFix P1 issues
50–69🟠 CObvious anti-patterns are presentPlan a refactoring Sprint
< 50🔴 DSubstantial refactoring is requiredBlock 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
undefined
Every review must output a structured report containing:
markdown
undefined

DDD 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上帝 ServiceOrderService.java:45-320拆分为 OrderPricingService + OrderFulfillmentService
严重级别反模式位置修复建议
P0上帝 ServiceOrderService.java:45-320拆分为 OrderPricingService + OrderFulfillmentService

改进建议(按优先级)

改进建议(按优先级)

  1. [P0] 将 OrderAppService 中的 SQL 移到 Repository 实现
  2. [P1] 将 User 实体改为充血模型
  3. [P2] 为关键业务操作补充领域事件

完整模板见 [references/report-template.md](references/08-report-template.md)、示例见 [examples/review-report-example.md](examples/03-review-report-example.md)。
  1. [P0] 将 OrderAppService 中的 SQL 移到 Repository 实现
  2. [P1] 将 User 实体改为充血模型
  3. [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 审查关注代码规范/性能/安全
QuestionAnswer
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.md5 维度评分细则 — 每项检查的权重和检测方法
references/archunit-config.mdArchUnit 完整配置 — Maven/Gradle 依赖 + 全套检查规则
references/report-template.md审查报告模板 — Markdown + 快速摘要格式
references/clean-ddd-hexagonal-layers.md四层架构结构详解 — Domain/App/Infra/Presentation
references/clean-ddd-hexagonal-tactical.mdDDD 战术模式参考 — Entity/VO/Aggregate/Repository
references/clean-ddd-hexagonal-testing.md测试模式 — 单元测试/集成测试/架构测试
references/partme-15-boundaries.md微服务边界理论 — 逻辑边界/物理边界/代码边界
references/gotchas.md审查常见陷阱 — 跨聚合引用、PO/DTO 混用等
FilePurpose
references/checklist.mdAnti-pattern quick-reference table — P0/P1/P2 classification + remediation paths
references/scoring-criteria.mdFive-dimension scoring criteria — weights and detection methods for each check
references/archunit-config.mdComplete ArchUnit configuration — Maven/Gradle dependencies + all validation rules
references/report-template.mdReview report template — Markdown + quick-summary format
references/clean-ddd-hexagonal-layers.mdDetailed four-layer architecture structure — Domain/App/Infra/Presentation
references/clean-ddd-hexagonal-tactical.mdDDD tactical pattern reference — Entity/VO/Aggregate/Repository
references/clean-ddd-hexagonal-testing.mdTesting patterns — unit tests/integration tests/architecture tests
references/partme-15-boundaries.mdMicroservice boundary theory — logical/physical/code boundaries
references/gotchas.mdCommon 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.mdArchUnit 合规测试 — P0/P1/P2 门禁实现
FilePurpose
examples/review-report-example.mdComplete review report example (62/100 🟠 C)
examples/scoring-example.mdEnd-to-end score calculation + remediation ROI analysis
examples/rich-model-refactoring.mdAnemic → rich model refactoring: three real-world cases
examples/anti-pattern-fix-guide.mdAnti-pattern remediation quick-reference
examples/archunit-compliance-test.mdArchUnit 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
    /
    ddd-architecture-cola
    , etc.)
  • Increase test coverage →
    ddd-testing-strategist
  • Produce architecture documentation →
    ddd-architecture-doc