clean-architecture
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseClean Architecture Best Practices
Clean Architecture最佳实践
Comprehensive guide to Clean Architecture principles for designing maintainable, testable software systems. Based on Robert C. Martin's "Clean Architecture: A Craftsman's Guide to Software Structure and Design." Contains 42 rules across 8 categories, prioritized by architectural impact.
本指南全面介绍了用于设计可维护、可测试软件系统的Clean Architecture原则,基于Robert C. Martin所著的《Clean Architecture: A Craftsman's Guide to Software Structure and Design》。包含8个类别共42条规则,按架构影响优先级排序。
When to Apply
适用场景
Reference these guidelines when:
- Designing new software systems or modules
- Structuring dependencies between layers
- Defining boundaries between business logic and infrastructure
- Reviewing code for architectural violations
- Refactoring coupled systems toward cleaner structure
在以下场景中可参考这些指南:
- 设计新的软件系统或模块时
- 规划各层之间的依赖结构时
- 定义业务逻辑与基础设施之间的边界时
- 审查代码是否存在架构违规情况时
- 重构耦合系统以实现更清晰的结构时
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Dependency Direction | CRITICAL | |
| 2 | Entity Design | CRITICAL | |
| 3 | Use Case Isolation | HIGH | |
| 4 | Component Cohesion | HIGH | |
| 5 | Boundary Definition | MEDIUM-HIGH | |
| 6 | Interface Adapters | MEDIUM | |
| 7 | Framework Isolation | MEDIUM | |
| 8 | Testing Architecture | LOW-MEDIUM | |
| 优先级 | 类别 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | Dependency Direction | CRITICAL | |
| 2 | Entity Design | CRITICAL | |
| 3 | Use Case Isolation | HIGH | |
| 4 | Component Cohesion | HIGH | |
| 5 | Boundary Definition | MEDIUM-HIGH | |
| 6 | Interface Adapters | MEDIUM | |
| 7 | Framework Isolation | MEDIUM | |
| 8 | Testing Architecture | LOW-MEDIUM | |
Quick Reference
快速参考
1. Dependency Direction (CRITICAL)
1. Dependency Direction (CRITICAL)
- - Source dependencies point inward only
dep-inward-only - - Interfaces belong to clients not implementers
dep-interface-ownership - - Avoid framework imports in inner layers
dep-no-framework-imports - - Use simple data structures across boundaries
dep-data-crossing-boundaries - - Eliminate cyclic dependencies between components
dep-acyclic-dependencies - - Depend on stable abstractions not volatile concretions
dep-stable-abstractions
- - 源码依赖仅指向内部
dep-inward-only - - 接口归客户端所有,而非实现方
dep-interface-ownership - - 避免在内层中导入框架代码
dep-no-framework-imports - - 跨边界使用简单数据结构
dep-data-crossing-boundaries - - 消除组件间的循环依赖
dep-acyclic-dependencies - - 依赖稳定的抽象而非易变的具体实现
dep-stable-abstractions
2. Entity Design (CRITICAL)
2. Entity Design (CRITICAL)
- - Entities contain only enterprise business rules
entity-pure-business-rules - - Entities must not know how they are persisted
entity-no-persistence-awareness - - Encapsulate business invariants within entities
entity-encapsulate-invariants - - Use value objects for domain concepts
entity-value-objects - - Build rich domain models not anemic data structures
entity-rich-not-anemic
- - 实体仅包含企业级业务规则
entity-pure-business-rules - - 实体不得知晓自身的持久化方式
entity-no-persistence-awareness - - 在实体中封装业务不变量
entity-encapsulate-invariants - - 对领域概念使用值对象
entity-value-objects - - 构建富领域模型而非贫血数据结构
entity-rich-not-anemic
3. Use Case Isolation (HIGH)
3. Use Case Isolation (HIGH)
- - Each use case has one reason to change
usecase-single-responsibility - - Define input and output ports for use cases
usecase-input-output-ports - - Use cases orchestrate entities not implement business rules
usecase-orchestrates-not-implements - - Use cases must not contain presentation logic
usecase-no-presentation-logic - - Declare all dependencies explicitly in constructor
usecase-explicit-dependencies - - Use case defines the transaction boundary
usecase-transaction-boundary
- - 每个用例仅有一个变更理由
usecase-single-responsibility - - 为用例定义输入和输出端口
usecase-input-output-ports - - 用例协调实体而非实现业务规则
usecase-orchestrates-not-implements - - 用例不得包含展示逻辑
usecase-no-presentation-logic - - 在构造函数中显式声明所有依赖
usecase-explicit-dependencies - - 用例定义事务边界
usecase-transaction-boundary
4. Component Cohesion (HIGH)
4. Component Cohesion (HIGH)
- - Structure should scream the domain not the framework
comp-screaming-architecture - - Group classes that change together
comp-common-closure - - Avoid forcing clients to depend on unused code
comp-common-reuse - - Release components as cohesive units
comp-reuse-release-equivalence - - Depend in the direction of stability
comp-stable-dependencies
- - 架构结构应突出领域而非框架
comp-screaming-architecture - - 将同时变更的类归为一组
comp-common-closure - - 避免强制客户端依赖未使用的代码
comp-common-reuse - - 以内聚单元的形式发布组件
comp-reuse-release-equivalence - - 朝稳定性方向依赖
comp-stable-dependencies
5. Boundary Definition (MEDIUM-HIGH)
5. Boundary Definition (MEDIUM-HIGH)
- - Use humble objects at architectural boundaries
bound-humble-object - - Use partial boundaries when full separation is premature
bound-partial-boundaries - - Weigh boundary cost against ignorance cost
bound-boundary-cost-awareness - - Treat main as a plugin to the application
bound-main-component - - Defer framework and database decisions
bound-defer-decisions - - Services must have internal clean architecture
bound-service-internal-architecture
- - 在架构边界处使用简单对象(Humble Object)
bound-humble-object - - 当完全分离为时尚早时,使用部分边界
bound-partial-boundaries - - 权衡边界成本与无知成本
bound-boundary-cost-awareness - - 将main视为应用的插件
bound-main-component - - 推迟框架和数据库相关决策
bound-defer-decisions - - 服务必须具备内部整洁架构
bound-service-internal-architecture
6. Interface Adapters (MEDIUM)
6. Interface Adapters (MEDIUM)
- - Keep controllers thin
adapt-controller-thin - - Presenters format data for the view
adapt-presenter-formats - - Gateways hide external system details
adapt-gateway-abstraction - - Use mappers to translate between layers
adapt-mapper-translation - - Build anti-corruption layers for external systems
adapt-anti-corruption-layer
- - 保持控制器轻量化
adapt-controller-thin - - 由展示器(Presenter)为视图格式化数据
adapt-presenter-formats - - 网关隐藏外部系统细节
adapt-gateway-abstraction - - 使用映射器在各层间转换数据
adapt-mapper-translation - - 为外部系统构建防腐层(Anti-Corruption Layer)
adapt-anti-corruption-layer
7. Framework Isolation (MEDIUM)
7. Framework Isolation (MEDIUM)
- - Domain layer has zero framework dependencies
frame-domain-purity - - Keep ORM usage in infrastructure layer
frame-orm-in-infrastructure - - Web framework concerns stay in interface layer
frame-web-in-infrastructure - - Dependency injection containers live at the edge
frame-di-container-edge - - Abstract logging behind domain interfaces
frame-logging-abstraction
- - 领域层无任何框架依赖
frame-domain-purity - - 将ORM使用限制在基础设施层
frame-orm-in-infrastructure - - Web框架相关逻辑仅存于接口层
frame-web-in-infrastructure - - 依赖注入容器位于系统边缘
frame-di-container-edge - - 基于领域接口抽象日志功能
frame-logging-abstraction
8. Testing Architecture (LOW-MEDIUM)
8. Testing Architecture (LOW-MEDIUM)
- - Tests are part of the system architecture
test-tests-are-architecture - - Design for testability from the start
test-testable-design - - Test each layer in isolation
test-layer-isolation - - Verify architectural boundaries with tests
test-boundary-verification
- - 测试是系统架构的一部分
test-tests-are-architecture - - 从设计初期就考虑可测试性
test-testable-design - - 独立测试每一层
test-layer-isolation - - 用测试验证架构边界
test-boundary-verification
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件以获取详细说明和代码示例:
- 章节定义 - 类别结构与影响级别说明
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 说明 |
|---|---|
| references/_sections.md | 类别定义与排序说明 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |