aggregate-design
Design DDD tactical aggregates for a bounded context — aggregate roots, entities vs value objects, invariants, transaction/consistency boundaries, reference-by-id.
- License 许可
- MIT
- 最近更新
- 2026-10-29
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- aggregate-design
- description
- Design DDD tactical aggregates for a bounded context — aggregate roots, entities vs value objects, invariants, transaction/consistency boundaries, reference-by-id.
02 资源文件 1
- 参考文档routing.yaml495 B
03 SKILL.md 正文
aggregate-design
An aggregate is a consistency boundary, not a data model. Get the invariants right and the boundaries fall out; get them wrong and you fight the database forever.
Mode: analytical — judgment inside a fixed frame. Keep the output shape; every verdict cites its evidence; unknowns are marked [TBD] or [VERIFY], never filled in.
When to use
- You have a bounded context (see
bounded-context-map) and need its internal object model. - Transactions are touching too many rows, or two requests keep corrupting the same invariant.
- A workflow invokes
polymath-architecture:aggregate-design.
Not this when: you are choosing cross-node consistency (eventual vs strong across services) — that is distributed-consistency. For the persistence DDL, hand off to polymath-backend:db-schema.
Inputs
- The bounded context and its core business rules ("a refund can't exceed the captured amount").
- Known concurrency hotspots.
Procedure
- List the true invariants — rules that must hold at every commit. Each invariant must live inside exactly one aggregate; that is what defines the boundary.
- Classify each concept: entity (has identity + lifecycle), value object (defined wholly by its attributes, immutable — prefer these), domain event (something that happened).
- Pick the aggregate root — the single entity that guards the invariant and is the only entry point. Outside code references the root, never its internals.
- Set the boundary: include only what must be transactionally consistent. Keep aggregates small — a large aggregate serializes unrelated writes.
- Reference other aggregates by id, not by object. Crossing an aggregate boundary in one transaction is the most common design smell — flag it.
- Decide cross-aggregate consistency: between aggregates use eventual consistency via domain events, not a bigger transaction. Name the event and who reacts.
- Check concurrency: each aggregate gets an optimistic-concurrency version. Two writers to one aggregate is a real conflict; to two aggregates is not.
Output
Aggregate model: Refunds context
Refund (root) invariant: Σ refunds ≤ captured amount of the payment
RefundLine entity (identity: line id; lifecycle: requested → issued → settled)
Money value object (amount, currency) — immutable
references Payment by id (PaymentId), never by object
version: optimistic concurrency column `version`
Payment (root, Payments context) invariant: captured ≤ authorized
emits PaymentCaptured → Refunds caches capturedAmount per PaymentId
Domain events
RefundIssued(refundId, paymentId, amount) → Ledger posts the reversal (eventual)
Invariants spanning two aggregates (rethink):
- "a refund cannot exceed the *remaining* capture" needs Payment + all Refunds → keep a
RefundAllowance on Refund, fed by PaymentCaptured / RefundIssued, not a two-aggregate transaction.
Inline by default; when a workflow step or the user wants it kept, write it to docs/architecture/<slug>-aggregates.md (slug from the system or context name, lowercased, non-a-z0-9 runs → -).
Quality bar
- Every invariant is owned by exactly one aggregate.
- No transaction modifies more than one aggregate; cross-aggregate effects are domain events.
- Value objects are preferred over entities wherever identity does not matter.
- Aggregates are small — a god-aggregate that owns half the context is the failure mode.
04 同仓库更多 Skills
runbook
Write or refresh an operational runbook for an alert/symptom; structured as TL;DR + Pre-checks + Steps + Verification + Rollback + Escalation.
rfc
Draft a lightweight design RFC to docs/rfcs/<slug>.md; Summary, Motivation, Detailed design, Drawbacks, Alternatives, Unresolved questions. A proposal to debate, not a recorded decision (adr).
editorial-pass
Review and tighten prose in a Markdown artifact for a target tone (terse, formal, friendly) without dropping information; preserve quotations and load-bearing constraints.
architecture-review-facilitation
Run an architecture review as a process — entry criteria, reviewer checklist, decision log, and follow-up actions — pairing the design artifact with a red-team pass.
architecture-doc
Write a C4-style architecture document; System Context + Containers (+ Components when interesting) with mermaid diagrams. Describes structure, not a decision (adr) or proposal (rfc).
adr
Write an Architecture Decision Record (Michael Nygard format) to docs/adrs/NNNN-<slug>.md; frontmatter validated by the ADR artifact schema. Records a made decision, not a proposal to debate (rfc).