Context Mapping: How Contexts Relate
Context Mapping is the practice of identifying relationships between bounded contexts and choosing appropriate integration patterns.
TL;DR
| Pattern | Relationship | When to Use |
|---|---|---|
| Partnership | Teams collaborate closely | Co-evolving contexts |
| Shared Kernel | Shared code/model | Tightly coupled teams |
| Customer-Supplier | Upstream serves downstream | Clear dependency |
| Conformist | Downstream adopts upstream model | No negotiating power |
| ACL | Translation layer | Protect from external models |
| OHS | Published API/protocol | Many consumers |
| Published Language | Standard interchange format | Industry standards |
| Separate Ways | No integration | Independent contexts |
Key Patterns
Anti-Corruption Layer (ACL)
Protect your domain from messy external models:
// External legacy system (messy)
public class LegacyCustomerRecord
{
public string CUST_ID { get; set; }
public string CUST_NM { get; set; }
public int CUST_TYP_CD { get; set; }
}
// ACL Translator
public class CustomerTranslator
{
public Customer Translate(LegacyCustomerRecord legacy)
{
return new Customer(
id: CustomerId.From(legacy.CUST_ID),
name: new CustomerName(legacy.CUST_NM),
type: TranslateType(legacy.CUST_TYP_CD)
);
}
}
Customer-Supplier
Upstream serves downstream with negotiating power:
// Downstream consumes Upstream API
public class InventoryClient
{
public async Task<StockAvailability> CheckStock(ProductId productId)
{
var response = await _httpClient.GetAsync($"/api/stock/{productId}");
return await response.Content.ReadFromJsonAsync<StockAvailability>();
}
}
Open Host Service (OHS)
Published API for many consumers:
[ApiController]
[Route("api/v1/users")]
public class UsersController : ControllerBase
{
[HttpGet("{userId}")]
public async Task<ActionResult<UserDto>> GetUser(Guid userId)
{
// Published API for all consumers
var user = await _userService.GetById(userId);
return Ok(new UserDto { Id = user.Id, Email = user.Email });
}
}
Choosing the Right Pattern
Do you control the upstream?
├── NO → Conformist or ACL
│ └── Is upstream model acceptable?
│ ├── YES → Conformist
│ └── NO → ACL
│
└── YES → Partnership, Customer-Supplier, or OHS
└── How many consumers?
├── One → Customer-Supplier
├── Many → OHS
└── Mutual dependency → Partnership