关于 domain-driven-design
面向开发者的领域驱动设计技能,帮助围绕业务领域构建软件模型,涵盖核心概念、战术构件及用于组织领域模型和上下文关系的战略模式。
主要功能
- 01通用语言与限界上下文
- 02上下文映射
- 03实体与值对象
- 04聚合与仓储
- 05领域事件和战略模式
适用场景
- 01划分领域模型边界
- 02建模实体与不可变值对象
- 03组织聚合的持久化
- 04梳理上下文间的关系
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dralgorhythm/ claude-agentic-framework --skill domain-driven-design安装后在 Claude Code 中输入/domain-driven-design确认已加载。
详细介绍
领域驱动设计
核心概念
统一语言
使用与领域专家相同的术语。代码应读起来像业务文档。
限界上下文
定义并应用特定领域模型的边界。
上下文映射
展示限界上下文之间的关系。
构建块
实体
具有随时间持续存在的身份。相等性基于 ID。
class User {
constructor(
public readonly id: UserId,
public email: Email,
public name: string
) {}
}
值对象
不可变,相等性基于属性。
class Email {
private constructor(public readonly value: string) {}
static create(value: string): Email {
if (!value.includes('@')) {
throw new Error('Invalid email');
}
return new Email(value);
}
equals(other: Email): boolean {
return this.value === other.value;
}
}
聚合
由实体和值对象组成、以一个根实体为核心的集群。
class Order { // Aggregate Root
private items: OrderItem[] = [];
addItem(product: ProductId, quantity: number): void {
// Business rules enforced here
this.items.push(new OrderItem(product, quantity));
}
get total(): Money {
return this.items.reduce((sum, item) => sum.add(item.subtotal), Money.zero());
}
}
仓储
抽象聚合的数据访问。
interface OrderRepository {
findById(id: OrderId): Promise<Order | null>;
save(order: Order): Promise<void>;
}
领域事件
领域中发生的事情。
class OrderPlaced {
constructor(
public readonly orderId: OrderId,
public readonly userId: UserId,
public readonly occurredAt: Date
) {}
}
战略模式
防腐层
在你的模型与外部系统之间进行转换。
共享内核
多个上下文共享的领域模型子集。
客户端-供应商
上游提供下游所需的内容。
评论
支持 Markdown