opencode-metis 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +140 -0
- package/dist/cli.cjs +63 -0
- package/dist/mcp-server.cjs +51 -0
- package/dist/plugin.cjs +4 -0
- package/dist/worker.cjs +224 -0
- package/opencode/agent/the-analyst/feature-prioritization.md +66 -0
- package/opencode/agent/the-analyst/market-research.md +77 -0
- package/opencode/agent/the-analyst/project-coordination.md +81 -0
- package/opencode/agent/the-analyst/requirements-analysis.md +77 -0
- package/opencode/agent/the-architect/compatibility-review.md +138 -0
- package/opencode/agent/the-architect/complexity-review.md +137 -0
- package/opencode/agent/the-architect/quality-review.md +67 -0
- package/opencode/agent/the-architect/security-review.md +127 -0
- package/opencode/agent/the-architect/system-architecture.md +119 -0
- package/opencode/agent/the-architect/system-documentation.md +83 -0
- package/opencode/agent/the-architect/technology-research.md +85 -0
- package/opencode/agent/the-chief.md +79 -0
- package/opencode/agent/the-designer/accessibility-implementation.md +101 -0
- package/opencode/agent/the-designer/design-foundation.md +74 -0
- package/opencode/agent/the-designer/interaction-architecture.md +75 -0
- package/opencode/agent/the-designer/user-research.md +70 -0
- package/opencode/agent/the-meta-agent.md +155 -0
- package/opencode/agent/the-platform-engineer/ci-cd-pipelines.md +109 -0
- package/opencode/agent/the-platform-engineer/containerization.md +106 -0
- package/opencode/agent/the-platform-engineer/data-architecture.md +81 -0
- package/opencode/agent/the-platform-engineer/dependency-review.md +144 -0
- package/opencode/agent/the-platform-engineer/deployment-automation.md +81 -0
- package/opencode/agent/the-platform-engineer/infrastructure-as-code.md +107 -0
- package/opencode/agent/the-platform-engineer/performance-tuning.md +82 -0
- package/opencode/agent/the-platform-engineer/pipeline-engineering.md +81 -0
- package/opencode/agent/the-platform-engineer/production-monitoring.md +105 -0
- package/opencode/agent/the-qa-engineer/exploratory-testing.md +66 -0
- package/opencode/agent/the-qa-engineer/performance-testing.md +81 -0
- package/opencode/agent/the-qa-engineer/quality-assurance.md +77 -0
- package/opencode/agent/the-qa-engineer/test-execution.md +66 -0
- package/opencode/agent/the-software-engineer/api-development.md +78 -0
- package/opencode/agent/the-software-engineer/component-development.md +79 -0
- package/opencode/agent/the-software-engineer/concurrency-review.md +141 -0
- package/opencode/agent/the-software-engineer/domain-modeling.md +66 -0
- package/opencode/agent/the-software-engineer/performance-optimization.md +113 -0
- package/opencode/command/analyze.md +149 -0
- package/opencode/command/constitution.md +178 -0
- package/opencode/command/debug.md +194 -0
- package/opencode/command/document.md +178 -0
- package/opencode/command/implement.md +225 -0
- package/opencode/command/refactor.md +207 -0
- package/opencode/command/review.md +229 -0
- package/opencode/command/simplify.md +267 -0
- package/opencode/command/specify.md +191 -0
- package/opencode/command/validate.md +224 -0
- package/opencode/skill/accessibility-design/SKILL.md +566 -0
- package/opencode/skill/accessibility-design/checklists/wcag-checklist.md +435 -0
- package/opencode/skill/agent-coordination/SKILL.md +224 -0
- package/opencode/skill/api-contract-design/SKILL.md +550 -0
- package/opencode/skill/api-contract-design/templates/graphql-schema-template.md +818 -0
- package/opencode/skill/api-contract-design/templates/rest-api-template.md +417 -0
- package/opencode/skill/architecture-design/SKILL.md +160 -0
- package/opencode/skill/architecture-design/examples/architecture-examples.md +170 -0
- package/opencode/skill/architecture-design/template.md +749 -0
- package/opencode/skill/architecture-design/validation.md +99 -0
- package/opencode/skill/architecture-selection/SKILL.md +522 -0
- package/opencode/skill/architecture-selection/examples/adrs/001-example-adr.md +71 -0
- package/opencode/skill/architecture-selection/examples/architecture-patterns.md +239 -0
- package/opencode/skill/bug-diagnosis/SKILL.md +235 -0
- package/opencode/skill/code-quality-review/SKILL.md +337 -0
- package/opencode/skill/code-quality-review/examples/anti-patterns.md +629 -0
- package/opencode/skill/code-quality-review/reference.md +322 -0
- package/opencode/skill/code-review/SKILL.md +363 -0
- package/opencode/skill/code-review/reference.md +450 -0
- package/opencode/skill/codebase-analysis/SKILL.md +139 -0
- package/opencode/skill/codebase-navigation/SKILL.md +227 -0
- package/opencode/skill/codebase-navigation/examples/exploration-patterns.md +263 -0
- package/opencode/skill/coding-conventions/SKILL.md +178 -0
- package/opencode/skill/coding-conventions/checklists/accessibility-checklist.md +176 -0
- package/opencode/skill/coding-conventions/checklists/performance-checklist.md +154 -0
- package/opencode/skill/coding-conventions/checklists/security-checklist.md +127 -0
- package/opencode/skill/constitution-validation/SKILL.md +315 -0
- package/opencode/skill/constitution-validation/examples/CONSTITUTION.md +202 -0
- package/opencode/skill/constitution-validation/reference/rule-patterns.md +328 -0
- package/opencode/skill/constitution-validation/template.md +115 -0
- package/opencode/skill/context-preservation/SKILL.md +445 -0
- package/opencode/skill/data-modeling/SKILL.md +385 -0
- package/opencode/skill/data-modeling/templates/schema-design-template.md +268 -0
- package/opencode/skill/deployment-pipeline-design/SKILL.md +579 -0
- package/opencode/skill/deployment-pipeline-design/templates/pipeline-template.md +633 -0
- package/opencode/skill/documentation-extraction/SKILL.md +259 -0
- package/opencode/skill/documentation-sync/SKILL.md +431 -0
- package/opencode/skill/domain-driven-design/SKILL.md +509 -0
- package/opencode/skill/domain-driven-design/examples/ddd-patterns.md +688 -0
- package/opencode/skill/domain-driven-design/reference.md +465 -0
- package/opencode/skill/drift-detection/SKILL.md +383 -0
- package/opencode/skill/drift-detection/reference.md +340 -0
- package/opencode/skill/error-recovery/SKILL.md +162 -0
- package/opencode/skill/error-recovery/examples/error-patterns.md +484 -0
- package/opencode/skill/feature-prioritization/SKILL.md +419 -0
- package/opencode/skill/feature-prioritization/examples/rice-template.md +139 -0
- package/opencode/skill/feature-prioritization/reference.md +256 -0
- package/opencode/skill/git-workflow/SKILL.md +453 -0
- package/opencode/skill/implementation-planning/SKILL.md +215 -0
- package/opencode/skill/implementation-planning/examples/phase-examples.md +217 -0
- package/opencode/skill/implementation-planning/template.md +220 -0
- package/opencode/skill/implementation-planning/validation.md +88 -0
- package/opencode/skill/implementation-verification/SKILL.md +272 -0
- package/opencode/skill/knowledge-capture/SKILL.md +265 -0
- package/opencode/skill/knowledge-capture/reference/knowledge-capture.md +402 -0
- package/opencode/skill/knowledge-capture/reference.md +444 -0
- package/opencode/skill/knowledge-capture/templates/domain-template.md +325 -0
- package/opencode/skill/knowledge-capture/templates/interface-template.md +255 -0
- package/opencode/skill/knowledge-capture/templates/pattern-template.md +144 -0
- package/opencode/skill/observability-design/SKILL.md +291 -0
- package/opencode/skill/observability-design/references/monitoring-patterns.md +461 -0
- package/opencode/skill/pattern-detection/SKILL.md +171 -0
- package/opencode/skill/pattern-detection/examples/common-patterns.md +359 -0
- package/opencode/skill/performance-analysis/SKILL.md +266 -0
- package/opencode/skill/performance-analysis/references/profiling-tools.md +499 -0
- package/opencode/skill/requirements-analysis/SKILL.md +139 -0
- package/opencode/skill/requirements-analysis/examples/good-prd.md +66 -0
- package/opencode/skill/requirements-analysis/template.md +177 -0
- package/opencode/skill/requirements-analysis/validation.md +69 -0
- package/opencode/skill/requirements-elicitation/SKILL.md +518 -0
- package/opencode/skill/requirements-elicitation/examples/interview-questions.md +226 -0
- package/opencode/skill/requirements-elicitation/examples/user-stories.md +414 -0
- package/opencode/skill/safe-refactoring/SKILL.md +312 -0
- package/opencode/skill/safe-refactoring/reference/code-smells.md +347 -0
- package/opencode/skill/security-assessment/SKILL.md +421 -0
- package/opencode/skill/security-assessment/checklists/security-review-checklist.md +285 -0
- package/opencode/skill/specification-management/SKILL.md +143 -0
- package/opencode/skill/specification-management/readme-template.md +32 -0
- package/opencode/skill/specification-management/reference.md +115 -0
- package/opencode/skill/specification-management/spec.py +229 -0
- package/opencode/skill/specification-validation/SKILL.md +397 -0
- package/opencode/skill/specification-validation/reference/3cs-framework.md +306 -0
- package/opencode/skill/specification-validation/reference/ambiguity-detection.md +132 -0
- package/opencode/skill/specification-validation/reference/constitution-validation.md +301 -0
- package/opencode/skill/specification-validation/reference/drift-detection.md +383 -0
- package/opencode/skill/task-delegation/SKILL.md +607 -0
- package/opencode/skill/task-delegation/examples/file-coordination.md +495 -0
- package/opencode/skill/task-delegation/examples/parallel-research.md +337 -0
- package/opencode/skill/task-delegation/examples/sequential-build.md +504 -0
- package/opencode/skill/task-delegation/reference.md +825 -0
- package/opencode/skill/tech-stack-detection/SKILL.md +89 -0
- package/opencode/skill/tech-stack-detection/references/framework-signatures.md +598 -0
- package/opencode/skill/technical-writing/SKILL.md +190 -0
- package/opencode/skill/technical-writing/templates/adr-template.md +205 -0
- package/opencode/skill/technical-writing/templates/system-doc-template.md +380 -0
- package/opencode/skill/test-design/SKILL.md +464 -0
- package/opencode/skill/test-design/examples/test-pyramid.md +724 -0
- package/opencode/skill/testing/SKILL.md +213 -0
- package/opencode/skill/testing/examples/test-pyramid.md +724 -0
- package/opencode/skill/user-insight-synthesis/SKILL.md +576 -0
- package/opencode/skill/user-insight-synthesis/templates/research-plan-template.md +217 -0
- package/opencode/skill/user-research/SKILL.md +508 -0
- package/opencode/skill/user-research/examples/interview-questions.md +265 -0
- package/opencode/skill/user-research/examples/personas.md +267 -0
- package/opencode/skill/vibe-security/SKILL.md +654 -0
- package/package.json +45 -0
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: domain-driven-design
|
|
3
|
+
description: "Domain-Driven Design tactical and strategic patterns including entities, value objects, aggregates, bounded contexts, and consistency strategies. Use when modeling business domains, designing aggregate boundaries, implementing business rules, or planning data consistency."
|
|
4
|
+
license: MIT
|
|
5
|
+
compatibility: opencode
|
|
6
|
+
metadata:
|
|
7
|
+
category: development
|
|
8
|
+
version: "1.0"
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# Domain-Driven Design
|
|
12
|
+
|
|
13
|
+
Roleplay as a domain modeling specialist that applies DDD tactical and strategic patterns to design bounded contexts, aggregates, and consistency strategies for complex business domains.
|
|
14
|
+
|
|
15
|
+
DomainDrivenDesign {
|
|
16
|
+
Activation {
|
|
17
|
+
When modeling business domains and entities
|
|
18
|
+
When designing aggregate boundaries
|
|
19
|
+
When implementing complex business rules
|
|
20
|
+
When planning data consistency strategies
|
|
21
|
+
When establishing bounded contexts
|
|
22
|
+
When designing domain events and integration
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
Constraints {
|
|
26
|
+
Model business concepts explicitly using ubiquitous language
|
|
27
|
+
Encapsulate business rules in domain layer
|
|
28
|
+
Keep domain free of framework dependencies
|
|
29
|
+
Reference other aggregates by identity only
|
|
30
|
+
Update one aggregate per transaction
|
|
31
|
+
Design small aggregates (prefer single entity)
|
|
32
|
+
Protect invariants at aggregate boundary
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
StrategicPatterns {
|
|
36
|
+
BoundedContext {
|
|
37
|
+
A bounded context defines the boundary within which a domain model applies
|
|
38
|
+
The same term can mean different things in different contexts
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
Example: "Customer" in different contexts
|
|
42
|
+
|
|
43
|
+
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
|
|
44
|
+
│ Sales │ │ Support │ │ Billing │
|
|
45
|
+
│ Context │ │ Context │ │ Context │
|
|
46
|
+
├─────────────────┤ ├─────────────────┤ ├─────────────────┤
|
|
47
|
+
│ Customer: │ │ Customer: │ │ Customer: │
|
|
48
|
+
│ - Leads │ │ - Tickets │ │ - Invoices │
|
|
49
|
+
│ - Opportunities │ │ - SLA │ │ - Payment │
|
|
50
|
+
│ - Proposals │ │ - Satisfaction │ │ - Credit Limit │
|
|
51
|
+
└─────────────────┘ └─────────────────┘ └─────────────────┘
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
ContextIdentification {
|
|
55
|
+
Ask these questions to find context boundaries:
|
|
56
|
+
- Where does the ubiquitous language change?
|
|
57
|
+
- Which teams own which concepts?
|
|
58
|
+
- Where do integration points naturally occur?
|
|
59
|
+
- What could be deployed independently?
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
ContextMapping {
|
|
64
|
+
Define how bounded contexts integrate:
|
|
65
|
+
|
|
66
|
+
| Pattern | Description | Use When |
|
|
67
|
+
|---------|-------------|----------|
|
|
68
|
+
| **Shared Kernel** | Shared code between contexts | Close collaboration, same team |
|
|
69
|
+
| **Customer-Supplier** | Upstream/downstream relationship | Clear dependency direction |
|
|
70
|
+
| **Conformist** | Downstream adopts upstream model | No negotiation power |
|
|
71
|
+
| **Anti-Corruption Layer** | Translation layer between models | Protecting domain from external models |
|
|
72
|
+
| **Open Host Service** | Published API for integration | Multiple consumers |
|
|
73
|
+
| **Published Language** | Shared interchange format | Industry standards exist |
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
UbiquitousLanguage {
|
|
77
|
+
The shared vocabulary between developers and domain experts
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
Building Ubiquitous Language:
|
|
81
|
+
|
|
82
|
+
1. EXTRACT terms from domain expert conversations
|
|
83
|
+
2. DOCUMENT in a glossary with precise definitions
|
|
84
|
+
3. ENFORCE in code - class names, method names, variables
|
|
85
|
+
4. EVOLVE as understanding deepens
|
|
86
|
+
|
|
87
|
+
Example Glossary Entry:
|
|
88
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
89
|
+
│ Term: Order │
|
|
90
|
+
│ Definition: A confirmed request from a customer to purchase │
|
|
91
|
+
│ one or more products at agreed prices. │
|
|
92
|
+
│ NOT: A shopping cart (which is an Intent, not an Order) │
|
|
93
|
+
│ Context: Sales │
|
|
94
|
+
└─────────────────────────────────────────────────────────────┘
|
|
95
|
+
```
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
TacticalPatterns {
|
|
100
|
+
Entities {
|
|
101
|
+
Objects with identity that persists over time
|
|
102
|
+
Equality is based on identity, not attributes
|
|
103
|
+
|
|
104
|
+
Characteristics:
|
|
105
|
+
- Has a unique identifier
|
|
106
|
+
- Mutable state
|
|
107
|
+
- Lifecycle (created, modified, archived)
|
|
108
|
+
- Equality by ID
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
Example:
|
|
112
|
+
┌─────────────────────────────────────────┐
|
|
113
|
+
│ Entity: Order │
|
|
114
|
+
├─────────────────────────────────────────┤
|
|
115
|
+
│ Identity: orderId (UUID) │
|
|
116
|
+
│ State: status, items, total │
|
|
117
|
+
│ Behavior: addItem(), submit(), cancel() │
|
|
118
|
+
└─────────────────────────────────────────┘
|
|
119
|
+
|
|
120
|
+
class Order {
|
|
121
|
+
private readonly id: OrderId; // Identity - immutable
|
|
122
|
+
private status: OrderStatus; // State - mutable
|
|
123
|
+
private items: OrderItem[]; // State - mutable
|
|
124
|
+
|
|
125
|
+
constructor(id: OrderId) {
|
|
126
|
+
this.id = id;
|
|
127
|
+
this.status = OrderStatus.Draft;
|
|
128
|
+
this.items = [];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
equals(other: Order): boolean {
|
|
132
|
+
return this.id.equals(other.id); // Equality by identity
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
ValueObjects {
|
|
139
|
+
Objects without identity
|
|
140
|
+
Equality is based on attributes
|
|
141
|
+
Always immutable
|
|
142
|
+
|
|
143
|
+
Characteristics:
|
|
144
|
+
- No unique identifier
|
|
145
|
+
- Immutable (all properties readonly)
|
|
146
|
+
- Equality by attributes
|
|
147
|
+
- Self-validating
|
|
148
|
+
|
|
149
|
+
```
|
|
150
|
+
Example:
|
|
151
|
+
┌─────────────────────────────────────────┐
|
|
152
|
+
│ Value Object: Money │
|
|
153
|
+
├─────────────────────────────────────────┤
|
|
154
|
+
│ Attributes: amount, currency │
|
|
155
|
+
│ Behavior: add(), subtract(), format() │
|
|
156
|
+
│ Invariant: amount >= 0 │
|
|
157
|
+
└─────────────────────────────────────────┘
|
|
158
|
+
|
|
159
|
+
class Money {
|
|
160
|
+
constructor(
|
|
161
|
+
public readonly amount: number,
|
|
162
|
+
public readonly currency: Currency
|
|
163
|
+
) {
|
|
164
|
+
if (amount < 0) throw new Error('Amount cannot be negative');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
add(other: Money): Money {
|
|
168
|
+
if (!this.currency.equals(other.currency)) {
|
|
169
|
+
throw new Error('Cannot add different currencies');
|
|
170
|
+
}
|
|
171
|
+
return new Money(this.amount + other.amount, this.currency);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
equals(other: Money): boolean {
|
|
175
|
+
return this.amount === other.amount &&
|
|
176
|
+
this.currency.equals(other.currency);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
DecisionMatrix {
|
|
182
|
+
| Use Value Object | Use Entity |
|
|
183
|
+
|------------------|------------|
|
|
184
|
+
| No need to track over time | Need to track lifecycle |
|
|
185
|
+
| Interchangeable instances | Unique identity matters |
|
|
186
|
+
| Defined by attributes | Defined by continuity |
|
|
187
|
+
| Examples: Money, Address, DateRange | Examples: User, Order, Account |
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
Aggregates {
|
|
192
|
+
A cluster of entities and value objects with a defined boundary
|
|
193
|
+
One entity is the aggregate root
|
|
194
|
+
|
|
195
|
+
DesignRules {
|
|
196
|
+
1. PROTECT invariants at aggregate boundary
|
|
197
|
+
2. REFERENCE other aggregates by identity only
|
|
198
|
+
3. UPDATE one aggregate per transaction
|
|
199
|
+
4. DESIGN small aggregates (prefer single entity)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
```
|
|
203
|
+
Example:
|
|
204
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
205
|
+
│ Aggregate: Order │
|
|
206
|
+
│ Root: Order (entity) │
|
|
207
|
+
├─────────────────────────────────────────────────────────────┤
|
|
208
|
+
│ ┌─────────────────┐ │
|
|
209
|
+
│ │ Order (Root) │◄── Aggregate Root │
|
|
210
|
+
│ │ - orderId │ │
|
|
211
|
+
│ │ - customerId ───┼──► Reference by ID only │
|
|
212
|
+
│ │ - status │ │
|
|
213
|
+
│ └────────┬────────┘ │
|
|
214
|
+
│ │ │
|
|
215
|
+
│ ┌────────▼────────┐ │
|
|
216
|
+
│ │ OrderItem │◄── Inside aggregate │
|
|
217
|
+
│ │ - productId ────┼──► Reference by ID only │
|
|
218
|
+
│ │ - quantity │ │
|
|
219
|
+
│ │ - price (Money) │◄── Value Object │
|
|
220
|
+
│ └─────────────────┘ │
|
|
221
|
+
└─────────────────────────────────────────────────────────────┘
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Sizing {
|
|
225
|
+
Start Small:
|
|
226
|
+
- Begin with single-entity aggregates
|
|
227
|
+
- Expand only when invariants require it
|
|
228
|
+
|
|
229
|
+
SignsOfTooLarge:
|
|
230
|
+
- Frequent optimistic lock conflicts
|
|
231
|
+
- Loading too much data for simple operations
|
|
232
|
+
- Multiple users editing simultaneously
|
|
233
|
+
- Transactional failures across unrelated data
|
|
234
|
+
|
|
235
|
+
SignsOfTooSmall:
|
|
236
|
+
- Invariants not protected
|
|
237
|
+
- Business rules scattered across services
|
|
238
|
+
- Eventual consistency where immediate is required
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
DomainEvents {
|
|
243
|
+
Represent something that happened in the domain
|
|
244
|
+
Immutable facts about the past
|
|
245
|
+
|
|
246
|
+
```
|
|
247
|
+
Event Structure:
|
|
248
|
+
┌─────────────────────────────────────────┐
|
|
249
|
+
│ Event: OrderPlaced │
|
|
250
|
+
├─────────────────────────────────────────┤
|
|
251
|
+
│ eventId: UUID │
|
|
252
|
+
│ occurredAt: DateTime │
|
|
253
|
+
│ aggregateId: orderId │
|
|
254
|
+
│ payload: │
|
|
255
|
+
│ - customerId │
|
|
256
|
+
│ - items │
|
|
257
|
+
│ - totalAmount │
|
|
258
|
+
└─────────────────────────────────────────┘
|
|
259
|
+
|
|
260
|
+
Naming Convention:
|
|
261
|
+
- Past tense (OrderPlaced, not PlaceOrder)
|
|
262
|
+
- Domain language (not technical)
|
|
263
|
+
- Include all relevant data (event is immutable)
|
|
264
|
+
|
|
265
|
+
class OrderPlaced implements DomainEvent {
|
|
266
|
+
readonly eventId = uuid();
|
|
267
|
+
readonly occurredAt = new Date();
|
|
268
|
+
|
|
269
|
+
constructor(
|
|
270
|
+
readonly orderId: OrderId,
|
|
271
|
+
readonly customerId: CustomerId,
|
|
272
|
+
readonly items: OrderItemData[],
|
|
273
|
+
readonly totalAmount: Money
|
|
274
|
+
) {}
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
EventPatterns {
|
|
279
|
+
| Pattern | Description | Use Case |
|
|
280
|
+
|---------|-------------|----------|
|
|
281
|
+
| **Event Notification** | Minimal data, query for details | Loose coupling |
|
|
282
|
+
| **Event-Carried State** | Full data in event | Performance, offline |
|
|
283
|
+
| **Event Sourcing** | Events as source of truth | Audit, temporal queries |
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
Repositories {
|
|
288
|
+
Abstract persistence, providing collection-like access to aggregates
|
|
289
|
+
|
|
290
|
+
Principles:
|
|
291
|
+
- One repository per aggregate
|
|
292
|
+
- Returns aggregate roots only
|
|
293
|
+
- Hides persistence mechanism
|
|
294
|
+
- Supports aggregate reconstitution
|
|
295
|
+
|
|
296
|
+
```typescript
|
|
297
|
+
interface OrderRepository {
|
|
298
|
+
findById(id: OrderId): Promise<Order | null>;
|
|
299
|
+
findByCustomer(customerId: CustomerId): Promise<Order[]>;
|
|
300
|
+
save(order: Order): Promise<void>;
|
|
301
|
+
delete(order: Order): Promise<void>;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Implementation hides persistence details
|
|
305
|
+
class PostgresOrderRepository implements OrderRepository {
|
|
306
|
+
async findById(id: OrderId): Promise<Order | null> {
|
|
307
|
+
const row = await this.db.query('SELECT * FROM orders WHERE id = $1', [id]);
|
|
308
|
+
return row ? this.reconstitute(row) : null;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private reconstitute(row: OrderRow): Order {
|
|
312
|
+
// Rebuild aggregate from persistence
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
ConsistencyStrategies {
|
|
320
|
+
TransactionalConsistency {
|
|
321
|
+
Use for invariants within an aggregate
|
|
322
|
+
|
|
323
|
+
Rule: One aggregate per transaction
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
// Good: Single aggregate updated
|
|
327
|
+
async function addItemToOrder(orderId: OrderId, item: OrderItem) {
|
|
328
|
+
const order = await orderRepo.findById(orderId);
|
|
329
|
+
order.addItem(item); // Business rules enforced
|
|
330
|
+
await orderRepo.save(order);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Bad: Multiple aggregates in one transaction
|
|
334
|
+
async function createOrderWithInventory() {
|
|
335
|
+
await db.transaction(async (tx) => {
|
|
336
|
+
await orderRepo.save(order, tx);
|
|
337
|
+
await inventoryRepo.decrement(productId, quantity, tx); // Don't do this
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
```
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
EventualConsistency {
|
|
344
|
+
Use for consistency across aggregates
|
|
345
|
+
|
|
346
|
+
```typescript
|
|
347
|
+
// Order aggregate publishes event
|
|
348
|
+
class Order {
|
|
349
|
+
submit(): void {
|
|
350
|
+
this.status = OrderStatus.Placed;
|
|
351
|
+
this.addEvent(new OrderPlaced(this.id, this.customerId, this.items));
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Separate handler updates inventory (eventually)
|
|
356
|
+
class InventoryHandler {
|
|
357
|
+
async handle(event: OrderPlaced): Promise<void> {
|
|
358
|
+
for (const item of event.items) {
|
|
359
|
+
await this.inventoryService.reserve(item.productId, item.quantity);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
SagaPattern {
|
|
367
|
+
Coordinate multiple aggregates with compensation
|
|
368
|
+
|
|
369
|
+
```
|
|
370
|
+
Saga: Order Fulfillment
|
|
371
|
+
|
|
372
|
+
┌─────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────┐
|
|
373
|
+
│ Create │────►│ Reserve │────►│ Charge │────►│ Ship │
|
|
374
|
+
│ Order │ │ Inventory │ │ Payment │ │ Order │
|
|
375
|
+
└────┬────┘ └──────┬──────┘ └──────┬──────┘ └─────────┘
|
|
376
|
+
│ │ │
|
|
377
|
+
│ Compensate: │ Compensate: │ Compensate:
|
|
378
|
+
│ Cancel Order │ Release Inventory │ Refund Payment
|
|
379
|
+
▼ ▼ ▼
|
|
380
|
+
|
|
381
|
+
On failure at any step, execute compensation in reverse order.
|
|
382
|
+
```
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
ConsistencyDecisionMatrix {
|
|
386
|
+
| Scenario | Strategy |
|
|
387
|
+
|----------|----------|
|
|
388
|
+
| Within single aggregate | Transactional (ACID) |
|
|
389
|
+
| Across aggregates, same service | Eventual (domain events) |
|
|
390
|
+
| Across services | Saga with compensation |
|
|
391
|
+
| Read model updates | Eventual (projection) |
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
AntiPatterns {
|
|
396
|
+
AnemicDomainModel {
|
|
397
|
+
```typescript
|
|
398
|
+
// Anti-pattern: Logic outside domain objects
|
|
399
|
+
class Order {
|
|
400
|
+
id: string;
|
|
401
|
+
items: Item[];
|
|
402
|
+
status: string;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
class OrderService {
|
|
406
|
+
calculateTotal(order: Order): number { ... }
|
|
407
|
+
validate(order: Order): boolean { ... }
|
|
408
|
+
submit(order: Order): void { ... }
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Better: Logic inside domain objects
|
|
412
|
+
class Order {
|
|
413
|
+
private items: OrderItem[];
|
|
414
|
+
private status: OrderStatus;
|
|
415
|
+
|
|
416
|
+
get total(): Money {
|
|
417
|
+
return this.items.reduce((sum, item) => sum.add(item.subtotal), Money.zero());
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
submit(): void {
|
|
421
|
+
this.validate();
|
|
422
|
+
this.status = OrderStatus.Submitted;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
```
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
LargeAggregates {
|
|
429
|
+
```typescript
|
|
430
|
+
// Anti-pattern: Everything in one aggregate
|
|
431
|
+
class Customer {
|
|
432
|
+
orders: Order[]; // Could be thousands
|
|
433
|
+
addresses: Address[];
|
|
434
|
+
paymentMethods: PaymentMethod[];
|
|
435
|
+
preferences: Preferences;
|
|
436
|
+
activityLog: Activity[]; // Could be millions
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// Better: Separate aggregates referenced by ID
|
|
440
|
+
class Customer {
|
|
441
|
+
id: CustomerId;
|
|
442
|
+
defaultAddressId: AddressId;
|
|
443
|
+
defaultPaymentMethodId: PaymentMethodId;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
class Order {
|
|
447
|
+
customerId: CustomerId; // Reference by ID
|
|
448
|
+
}
|
|
449
|
+
```
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
PrimitiveObsession {
|
|
453
|
+
```typescript
|
|
454
|
+
// Anti-pattern: Primitive types for domain concepts
|
|
455
|
+
function createOrder(
|
|
456
|
+
customerId: string,
|
|
457
|
+
productId: string,
|
|
458
|
+
quantity: number,
|
|
459
|
+
price: number,
|
|
460
|
+
currency: string
|
|
461
|
+
) { ... }
|
|
462
|
+
|
|
463
|
+
// Better: Value objects
|
|
464
|
+
function createOrder(
|
|
465
|
+
customerId: CustomerId,
|
|
466
|
+
productId: ProductId,
|
|
467
|
+
quantity: Quantity,
|
|
468
|
+
price: Money
|
|
469
|
+
) { ... }
|
|
470
|
+
```
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
ImplementationChecklists {
|
|
475
|
+
AggregateDesign {
|
|
476
|
+
- [ ] Single entity can be aggregate root
|
|
477
|
+
- [ ] Invariants are protected at boundary
|
|
478
|
+
- [ ] Other aggregates referenced by ID only
|
|
479
|
+
- [ ] Fits in memory comfortably
|
|
480
|
+
- [ ] One transaction per aggregate
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
EntityImplementation {
|
|
484
|
+
- [ ] Has unique identifier
|
|
485
|
+
- [ ] Equality based on ID
|
|
486
|
+
- [ ] Encapsulates business rules
|
|
487
|
+
- [ ] State changes through methods
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
ValueObjectImplementation {
|
|
491
|
+
- [ ] All properties immutable
|
|
492
|
+
- [ ] Equality based on attributes
|
|
493
|
+
- [ ] Self-validating
|
|
494
|
+
- [ ] Operations return new instances
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
RepositoryImplementation {
|
|
498
|
+
- [ ] One per aggregate
|
|
499
|
+
- [ ] Returns aggregate roots only
|
|
500
|
+
- [ ] Hides persistence details
|
|
501
|
+
- [ ] Supports queries needed by domain
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
## References
|
|
507
|
+
|
|
508
|
+
- [Pattern Implementation Examples](examples/ddd-patterns.md) - Code examples in multiple languages
|
|
509
|
+
- [Aggregate Design Guide](reference.md) - Detailed aggregate sizing heuristics
|