跳到主要内容
哒哒星球DADA PLANET

Skill · 已公开版本

ddd-tactical-patterns · DDD战术模式编码指南

指导用实体、值对象、聚合、仓储与领域事件落地DDD战术模式,强调不变量优先与聚合边界。

先看看示例

作者提供的示例,实际效果取决于你的输入和使用工具。

输入

把贫血订单模型重构为带提交不变量的聚合,并说明领域事件命名。

输出

预期交付:聚合与值对象设计建议、仓储与领域事件规则、代码示例。(编辑说明,非实际运行结果)

方法说明

---
name: ddd-tactical-patterns
description: "Apply DDD tactical patterns in code using entities, value objects, aggregates, repositories, and domain events with explicit invariants."
risk: safe
source: self
tags: "[ddd, tactical, aggregates, value-objects, domain-events]"
date_added: "2026-02-27"
---

# DDD Tactical Patterns

## Use this skill when

- Translating domain rules into code structures.
- Designing aggregate boundaries and invariants.
- Refactoring an anemic model into behavior-rich domain objects.
- Defining repository contracts and domain event boundaries.

## Do not use this skill when

- You are still defining strategic boundaries.
- The task is only API documentation or UI layout.
- Full DDD complexity is not justified.

## Instructions

1. Identify invariants first and design aggregates around them.
2. Model immutable value objects for validated concepts.
3. Keep domain behavior in domain objects, not controllers.
4. Emit domain events for meaningful state transitions.
5. Keep repositories at aggregate root boundaries.

If detailed checklists are needed, open `references/tactical-checklist.md`.

## Example

```typescript
class Order {
  private status: "draft" | "submitted" = "draft";

  submit(itemsCount: number): void {
    if (!Number.isInteger(itemsCount) || itemsCount <= 0) throw new Error("Order cannot be submitted empty");
    if (this.status !== "draft") throw new Error("Order already submitted");
    this.status = "submitted";
  }
}
```

## Limitations

- This skill does not define deployment architecture.
- It does not choose databases or transport protocols.
- It should be paired with testing patterns for invariant coverage.