Skip to main content

Context Mapping: How Contexts Relate

Context Mapping is the practice of identifying relationships between bounded contexts and choosing appropriate integration patterns.

TL;DR

PatternRelationshipWhen to Use
PartnershipTeams collaborate closelyCo-evolving contexts
Shared KernelShared code/modelTightly coupled teams
Customer-SupplierUpstream serves downstreamClear dependency
ConformistDownstream adopts upstream modelNo negotiating power
ACLTranslation layerProtect from external models
OHSPublished API/protocolMany consumers
Published LanguageStandard interchange formatIndustry standards
Separate WaysNo integrationIndependent 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