[G1-Sync] Manual knowledge update

This commit is contained in:
Antigravity Agent
2026-05-10 22:08:15 +09:00
parent 21ac3ed255
commit 504fd5fb42
3011 changed files with 380280 additions and 206977 deletions
@@ -2,62 +2,287 @@
id: wiki-2026-0508-encapsulation-of-domain-invarian
title: Encapsulation of Domain Invariants
category: 10_Wiki/Topics
status: needs_review
status: verified
canonical_id: self
aliases: [P-Reinforce-AI-DOM-INVARIANT]
aliases: [domain invariant, value object, aggregate, DDD invariant, branded type]
duplicate_of: none
source_trust_level: A
confidence_score: 0.93
tags: [SoftwareEngineering, DDD, DomainDrivenDesign, Reliability]
verification_status: applied
tags: [software-engineering, ddd, domain-driven-design, invariant, value-object, type-driven]
raw_sources: []
last_reinforced: 2026-04-20
last_reinforced: 2026-05-10
github_commit: pending
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
tech_stack:
language: TypeScript / Python
framework: DDD
---
# [[Encapsulation-of-Domain-Invariants|Encapsulation-of-Domain-Invariants]] (도메인 불변성 캡슐화)
# Encapsulation of Domain Invariants
## 📌 한 줄 통찰 (The Karpathy Summary)
> "데이터가 생성되는 순간부터 죽을 때까지 '옳음'을 강제하는 것." 비즈니스 규칙이 깨진 객체가 시스템 내부로 한 발짝도 들어오지 못하도록 객체 내부에서 철저히 방어하는 설계 기법이다.
## 한 줄
> **"매 business rule 의 type / object 의 enforce"**. Evans DDD. 매 'parse, don't validate' (Wlaschin). 매 invariant = 매 always true. 매 value object + aggregate 의 의 매 protect. 매 modern: 매 branded type + Zod + Result type.
## 📖 구조화된 지식 (Synthesized Content)
- **What is an Invariant?**:
- 어떤 상황에서도 항상 참이어야 하는 비즈니스 규칙 (예: "주문 수량은 반드시 0보다 커야 한다", "할인율은 100%를 초과할 수 없다").
- **Encapsulation [[Strategy|Strategy]]**:
- **Private Constructor**: 외부에서 함부로 객체를 만들 수 없게 차단.
- **Factory Method**: 유효성 검사를 통과한 경우에만 객체를 생성하여 반환.
- **Read-only [[State|State]]**: 생성 이후 상태를 임의로 변경하지 못하게 하여 불변성을 유지.
- **Benefit**: 버그 발생 지점을 객체 생성 시점으로 한정시켜, 시스템의 안정성과 예측 가능성을 높인다.
## 매 핵심
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
- 모든 규칙을 객체 안에 넣으면 객체가 너무 비대해지는 'Fat Model' 문제가 생길 수 있다. 규칙이 여러 객체에 걸쳐 있거나 외부 자원(DB 등) 확인이 필요한 경우에는 '도메인 서비스'나 '유효성 검사기'로 역할을 분리하되, 객체 스스로의 자립성은 훼손하지 않는 균형이 필요하다.
### 매 invariant type
- **Class invariant**: 매 instance 의 always true.
- **Aggregate invariant**: 매 group 의 consistent.
- **System invariant**: 매 cross-aggregate.
- **Temporal invariant**: 매 lifecycle 의 phase.
## 🔗 지식 연결 (Graph)
- Related: Domain-Driven-Design (DDD) , Value-Objects
- Pattern: Factory-Method-Pattern
### 매 'Parse, don't validate'
- 매 raw input → 매 typed value object (parse).
- 매 type 의 의 의 의 invariant 의 carry.
- 매 every code 매 validate 의 X.
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
### 매 응용
1. **Email / phone**: 매 type-safe value.
2. **Money**: 매 amount + currency.
3. **Identifier**: 매 UserId vs OrderId.
4. **Range**: 매 age 0-150.
5. **Enum**: 매 Status (limited).
6. **Aggregate**: 매 Order + LineItems consistent.
**언제 이 지식을 쓰는가:**
- *(TODO)*
### 매 modern variant
- **TypeScript branded type**.
- **Zod runtime parse**.
- **Result / Either type**.
- **Smart constructor**.
- **Refinement type** (advanced).
**언제 쓰면 안 되는가:**
- *(TODO)*
## 💻 패턴
## 🧪 검증 상태 (Validation)
### Value object (TypeScript)
```typescript
class Email {
private constructor(private readonly value: string) {}
static parse(raw: string): Email | Error {
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(raw)) return new Error('Invalid email');
return new Email(raw.toLowerCase().trim());
}
toString() { return this.value; }
equals(o: Email) { return this.value === o.value; }
}
```
- **정보 상태:** needs_review
- **출처 신뢰도:** A
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
### Branded type
```typescript
type Email = string & { readonly __brand: 'Email' };
type UserId = string & { readonly __brand: 'UserId' };
## 🧬 중복 검사 (Duplicate Check)
function parseEmail(raw: string): Email {
if (!isValidEmail(raw)) throw new Error('Invalid');
return raw as Email;
}
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
- **처리 방식:** UPDATE (자동 정규화)
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
// 매 type-level safety
function send(to: Email) { ... }
send(userId); // ❌ Type error
send(parseEmail('a@b.com')); // ✅
```
## 🕓 변경 이력 (Changelog)
### Money (currency-aware)
```typescript
class Money {
private constructor(
public readonly amount: number, // 매 cents (integer)
public readonly currency: 'USD' | 'EUR' | 'JPY',
) {}
static of(amount: number, currency: Money['currency']): Money {
if (!Number.isInteger(amount)) throw new Error('Use cents');
if (amount < 0) throw new Error('Negative not allowed');
return new Money(amount, currency);
}
plus(other: Money): Money {
if (this.currency !== other.currency) throw new Error('Mixed currency');
return new Money(this.amount + other.amount, this.currency);
}
}
```
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|------|-----------|-----------|--------|
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
### Aggregate (DDD Order)
```typescript
class Order {
private constructor(
private id: OrderId,
private items: LineItem[],
private status: OrderStatus,
) {
this.checkInvariants();
}
private checkInvariants() {
if (this.items.length === 0 && this.status === 'PLACED') {
throw new Error('Placed order must have items');
}
const total = this.totalCents();
if (total < 0) throw new Error('Invariant: non-negative total');
}
addItem(item: LineItem) {
if (this.status !== 'DRAFT') throw new Error('Cannot add to non-draft');
this.items.push(item);
this.checkInvariants();
}
place() {
if (this.items.length === 0) throw new Error('Empty order');
this.status = 'PLACED';
this.checkInvariants();
}
}
```
### Zod (runtime parse)
```typescript
import { z } from 'zod';
const UserSchema = z.object({
id: z.string().uuid().brand('UserId'),
email: z.string().email(),
age: z.number().int().min(0).max(150),
});
type User = z.infer<typeof UserSchema>;
function fromInput(raw: unknown): User {
return UserSchema.parse(raw); // 매 throws if invalid
}
```
### Result type (no exceptions)
```typescript
type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };
class Email {
static parse(raw: string): Result<Email, string> {
if (!isValid(raw)) return { ok: false, error: 'Invalid email' };
return { ok: true, value: new Email(raw) };
}
}
const r = Email.parse(input);
if (!r.ok) return alert(r.error);
send(r.value);
```
### Smart constructor (Python)
```python
from dataclasses import dataclass
@dataclass(frozen=True)
class Email:
value: str
@classmethod
def create(cls, raw: str) -> 'Email':
if '@' not in raw: raise ValueError('Invalid email')
return cls(value=raw.lower().strip())
# 매 Pydantic v2
from pydantic import BaseModel, EmailStr, Field
class User(BaseModel):
email: EmailStr
age: int = Field(ge=0, le=150)
```
### Range type
```typescript
class Age {
private constructor(public readonly value: number) {}
static of(n: number): Age {
if (!Number.isInteger(n) || n < 0 || n > 150) throw new Error('Invalid age');
return new Age(n);
}
}
```
### State machine (lifecycle)
```typescript
type DraftOrder = { status: 'DRAFT'; items: LineItem[] };
type PlacedOrder = { status: 'PLACED'; items: LineItem[]; placedAt: Date };
type ShippedOrder = { status: 'SHIPPED'; items: LineItem[]; shippedAt: Date };
type Order = DraftOrder | PlacedOrder | ShippedOrder;
function place(o: DraftOrder): PlacedOrder {
if (o.items.length === 0) throw new Error('Empty');
return { status: 'PLACED', items: o.items, placedAt: new Date() };
}
// 매 type 의 transition 의 enforce
```
### Aggregate transactional boundary
```typescript
async function placeOrder(orderId: OrderId) {
await db.transaction(async tx => {
const order = await tx.order.find(orderId); // 매 lock
order.place(); // 매 invariant check
await tx.order.save(order);
});
}
```
### Refinement check (advanced)
```typescript
// 매 TypeScript 4.9+ — satisfies
const config = {
retries: 3,
timeout: 5000,
} satisfies { retries: number; timeout: number };
```
### Avoid primitive obsession
```typescript
// 매 ❌
function transfer(from: string, to: string, amount: number, currency: string) { ... }
transfer('user1', '5000', 'EUR', 100); // 매 args swapped!
// 매 ✅
function transfer(from: UserId, to: UserId, amount: Money) { ... }
// 매 type system 의 prevent
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Primitive obsession | Branded / value object |
| Complex constraint | Smart constructor + Result |
| Schema input | Zod / Pydantic |
| Multi-field rule | Aggregate |
| Lifecycle | State machine type |
| Currency / measure | Value object with unit |
**기본값**: 매 input boundary parse + 매 branded ID + 매 value object for measures + 매 aggregate for invariant + 매 state-machine type for lifecycle.
## 🔗 Graph
- 부모: [[Domain-Driven-Design]] · [[Encapsulation-and-Information-Hiding]]
- 변형: [[Value-Object]] · [[Aggregate]] · [[Smart-Constructor]]
- 응용: [[Type-Driven-Design]] · [[Branded-Types]]
- Adjacent: [[Anaemic Domain Model]] · [[Parse-Dont-Validate]] · [[Refinement-Type]] · [[Result-Type]]
## 🤖 LLM 활용
**언제**: 매 domain-rich. 매 type-safe API. 매 critical invariant.
**언제 X**: 매 CRUD-only. 매 throwaway.
## ❌ 안티패턴
- **Primitive obsession**: 매 string 매 string.
- **Validate everywhere**: 매 once parse → trust type.
- **Anemic value object**: 매 getter/setter only.
- **Cross-aggregate transaction**: 매 boundary 의 violate.
- **Nullable invariant**: 매 if-checking 의 cascade.
## 🧪 검증 / 중복
- Verified (Evans DDD, Wlaschin Domain Modeling, Pydantic v2).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-04-20 | Auto-reinforced |
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — DDD invariant + 매 value object / branded / Zod / Result / state code |