Domain-Driven Design and Category Theory

Domain-driven design is an approach to system design and development that emphasizes understanding and modeling the domain (problem space) rather than focusing on technical implementation. It helps bridging the gap between business requirements and implementation architecture by creating a common language for communication and shared understanding between domain experts (stakeholders and core competencies) and (AI and IT) developers.

Bounded context represent specific parts of a system where a particular model applies. Each bounded context has its own language, rules and understanding of the domain. Context mapping entails defining and managing relationships between different bounded contexts within a complex system. Context mapping patterns help establish coherent communication and alignment when different bounded contexts interact and ensure that different parts of a system can collaborate effectively while maintaining their own integrity (truth maintenance). Common patterns include:

shared kernel sharing of subsets of domain models between contexts
client-server one context provides data or services to another
conforming one context adapts to the model of another
truth maintenance protecting contexts from the influence of another by translating between their models
open host service providing generic services that other contexts can use

Bounded contexts represent where a particular model reigns. Context mapping realizes explicit boundaries between bounded contexts and ensures these have the right interfaces, and provides patterns to manage interactions between bounded contexts effectively, by ensuring that the same terminology (syntax grammar) and pragmatics (utility) are shared between domain experts and technical teams.

At the design level, domain-driven models are systems using entities, values and functions (transformations). Functions move entities / values through the system. A transformation from an entity X to an entity Y and then from Y to an entity Z implicitly results in a transformation from X to Z. This concept of transformation hiding allows to design systems more accurately and identify gaps. Category theory provides a similar structure, of objects (entities) and morphisms (functions), equipped with the notion of composability, core to category theory and resting on the key concepts associativity and duality.

At a high level of abstraction, category theory provides a general framework for understanding structures and relationships, in terms of morphisms, functors and adjunctions. Context mapping is similar to functors. Functors are mappings between categories and allow relating different structures (categories of objects and their associated transformations or morphisms), whilst preserving structure and relationships. Both context mapping and functors deal with aligning different parts of a system (bounded contexts respectively categories).

Context mapping is specific to design, whereas functors are a powerful universal concept. Both contribute to understanding relationships and transformations in their respective domains. In a theory where bounded contexts are abstractly defined as categories, a functor can justly be understood as an abstraction (theory) of a context mapping (model of that theory).

All though functors are an abstraction and context mapping a technique used in design, there is an interesting parallel indeed:

functors map coherently sets of objects and morphisms between categories preserving relationships

context mapping defines limits, relationships and communication patterns between bounded contexts

There thus is an analogy.

Functors are a high level abstraction capturing how information flows between different parts of the system, whereas context mapping ensures that bounded contexts interact effectively, by defining their communication channels. Functors and context mapping serve different purposes, one abstract and the other practical, yet both contribute basically to understanding relationships and transformations.

Adjunctions are a good model for context mapping.

In the realm of category theory, an adjunction connects two categories through a pair of functors (an adjunction consists of two functors, a left adjoint and a right adjoint). These functors establish a relationship between objects and morphisms in different categories. Moreover, the adjunction unit and counit provide a way to move between the functors. Adjunctions thus generalize the notion of equivalence and capture the idea of left or right inverses.

In domain-driven design context mapping deals with managing relationships between different bounded contexts within a system. Bounded contexts represent specific parts of the system where a particular model applies and context mapping ensures clear communication and alignment between these contexts. Patterns help define interaction boundaries.

While adjunctions are an abstraction, they share similarities with context mapping.

duality adjunctions involve dual functors, context mapping establishes connections between bounded contexts
data flow adjunctions allow data to flow between categories, context mapping ensures effective data flow between bounded contexts
generalization of equivalence an adjoint functor need not be invertible yet has a left or right inverse; similarly, context mapping bridges gaps without requiring equivalence

Adjunctions provide a theoretical framework for understanding relationships. Context mapping provides practical guidelines for managing interactions. Conceptualizing adjunctions as models for context mappings is insightful, especially when designing complex systems. Both adjunctions and context mapping contribute to understanding relationships and transformations.

Main thesis:

understanding is based on adjunctions and context mappings are a model of adjunctions