npx skills add https://github.com/actionbook/rust-skills --skill m09-domain第二层:设计选择
这个概念在领域中的角色是什么?
在代码建模之前,需要理解:
| 领域概念 | Rust 模式 | 所有权含义 |
|---|---|---|
| 实体 | struct + Id | 拥有所有权,唯一身份 |
| 值对象 | struct + Clone/Copy | 可共享,不可变 |
| 聚合根 | struct 拥有子对象 | 清晰的所有权树 |
| 仓储 | trait | 抽象持久化 |
| 领域事件 | enum | 捕获状态变化 |
| 服务 | impl 块 / 自由函数 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 无状态操作 |
在创建领域类型之前:
概念的身份是什么?
必须保持哪些不变量?
谁拥有这些数据?
追溯至领域约束(第三层):
"我应该如何建模一个交易?"
↑ 提问:哪些领域规则管理交易?
↑ 检查:domain-fintech(审计、精度要求)
↑ 检查:业务利益相关者(哪些不变量?)
| 设计问题 | 追溯至 | 提问 |
|---|---|---|
| 实体 vs 值对象 | domain-* | 什么使两个实例“相同”? |
| 聚合边界 | domain-* | 哪些内容必须保持一致? |
| 验证规则 | domain-* | 适用哪些业务规则? |
追溯至实现(第一层):
"建模为实体"
↓ m01-ownership: 拥有所有权,唯一
↓ m05-type-driven: 使用新类型包装 Id
"建模为值对象"
↓ m01-ownership: Clone/Copy 可行
↓ m05-type-driven: 在构造时验证
"建模为聚合"
↓ m01-ownership: 父级拥有子级
↓ m02-resource: 考虑在聚合内使用 Rc 进行共享
| DDD 概念 | Rust 模式 | 示例 |
|---|---|---|
| 值对象 | 新类型 | struct Email(String); |
| 实体 | 结构体 + ID | struct User { id: UserId, ... } |
| 聚合 | 模块边界 | mod order { ... } |
| 仓储 | 特征 | trait UserRepo { fn find(...) } |
| 领域事件 | 枚举 | enum OrderEvent { Created, ... } |
struct Email(String);
impl Email {
pub fn new(s: &str) -> Result<Self, ValidationError> {
validate_email(s)?;
Ok(Self(s.to_string()))
}
}
struct UserId(Uuid);
struct User {
id: UserId,
email: Email,
// ... 其他字段
}
impl PartialEq for User {
fn eq(&self, other: &Self) -> bool {
self.id == other.id // 身份相等性
}
}
mod order {
pub struct Order {
id: OrderId,
items: Vec<OrderItem>, // 拥有的子对象
// ...
}
impl Order {
pub fn add_item(&mut self, item: OrderItem) {
// 强制执行聚合不变量
}
}
}
| 错误 | 为何错误 | 更好的做法 |
|---|---|---|
| 原始类型偏执 | 无类型安全 | 新类型包装器 |
| 带有不变量的公共字段 | 不变量被破坏 | 私有字段 + 访问器 |
| 泄露聚合内部细节 | 封装被破坏 | 在根上使用方法 |
| 使用字符串表示语义类型 | 无验证 | 经过验证的新类型 |
| 何时使用 | 参见 |
|---|---|
| 类型驱动实现 | m05-type-driven |
| 聚合的所有权 | m01-ownership |
| 领域错误处理 | m13-domain-error |
| 特定领域规则 | domain-* |
每周安装量
39
仓库
GitHub 星标数
828
首次出现
2026年1月23日
安全审计
安装于
gemini-cli36
opencode35
codex35
github-copilot31
claude-code30
cursor30
Layer 2: Design Choices
What is this concept's role in the domain?
Before modeling in code, understand:
| Domain Concept | Rust Pattern | Ownership Implication |
|---|---|---|
| Entity | struct + Id | Owned, unique identity |
| Value Object | struct + Clone/Copy | Shareable, immutable |
| Aggregate Root | struct owns children | Clear ownership tree |
| Repository | trait | Abstracts persistence |
| Domain Event | enum | Captures state changes |
| Service | impl block / free fn | Stateless operations |
Before creating a domain type:
What's the concept's identity?
What invariants must hold?
Who owns this data?
To domain constraints (Layer 3):
"How should I model a Transaction?"
↑ Ask: What domain rules govern transactions?
↑ Check: domain-fintech (audit, precision requirements)
↑ Check: Business stakeholders (what invariants?)
| Design Question | Trace To | Ask |
|---|---|---|
| Entity vs Value Object | domain-* | What makes two instances "the same"? |
| Aggregate boundaries | domain-* | What must be consistent together? |
| Validation rules | domain-* | What business rules apply? |
To implementation (Layer 1):
"Model as Entity"
↓ m01-ownership: Owned, unique
↓ m05-type-driven: Newtype for Id
"Model as Value Object"
↓ m01-ownership: Clone/Copy OK
↓ m05-type-driven: Validate at construction
"Model as Aggregate"
↓ m01-ownership: Parent owns children
↓ m02-resource: Consider Rc for shared within aggregate
| DDD Concept | Rust Pattern | Example |
|---|---|---|
| Value Object | Newtype | struct Email(String); |
| Entity | Struct + ID | struct User { id: UserId, ... } |
| Aggregate | Module boundary | mod order { ... } |
| Repository | Trait | trait UserRepo { fn find(...) } |
| Domain Event | Enum | enum OrderEvent { Created, ... } |
struct Email(String);
impl Email {
pub fn new(s: &str) -> Result<Self, ValidationError> {
validate_email(s)?;
Ok(Self(s.to_string()))
}
}
struct UserId(Uuid);
struct User {
id: UserId,
email: Email,
// ... other fields
}
impl PartialEq for User {
fn eq(&self, other: &Self) -> bool {
self.id == other.id // Identity equality
}
}
mod order {
pub struct Order {
id: OrderId,
items: Vec<OrderItem>, // Owned children
// ...
}
impl Order {
pub fn add_item(&mut self, item: OrderItem) {
// Enforce aggregate invariants
}
}
}
| Mistake | Why Wrong | Better |
|---|---|---|
| Primitive obsession | No type safety | Newtype wrappers |
| Public fields with invariants | Invariants violated | Private + accessor |
| Leaked aggregate internals | Broken encapsulation | Methods on root |
| String for semantic types | No validation | Validated newtype |
| When | See |
|---|---|
| Type-driven implementation | m05-type-driven |
| Ownership for aggregates | m01-ownership |
| Domain error handling | m13-domain-error |
| Specific domain rules | domain-* |
Weekly Installs
39
Repository
GitHub Stars
828
First Seen
Jan 23, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli36
opencode35
codex35
github-copilot31
claude-code30
cursor30
站立会议模板:敏捷开发每日站会指南与工具(含远程团队异步模板)
10,500 周安装
媒体改编技能:系统化分析转化方法,将现有作品转化为科幻设定 | 创意内容开发指南
120 周安装
Tamagui Monorepo 跨平台开发指南:React Native、Next.js、Expo、TypeScript 全栈解决方案
122 周安装
Motion动画库指南:高性能JavaScript/TypeScript网页动效开发与性能优化
123 周安装
Node.js开发专家指南:TypeScript、Payload CMS、Next.js与Vue.js全栈实战
122 周安装
Tailwind CSS 最佳实践指南:29条规则构建响应式、可维护界面(含v4迁移)
120 周安装
产品需求文档(PRD)创建指南与模板 - SDD工作流第2层产物,含双重评分标准
127 周安装