Skip to content
Contents

Bounded Contexts as a strategic pattern beyond DDD

The term bounded context (BC) was introduced by Eric Evans in the Domain-Driven Design “blue book” Part IV Strategic Design, Chapter 14 Maintaining Model Integrity.

Defining Bounded Contexts

A definition from the book:

A BOUNDED CONTEXT delimits the applicability of a particular model so that team members have a clear and shared understanding of what has to be consistent and how it relates to other CONTEXTS. Within that CONTEXT, work to keep the model logically unified, but do not worry about applicability outside those bounds. In other CONTEXTS, other models apply, with differences in terminology, in concepts and rules, and in dialects of the UBIQUITOUS LANGUAGE.

Eric Evans, Domain-Driven Design

A domain model is thus valid only within a context and may not and more importantly should not be applicable to another context. All too often I and many other developers fall into the trap of combining disparate contexts. What follows are attempts to coerce a single model to fit requirements of disparate contexts. This comes at no surprise given an inherent tendency to follow the DRY principle. This principle has value and the value is immediately evident in the most basic factoring of code, such as extracting a set of instructions into a sub-routine. DRY however is very much a double edged sword and can lead one astray when applied incorrectly. As easy as it is to gather value from this principle is to apply it incorrectly. Incorrect application often occurs when subtle nuances in the model are ignored, when responsibilities are conflated. Indeed, it is the proper division of responsibilities that leads to breakthroughs in the model. Furthermore, in practice, it is often easier and more rewarding to unify different parts of the model than it is to divide one part into two. For an in-depth discussion on the pitfalls of reuse, take a look at The Fallacy of Reuse by Udi Dahan.

Bounded contexts provide encapsulation much like methods and objects do, but at a higher architectural level and should therefore be a principal top-level architectural concern. Structured programming provided basic abstraction mechanisms to assist in organizing code and reasoning about code. Object orientation continued this paradigm by allowing the packaging of code and data into objects. Similarly, the UNIX philosophy emphasized small and well defined modules within a interconnected ecosystem. SOA purported to address the need for even higher levels of organization with services. The unifying theme is that of encapsulation and bounded contexts are no different.

A distinguishing characteristic of bounded contexts, however, is their alignment with the domain instead of a technical axis. And this is quite becoming since all code is designed to solve problems in some domain. Thus bounded contexts emerge as a high level abstraction mechanism. Bounded contexts are not artifacts of programming languages or frameworks, they are artifacts of the very essence of how humans think. Indeed part of the SOA vision was alignment between business and IT. However, this was somehow lost amidst an overzealous fixation on the technology, the XSD schemas, the governance guidelines. Bounded contexts and Domain-Driven design shift the focus back on the domain.

The importance of isolating appropriate bounded contexts in a domain cannot be overstated. Domain boundaries will determine characteristics of the model, but also delimit consistency boundaries and govern solution/project structure in the IDE. Few developers will argue against smaller and more focused solutions, projects, and classes.

Designing Bounded Contexts

To shamelessly re-quote SICP:

The acts of the mind, wherein it exerts its power over simple ideas, are chiefly these three: 1. Combining several simple ideas into one compound one, and thus all complex ideas are made. 2. The second is bringing two ideas, whether simple or complex, together, and setting them by one another so as to take a view of them at once, without uniting them into one, by which it gets all its ideas of relations. 3. The third is separating them from all other ideas that accompany them in their real existence: this is called abstraction, and thus all its general ideas are made.

John Locke, An Essay Concerning Human Understanding (1690)

Proper delineation of boundaries is not an exact science and follows an organic progression through iterations and continuous integration just as the rest of the development process. Developers can apply GRASP guidelines to the design of bounded contexts, especially concepts of low coupling, high cohesion and the information expert pattern. The dependency-inversion principle states Abstractions should not depend upon details. Details should depend upon abstractions.. This is yet another variation on the central theme of aligning code artifacts around the domain.

Unfortunately, these guidelines are excessively declarative and provide little insight into the how. Much like the mathematical statement that the square root of 2 is a number wich when squared equals 2 provides little insight into Newton’s method. Therefore, the structure of a bounded context must be distilled from interactions with subject matter experts, from refinement of the ubiquitous language and a process of continuous adaptation. From a software engineering perspective, developers and architects should also take care to make the cost of creating new bounded contexts as low as possible to prevent friction. Friction can cause teams to avoid bounded contexts in the first place.

Summary

The bounded context is a strategic pattern that was originally devised for Domain-Driven Design but is applicable regardless of methodology and technology. It maps to the natural human instinct to partition structures into composites and should be the cornerstone of the modeling and design phase. Bounded contexts make business and IT alignment explicit and relieved of technical concerns. In this way, they reinvigorate the original SOA vision. However, patterns for designing bounded contexts are still in their infancy.

Resources