Quick revision
The short answer
A mapping layer is justified only where two models differ for a real reason, such as external API stability, domain rules or persistence constraints; otherwise it is ceremony. So simple CRUD-like services can use an API DTO plus an entity, or records, while full hexagonal separation is reserved for complex domains; read paths use projections and read models that skip domain objects; mechanical mappings are generated with MapStruct or OpenAPI-generated DTOs and the remaining hand-written ones are tested. We write this down as decision criteria with examples and review a sample of services after a few months against change cost and defect data.
What to remember
In this answer, we decided how many models and mappings a service should have. A mapping is justified only where models differ for a real reason, such as API stability, domain rules or persistence constraints, and version history shows whether a boundary absorbs change. Simple services use an API DTO plus entity, complex domains keep full separation, read paths go straight to read models, mechanical mappings are generated with MapStruct or OpenAPI, and the remaining hand-written ones are tested. The guideline is written as criteria with examples and checked after a few months against change cost and defect data.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A mapping layer is justified only where two models differ for a real reason, such as external API stability, domain rules or persistence constraints; otherwise it is ceremony. So simple CRUD-like services can use an API DTO plus an entity, or records, while full hexagonal separation is reserved for complex domains; read paths use projections and read models that skip domain objects; mechanical mappings are generated with MapStruct or OpenAPI-generated DTOs and the remaining hand-written ones are tested. We write this down as decision criteria with examples and review a sample of services after a few months against change cost and defect data.
Why this question is asked
When mapping code is a third of a codebase, something has gone wrong, but the fix is not to remove all mappings either. Each model boundary exists to let one side change without the other, and in some services that protection is worth a lot. Interviewers ask this design question to see whether we can turn a vague complaint into criteria: when a separate model earns its cost, when it does not, and how to tell afterwards whether we chose well. In this answer, we will look at what each of the four models is for, decide when a boundary is real, offer simpler shapes for simple services, separate reads from writes, automate what is mechanical, and write and check the guideline.
1What Each of the Four Models Is For
The four steps in the question each have an intended purpose. The API DTO is the public contract; it must stay stable for clients while the service evolves. The command is the input to a use case, independent of how it arrived, so the same use case can be called from HTTP, a message or a test. The domain object holds business rules and protects its own invariants. The entity maps to the database, shaped by persistence needs such as JPA's requirements, indexes and denormalisation.
Each separation lets one model change without the others. The question is whether, for a given service, they ever actually change separately. If the four models have the same fields and change together in every commit, the mappings are copying data and protecting nothing.
| Boundary | Real reason to keep it | Ceremony when |
|---|---|---|
| API DTO <-> command | several entry points: HTTP, Kafka, batch | one controller, same fields |
| Command <-> domain | rich rules, invariants to protect | domain only holds data |
| Domain <-> entity | persistence shape differs, long-lived domain | same fields, simple CRUD |
| Entity <-> API DTO | always: API must not expose the schema | never skip this one |
2Deciding When a Boundary Is Real
A boundary is real when the two sides change for different reasons or at different rates. Version history tells us: if the API DTO changed 3 times in a year while the entity changed 20 times as the schema evolved, the mapping between them is absorbing change and earning its keep. If every commit touching the domain object also touches the entity and the command with the same edit, those boundaries are not absorbing anything.
The kind of logic matters too. If a service has pricing rules, state transitions or validation across several fields, a domain model separate from persistence keeps those rules testable without a database. If the service mostly stores and returns records, there are no rules to protect, and a domain layer is an extra copy of the entity.
3Simpler Shapes for Simple Services
So the guideline offers shapes, not one architecture. For CRUD-like services, the default is an API DTO plus a JPA entity, with the service mapping between them; often the DTOs are records with a static from method. That is one mapping in each direction instead of three. For services with complex domains or many integrations, full hexagonal separation with commands, domain objects and persistence entities is allowed and expected.
One rule applies to every shape: entities never appear in the API. That boundary always has a real reason, because clients must not be coupled to the schema or trigger lazy loading during serialisation.
4Read Paths Skip the Domain Entirely
A large share of mapping code usually sits on read paths: load the entity, map it to a domain object, map that to a DTO, just to show a list. Reads do not change state, so they do not need the domain model's rules. We let queries return read models directly, for example with a JPQL constructor expression, a Spring Data projection or a jOOQ or JDBC query into a record.
That removes two mappings per read endpoint, avoids loading full entity graphs, and usually makes the query faster because it selects only the needed columns. Writes still go through the domain model where it exists. This split between write and read models is a light form of CQRS, command query responsibility segregation, without separate databases.
- Is it a read?YesQuery straight into a read model recordNoNext question
- Complex rules or many integrations?YesAPI DTO -> command -> domain -> entityNoAPI DTO -> entity, service maps
- Mechanical mapping?YesGenerate with MapStruct or OpenAPINoHand-write and unit test it
3 steps in order. Play them, or step through them one at a time.
Most requests in a typical service take the short paths.
5Generate the Mechanical, Test the Rest
Where mappings remain, much of the code is mechanical: copy name to name, amount to amount. That should be generated, not typed. MapStruct generates mapping implementations at compile time and, with an error policy for unmapped targets, fails the build when a field is forgotten. OpenAPI Generator can produce the API DTOs from the contract, so the API model is not hand-maintained at all.
What is left are the mappings with decisions in them: currency conversion, defaulting, combining fields, translating error codes. Those are real logic and get unit tests like any other logic. After this step, the remaining hand-written mapping code is usually small and meaningful.
6Writing the Guideline and Checking It
The guideline is written as decision criteria with examples from our own services, not as a mandate: which shape to choose given the number of business rules, the number of entry points and integrations, and the expected lifetime, plus the one universal rule about entities in APIs. Each service records its choice in a short decision record.
After a few months, we review a sample of services, perhaps ten, across both shapes. We look at change cost, such as files touched and lead time per typical change, and at defect data, such as bugs caused by mapping mistakes or by business rules leaking into the wrong layer. If simple-shaped services ship faster with no rise in defects, the guideline works. If some of them grew complex rules and started hurting, we adjust the criteria or add a trigger for reviewing a service's shape.
7Follow-up questions
How would you measure whether a mapping layer pays off?
By looking at whether it absorbs change. From version history, we count how often changes on one side of the boundary were made without touching the other side: an API DTO that stayed stable through 20 entity changes shows a layer that pays off, while models that always change together show one that does not. We add the cost side, such as the share of code and of each change spent on mapping, and defect data, such as bugs from wrong mappings versus bugs prevented by keeping the API stable.
Isn't mixing shapes across services confusing?
Less than one rigid shape that fits half the services badly. With two documented shapes and examples, engineers quickly recognise which one a service uses, and the shared rules, such as no entities in APIs and feature packages, keep the codebases familiar.
What about the command object in simple services?
It usually adds nothing when there is a single controller calling the use case. The request DTO can be passed to the service, or its fields as parameters. A separate command earns its place when the same use case is triggered from several entry points with different input formats.
8Conclusion
In this answer, we decided how many models and mappings a service should have. A mapping is justified only where models differ for a real reason, such as API stability, domain rules or persistence constraints, and version history shows whether a boundary absorbs change. Simple services use an API DTO plus entity, complex domains keep full separation, read paths go straight to read models, mechanical mappings are generated with MapStruct or OpenAPI, and the remaining hand-written ones are tested. The guideline is written as criteria with examples and checked after a few months against change cost and defect data.
Open with
I would start from the purpose of each model. The API DTO protects clients from internal change, the command decouples a use case from how it is called, the domain object protects business rules, and the entity fits persistence.
Then, if they want more
A mapping is worth its cost only where the two sides actually change for different reasons, and I would check that in version history: if the DTO, command, domain object and entity always change together in the same commits, those mappings are ceremony. So I would replace the single mandated structure with criteria.
CRUD-like services use an API DTO plus an entity, often with records and a static from method, which is one mapping instead of four. Complex domains, or services with several entry points and integrations, keep the full separation. One rule stays for everyone: entities never go out through the API.
Read endpoints skip the domain entirely and query straight into read models with projections, which removes a lot of mapping and makes queries faster. Whatever mapping is mechanical gets generated, with MapStruct for object mappings and OpenAPI Generator for API DTOs, and the remaining hand-written mappings with real decisions get unit tests.
I would write all this as decision criteria with examples from our services, and after a few months review a sample of services for lead time per change and defect data to see whether the guideline helped.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Teams complain that each request passes through four mapping steps (API DTO, command...": https://scrivio-interview-library.netlify.app/java/application-architecture/e010f6a057/