Bounded Contexts define explicit boundaries where a particular domain model applies, with its own ubiquitous language.
Core Idea
- Each context has clear boundaries
- Same term can mean different things in different contexts
- Each context has its own model
- Explicit translation at context boundaries
Examples
E-Commerce System
Sales Context:
// Customer in Sales context
public class Customer
{
public CustomerId Id { get; }
public string Name { get; }
public Email Email { get; }
public ShippingAddress PreferredAddress { get; }
public List<Order> OrderHistory { get; }
}Inventory Context:
// Customer in Inventory context (different model!)
public class Customer
{
public CustomerId Id { get; } // Same ID
// Only what Inventory needs to know
public bool IsVIP { get; }
public int PriorityLevel { get; }
}Shipping Context:
// Customer in Shipping context
public class Recipient // Different name!
{
public string Name { get; }
public Address DeliveryAddress { get; }
public PhoneNumber Contact { get; }
}Context Boundaries
What Belongs in Same Context
- Shares same language
- Changes together
- Has consistent rules
- Single team ownership
What Belongs in Different Contexts
- Different language for same terms
- Independent change cycles
- Different business rules
- Different team ownership
Context Mapping
Shared Kernel
// Shared types between contexts
public class CustomerId // Shared
{
public Guid Value { get; }
}
public class Money // Shared
{
public decimal Amount { get; }
public Currency Currency { get; }
}Anti-Corruption Layer
// Translate from external context
public class LegacyOrderAdapter
{
public Order ToModernOrder(LegacyOrderDto legacyOrder)
{
// Translate legacy model to our context's model
return new Order(
new OrderId(legacyOrder.order_id),
new CustomerId(legacyOrder.cust_id),
// ... translation logic
);
}
}Benefits
- Prevents model confusion
- Team autonomy
- Independent evolution
- Clear integration points
Related Concepts
- Ubiquitous Language - Per-context language
- Microservices - Often align with bounded contexts
- Domain Driven Design - Overall pattern context