返回提示词目录第 1314 页

设计领域聚合与一致性边界

根据业务不变量划分聚合边界,输出领域模型代码、跨聚合事件及持久化并发方案。

完整提示词

<context>
An aggregate is a consistency boundary: the cluster of objects that must change together in one transaction to keep a business rule true, accessed only through its root. The most common mistake is making aggregates too large (an Order that contains the Customer, Products and Inventory), which causes lock contention and loading huge graphs; the second is making them anaemic, with all rules in service classes and public setters on every field. Good aggregates are as small as the true invariants allow, reference other aggregates by id rather than object, expose behaviour ("add line", "submit") instead of setters, and enforce their invariants in those methods. Rules spanning aggregates ("reserve stock when an order is submitted") are usually eventually consistent, connected by domain events, unless the business genuinely cannot tolerate the gap. Value objects (Money, Address, DateRange) are immutable, validated on creation, and remove primitive obsession. One repository per aggregate root loads and saves the whole aggregate, with optimistic concurrency via a version field. The right boundaries come from asking domain experts which rules must hold immediately and what happens if two people act at once.
</context>

<task>
<domain>
{{domain}}
</domain>
{{#language}}
Language and persistence: {{language}}
{{/language}}
{{#current_model}}
<current_model>
{{current_model}}
</current_model>
{{/current_model}}

1. If a repository is available, read the existing entities, services and tables for this context before proposing anything, and keep names that match the business language.
2. List the invariants: rules that must be true after every transaction. For each, decide whether it truly needs immediate consistency or can tolerate a short delay; ask when the description does not say.
3. Group the objects that share an immediate invariant into aggregates and pick each root. Keep aggregates small; reference other aggregates by id.
4. Identify value objects and make them immutable with validation on creation.
5. Sketch the main aggregate in the given language with behaviour methods that enforce invariants, no public setters for invariant-bearing state, and domain events raised on meaningful changes.
6. Define cross-aggregate rules as domain events and handlers, and say where a transactional outbox is needed to publish them reliably.
7. Define one repository per root, the concurrency control (version field), and how the persistence mapping avoids leaking into the domain model where the language and ORM allow.
</task>

<constraints>
- Do not put rules in service classes that belong in the aggregate.
- Do not create one large aggregate to avoid eventual consistency without naming the contention cost.
- Code must be idiomatic for the given language; do not invent ORM features.
- Domain rules come from the user; mark any rule you infer and ask domain experts to confirm it.
{{> output/uncertainty}}
</constraints>

<output_format>
## Aggregates
Table: aggregate, root, entities and value objects inside, invariants it protects.
## Model sketch
Code in the given language for the root aggregate: fields, constructor or factory, the main behaviour methods, invariant checks.
## Consistency rules
What is consistent in one transaction and what is eventually consistent, with the domain events that connect them.
## References and size
How aggregates refer to each other (by id) and why each aggregate is this size.
## Repositories and persistence
Repository interfaces and concurrency control.
## Open questions for domain experts
Bullets.
</output_format>

可替换内容

  • {{domain}}
  • {{#language}}
  • {{language}}
  • {{/language}}
  • {{#current_model}}
  • {{current_model}}
  • {{/current_model}}
  • {{> output/uncertainty}}

分类与标签

编程开发、领域驱动设计、聚合建模、事务一致性、领域事件、仓储设计

前往 AI 图片创作