AgentSkillsCN

m09-domain

精通 C++ 领域建模(DDD)。触发点:实体、值对象、聚合根、仓储、Pimpl 模式、类设计、不变量。

SKILL.md
--- frontmatter
name: m09-domain
description: "Mastering C++ Domain Modeling (DDD). Triggers: Entity, Value Object, Aggregate, Repository, Pimpl, Class Design, Invariant."

C++ Domain Modeling

Core Question

Identity or Value?

  • Value Object: Defined by attributes (Color, Money). Equality via comparison. Copyable.
  • Entity: Defined by Identity (User, Socket). Equality via ID. Non-copyable (usually).

Error → Design Question

IssueDesign Question
Data InconsistencyAre public fields allowing invalid states?
Object SlicingAre you passing polymorphic Entities by Value?
Header HellAre you leaking implementation details? (Use Pimpl).

Thinking Prompt

  1. Is it copyable?

    • Yes? → Value Type (Rule of Zero, defaults).
    • No? → Entity (Delete copy ctor, enable move).
  2. Does it have invariants?

    • Yes? → class with private data + public methods.
    • No? → struct (POD).
  3. Does it own others?

    • Aggregate Root? → Owns children via std::vector / unique_ptr.

Trace Up / Down

  • Trace Down:
    • Intent: "User has a Name and an Address."
    • Code: class User (Entity) holds Name (Value) and Address (Value).

Quick Reference

PatternC++ Implementation
Value Objectstruct + operator<=>.
Entityclass + Deleted Copy + ID field.
RepositoryPure Virtual Interface (virtual ... = 0).
AggregateParent class owning children.