In Domain-Driven Design (DDD), a bounded context is a boundary within which a particular domain model exists and is valid. Bounded contexts help to define the scope of a domain model and establish a clear understanding of the language used within that context.
Bounded contexts should be independent in their own context, but they may still need to interact with other bounded contexts to fulfill their own responsibilities. While they are independent in terms of their models, they may need to communicate with other bounded contexts to exchange information or collaborate on certain tasks. So bounded contexts can have relationships with each other. These relationships are important because they help to define the interactions between different parts of a system, as well as establish boundaries and responsibilities between different teams working on the same system. There are several types of bounded context relationships, including:
- Partnership: This is a relationship where two or more bounded contexts collaborate and share information. A partnership can be achieved through an integration event or by invoking a service.
- Shared Kernel: In this relationship, two bounded contexts share a common model, code or database schema. The shared components should be stable, mature and agreed upon by both teams. Changes made to shared components need to be carefully coordinated to ensure they do not break the other context’s functionality.
- Customer-Supplier: This is a relationship where one bounded context provides services or data to another. The customer context relies on the supplier context for certain capabilities or data. Changes to the supplier context can impact the customer context, so it is important to manage this relationship carefully.
- Conformist: This relationship exists when one bounded context follows the same ubiquitous language and concepts as another bounded context. This ensures that communication between the two contexts is clear and unambiguous.
- Anticorruption Layer: This is a relationship where a bounded context uses a layer to translate between its own language and that of another bounded context. This allows the two contexts to communicate even if they have different vocabularies or models.
- Open Host Service: This relationship occurs when a bounded context exposes an open, public API that can be used by other contexts. This allows other contexts to leverage the functionality of the host context in a standardized way.
- Published Language: In this relationship, a bounded context publishes its vocabulary and model for other contexts to use. This is useful when multiple contexts need to collaborate but do not want to integrate directly. The published language ensures that the meaning of terms is consistent across all contexts.
- Separate Ways: This relationship refers to the situation where two or more bounded contexts no longer have a relationship with each other, and the teams responsible for them choose to work independently.