概念概述
整洁架构(Clean Architecture)指的是:整洁架构强制依赖规则:内层实体与用例不感知外层的交付与持久化机制,核心业务规则保持稳定。
从更严格的定义看,Clean architecture is a set of concentric boundaries, from entities and use cases to interface adapters and frameworks, governed by the rule that source dependencies point only inward. 该概念在工程实践中的价值,在于它能帮助团队用统一的语言描述一个具体的质量属性或设计约束,从而在评审与协作中减少歧义。
为什么需要它
It is used to keep core business rules stable while delivery mechanisms and data stores change around them. 它与 Domain-Driven Design、Bounded Context、Aggregate Root 等概念经常一起出现,用于解决相近的工程问题。理解它的适用场景,有助于在多种备选方案之间做出有依据的取舍,而不是凭直觉选择技术方案。
常见误解
- clean architecture mandates a specific number of projects
- the dependency rule applies to runtime calls as well as source imports
需要避免的反模式
- letting a use case import a framework-specific request object
- requiring the domain to know how a response is serialised
实践要点
- The dependency rule constrains source code imports, not the runtime call direction, so an outer layer may call an inner one while the inner layer never references outer types.
- Crossing a boundary inward is done through data structures and interfaces the inner layer owns, keeping details such as SQL or HTTP out of the core.
- Applied dogmatically it can add ceremony for little benefit in small applications, so the boundary worth drawing is the one that changes most often.
参考来源
https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html