Tactical Ddd

作者 tech-leads-club120b67676388無授權條款收錄於 2026年10月8日更新於 2026年10月8日

Detects anemic domain models, validates and refactors them into rich domain models, and enforces tactical DDD patterns (Entities, Value Objects, Aggregates, Domain Services, Domain Events). Use when the user asks to validate, review, or check domain models or DDD code; detect anemia; refactor domain objects; improve encapsulation; or mentions terms like "anemic model", "rich domain", "aggregate", "value object", "domain event", "ubiquitous language", "is this good DDD", "does this follow DDD", or "check my domain". Do NOT use for module or service boundary design, architectural decomposition, strategic DDD context mapping, or code outside the domain layer (DTOs, controllers, infrastructure adapters).

AI 產生的概覽

偵測貧血領域模型,評估正確的 DDD 建構塊,並將程式碼重構為豐富領域模型。

功能
掃描領域層程式碼中的貧血訊號,例如公開 setter、setter 鏈,以及原本應屬於實體的服務邏輯,並給出嚴重程度評分與受影響類別清單。它會把每個類別對應到合適的戰術 DDD 建構塊(實體、值物件、聚合、領域服務),並在要求修復時依序套用重構,輸出修改前後的差異。它也會依據參考資料回答戰術模式的設計問題。
適用情境
適用於審查或驗證領域模型、檢查程式碼是否符合 DDD、偵測貧血模型,或重構領域物件以改善封裝。不適用於模組或服務邊界設計、策略情境對應,以及領域層以外的程式碼。
執行需求
僅為說明文件,不含指令碼。代理需要存取目標程式碼庫,以及隨附的偵測、重構與參考文件。

Tactical DDD — Rich Domain Modeling

Workflow

Determine the user's intent first:

IntentPhases to run
"validate / review / check / is this correct?"Phase 1 + 2 only → report findings, ask before refactoring
"fix / refactor / improve / clean up"Phase 1 + 2 + 3
"how should I design / model this?"Load reference.md [blocked] directly

Phase 1 — Detect

Load detection.md [blocked] and scan the target code for anemia signals. Produce a severity score and list of affected classes.

Phase 2 — Assess

For each affected class, determine the correct building block:

Has unique identity tracked over time?Has invariants tying multiple objects?→ Building Block
Yes—Entity
No—Value Object
Yes (root) + children with shared invariantsYesAggregate
Operation spans multiple Aggregates/doesn't belong to any—Domain Service

Prefer Value Objects over Entities. Prefer small Aggregates over large ones.

If intent was validate/review: stop here. Report findings using the output format below. Ask "Would you like me to apply these fixes?" before proceeding.

Phase 3 — Refactor

Load refactoring.md [blocked] for step-by-step moves. Apply in this order:

  1. Replace setter chains with a single expressive method
  2. Move service logic into the Aggregate that owns it
  3. Add business guards at the top of each method
  4. Publish a Domain Event after each successful state change
  5. Replace primitive types with Value Objects

For deep pattern questions (boundary design, event modeling, service vs. entity decision), load reference.md [blocked].


Quick Anemia Signals (scan first)

public setX() / public setY()        → behaviour should be encapsulatedservice.doX(entity, ...)              → logic likely belongs in entityentity.setA(); entity.setB(); ...     → setter chain = missing intent methodno domain methods beyond getters      → pure data bag

Golden Rules

  1. Behaviour with data — Objects own both state and the operations that change it
  2. Ubiquitous Language — Method names come from the domain, not CRUD (commitTo, not setStatus)
  3. Small Aggregates — Root + Value Objects by default; add child Entities only for true invariants
  4. One transaction = one Aggregate — Cross-Aggregate rules use eventual consistency via Domain Events
  5. Reference by ID — Never hold object references to other Aggregates
  6. Value Objects first — Use Entities only when individual identity is essential
  7. Domain Services sparingly — Excessive services → anemic model
  8. Protect invariants — The Aggregate is the last line of defence; never trust the caller

Output Format

When reviewing code, report:

## Anemia Diagnosis: <ClassName>
Severity: [None | Mild | Moderate | Severe]
Issues:- <description of problem>
Recommended refactoring:- <specific move from refactoring.md>

When refactoring, show a before/after diff for each class touched.

來源與署名

來源:tech-leads-club/agent-skills位於packages/skills-catalog/skills/(architecture)/tactical-ddd提交120b676

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架