概念概述
限界上下文(Bounded Context)指的是:限界上下文是领域模型及其语言保持一致的显式边界,防止一个被重载的词在不同模型中表示相互冲突的含义。
从更严格的定义看,A bounded context is a boundary that delimits where a particular domain model applies and where its terms have a single, precise meaning, separating models that would otherwise conflict. 该概念在工程实践中的价值,在于它能帮助团队用统一的语言描述一个具体的质量属性或设计约束,从而在评审与协作中减少歧义。
为什么需要它
It is used to partition a large domain so teams can evolve their models independently without a global, contradictory glossary. 它与 Aggregate Root、Repository Pattern、Unit of Work 等概念经常一起出现,用于解决相近的工程问题。理解它的适用场景,有助于在多种备选方案之间做出有依据的取舍,而不是凭直觉选择技术方案。
常见误解
- a bounded context equals one deployable service
- the same concept must have one shared representation everywhere
需要避免的反模式
- sharing a single large domain model across every team
- allowing one term to carry several meanings in the same model
实践要点
- The same word can legitimately mean different things in different contexts, so a policy in one context is not the same object as a policy in another, and modelling them together creates contradictions.
- Contexts relate through explicit mappings rather than a shared class, so translating at the boundary preserves each model’s integrity.
- A context is a model boundary, not a deployment unit, so several contexts may live in one service or a single context may span several.