AgentSkillsCN

domain-modeling

运用 DDD 设计模式对业务领域进行建模:实体、值对象、聚合根、领域事件。在实现业务逻辑、定义领域概念,或划定聚合边界时,可灵活选用这些模式。

SKILL.md
--- frontmatter
name: domain-modeling
description: Model business domains using DDD patterns: Entity, Value Object, Aggregate, Domain Event. Use when implementing business logic, defining domain concepts, or designing aggregate boundaries.

Applicability Rubric

ConditionPassFail
Business logic involvedFeature contains business rulesPure technical/UI change
Domain entity workWorking with entities/aggregatesInfrastructure-only change
New business conceptDefining new domain termsUsing existing concepts
Complex process modelingMulti-step business workflowSimple CRUD operation

Apply when: Any condition passes

Core Principles

Building Blocks

PatternPurposeCharacteristics
EntityObject with identityHas unique ID, lifecycle, mutable
Value ObjectImmutable dataNo ID, compared by value, immutable
AggregateConsistency boundaryRoot entity + related objects
Domain ServiceStateless operationsLogic that doesn't belong to entities
Domain EventRecord of something happenedImmutable, past tense naming

Pattern Selection Guide

QuestionEntityValue ObjectAggregate
Has unique identity?YesNoRoot has
Identity across changes?PersistsN/ARoot persists
How compared?By IDBy valueBy root ID
MutabilityMutableImmutableControlled
Consistency boundary?NoNoYes

Completion Rubric

Entity Design

CriterionPassFail
Unique identifierHas immutable IDNo identifier or mutable ID
Identity immutabilityID unchanged after creationID can be modified
Business rule encapsulationRules inside entityLogic scattered outside
Invariant validationValidates on state changesNo validation

Value Object Design

CriterionPassFail
ImmutabilityCannot be modified after creationHas setters or mutable state
Value equalityCompared by all attributesCompared by reference
Self-validationValidates on constructionAccepts invalid state
Side-effect freeNo external state changesHas side effects

Aggregate Design

CriterionPassFail
Clear rootAggregate root identifiedNo clear entry point
Access controlExternal access through root onlyDirect access to internals
Invariant enforcementConsistency rules enforcedInvariants can be violated
Size appropriatenessSmall and focusedToo large or unfocused

Domain Event Design

CriterionPassFail
Past tense namingNamed like OrderPlacedPresent/future tense
Complete dataContains all relevant infoMissing important data
ImmutabilityCannot be changedMutable fields
TimestampedHas occurrence timeNo timestamp

Bounded Context

When modeling, consider:

  • Context boundaries: Where does this model apply?
  • Ubiquitous language: Use domain expert terminology
  • Context mapping: How does this context relate to others?
code
┌──────────────────┐     ┌──────────────────┐
│   Sales Context  │     │  Shipping Context │
│                  │     │                   │
│  Order (Entity)  │────▶│  Shipment (Entity)│
│  Customer (VO)   │     │  Address (VO)     │
└──────────────────┘     └──────────────────┘