SOLID & Design Patterns Interview Question #113
Why should API models often be separated from domain entities?
Architectural Patterns, Application Boundaries & Distributed Design Patterns Senior Advanced
Quick Interview Answer
API models are external contracts, while domain entities represent internal business concepts and invariants. Using one type for both can couple API versioning and serialization concerns directly to the domain.
Detailed Explanation
API models are external contracts, while domain entities represent internal business concepts and invariants. Using one type for both can couple API versioning and serialization concerns directly to the domain.
Separate request and response models also help control writable fields, validation, and data exposure. For simple internal CRUD systems, direct mapping may sometimes be acceptable, but the tradeoff should be deliberate.
Code Example
public record CreateCustomerRequest(
string Name,
string Email);
// Map request -> domain object.
// Do not expose every Customer property
// automatically to external callers.