关于 ddd-clean-architecture
面向开发者的 Clean Architecture 与 DDD 设计参考,介绍分层、依赖规则和测试实践,帮助组织应用结构。
主要功能
- 01四层架构指引
- 02依赖规则与层边界
- 03DDD 实体和用例映射
- 04包结构与测试组织
适用场景
- 01设计分层应用
- 02避免控制器承载业务规则
- 03定义便于测试的 I/O 接口
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ partme-ai/ full-stack-skills --skill ddd-clean-architecture安装后在 Claude Code 中输入/ddd-clean-architecture确认已加载。
附带文件LICENSE.txt
详细介绍
适用场景
在用户希望进行以下操作时使用此技能:
- 按照整洁架构分层(实体、用例、接口适配器、框架)来组织应用程序
- 强制执行依赖规则:内层永不依赖外层
- 组织项目包并明确测试边界
- 将整洁架构与领域驱动设计模式结合
使用方法
工作流
- 定义实体(最内层),其中包含企业范围的业务规则
- 定义用例(应用层),其中包含特定于应用程序的业务逻辑
- 创建接口适配器(呈现器、网关),用于在各层之间转换数据
- 实现框架(最外层),处理 Web、数据库和 UI 相关事务
分层结构
com.example.app/
├── entity/ # Entities — business rules, no dependencies
├── usecase/ # Use Cases — application logic, depends only on entity
│ ├── port/ # Input/output port interfaces
│ └── interactor/ # Use case implementations
├── adapter/ # Interface Adapters — presenters, gateways
│ ├── controller/ # Web controllers
│ ├── presenter/ # Response formatting
│ └── gateway/ # Gateway implementations
└── framework/ # Frameworks — DB, web server, external APIs
├── web/
└── persistence/
用例示例
// Use case port (input boundary)
public interface CreateOrderUseCase {
OrderOutput execute(CreateOrderInput input);
}
// Use case interactor
public class CreateOrderInteractor implements CreateOrderUseCase {
private final OrderGateway orderGateway;
private final PaymentGateway paymentGateway;
public CreateOrderInteractor(OrderGateway orderGateway, PaymentGateway paymentGateway) {
this.orderGateway = orderGateway;
this.paymentGateway = paymentGateway;
}
@Override
public OrderOutput execute(CreateOrderInput input) {
Order order = Order.create(input.getItems());
paymentGateway.charge(order.totalAmount());
orderGateway.save(order);
return OrderOutput.from(order);
}
}
网关接口(在用例层中定义)
public interface OrderGateway {
void save(Order order);
Optional<Order> findById(String id);
}
最佳实践
- 将业务规则集中在实体和用例中;避免在控制器或数据库层放置业务逻辑
- 使用接口隔离 I/O;这样可以轻松替换和测试核心部分
- 实体和用例可以自然映射到 DDD 聚合和领域服务
- 使用单元测试测试核心层;使用集成测试或 E2E 测试外层
资源
- Robert C. Martin 所著《整洁架构》
- https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
关键词
整洁架构、依赖规则、用例、实体、接口适配器、网关、分层、DDD、可测试性
评论
支持 Markdown