DDD Code Reviewer
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
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
Trigger Terms (Use Immediately When Mentioned)
Prerequisites
| 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
| Skip | Use Instead |
|---|
| Non-DDD project (pure CRUD) | Standard code review (SonarQube / Checkstyle) |
| The project has not yet adopted DDD | (learn DDD concepts first) |
| The first DDD implementation has just been written | Finish writing it before the review |
| Architecture selection is needed | ddd-architecture-selector
|
| An architecture health assessment is needed | ddd-architecture-evaluator
|
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).
Rules
- 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
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
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 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
Five-Dimension Scoring
| 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
| 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
Every review must output a structured report containing:
markdown
# DDD Code Review Report
## Overall Score: 78/100 (🟡 B)
### 分层合规 (24/30)
|--------|:----:|------|
| Domain 零依赖 | ✅ | 无框架依赖 |
| App 层无 SQL | ❌ | OrderAppService L45 直接调用 Mapper |
| 依赖方向 | ✅ | 无反向依赖 |
### 领域模型质量 (22/30)
|--------|:----:|------|
| 充血模型 | ⚠️ | User 实体仍为贫血模型 |
| 值对象 | ⚠️ | Money/Email 已用,Phone 仍为 String |
### 反模式清单
|:------:|--------|------|---------|
| P0 | 上帝 Service | OrderService.java:45-320 | 拆分为 OrderPricingService + OrderFulfillmentService |
### 改进建议(按优先级)
1. [P0] 将 OrderAppService 中的 SQL 移到 Repository 实现
2. [P1] 将 User 实体改为充血模型
3. [P2] 为关键业务操作补充领域事件
See references/report-template.md for the complete template and examples/review-report-example.md for an example.
Gotchas
See references/gotchas.md for common review pitfalls.
FAQ
| 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
This skill is pure documentation. It contains no executable scripts, collects no user data, and accesses no external services or networks.
Keywords
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
| 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
| 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
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-hexagonal
/ , etc.)
- Increase test coverage →
- Produce architecture documentation →