Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Across your company, mapping between layers (entity, domain, DTO, event) accounts for a large share of code and bugs. What architectural guidance would you give to reduce mapping cost without coupling layers?

My guidance is to map only where a real boundary needs protecting, such as a public API or an event contract, and to stop creating layers of near-identical classes for their own sake. Where mapping is mechanical, generate it with MapStruct under strict policies; keep the contracts at real boundaries versioned and tested so mapping changes are deliberate; and inside a small service, let the domain model travel further instead of copying it. We decide where to change course by measuring the defects and change effort that mapping actually causes.

Why this question is asked

In many enterprise Java codebases, a single new field means editing an entity, a domain object, a DTO, an event, and four mappers between them. Each copy is a chance to forget something, and the total cost across a company is large. But the obvious fix, sharing one class everywhere, couples the database to the public API and makes every change risky in a different way. Interviewers ask this leadership question to see whether we can give guidance that reduces cost without destroying the boundaries that layers were meant to protect. In this answer, we will see where mapping cost comes from, separate boundaries that matter from those that do not, look at generation and contracts, and see how to measure the result.

1Where the Mapping Cost Comes From

Layered architectures introduce one model per layer: a JPA entity for persistence, a domain object for business rules, a DTO for the API and an event class for messaging. The intention is good. Each model can change for its own reasons, and a column rename should not break a mobile client. But when the models are nearly identical, the separation buys little and the copying costs a lot.

The typical bugs are quiet ones. A field is added to the entity and the DTO but not to the mapper, so the API returns null. A mapper copies a field in one direction but not back. An event omits a field that a consumer later needs, and nobody notices because the event class was copied from the DTO. These are not hard bugs, but they are frequent, and each layer multiplies them.

Step throughFour models for one order
  1. OrderEventcontract with other teams: versioned, long-lived
  2. OrderDtocontract with API clients: versioned
  3. Order (domain)business rules, internal
  4. OrderEntitydatabase mapping, internal

4 layers, from the top down. Step through them one at a time.

Only the top two are contracts others depend on; the bottom two are internal choices.

2Add a Layer Only Where It Protects a Real Boundary

The first and most effective guidance is to ask, for each model, which boundary it protects. A boundary is real when someone outside the team depends on it and cannot change in step with us: API clients, other services consuming our events, or partners reading a file format. Those deserve their own types, because their stability matters more than our convenience. The public API DTO and the event schema are such contracts.

Inside the service, the boundaries are often imaginary. If the same team owns the entity and the domain model, deploys them together and has no plan to change the database independently, a separate domain class that mirrors the entity field by field mostly adds mapping. In a small service, using one model internally, the entity with its business methods or a domain record mapped directly by the persistence layer, is simpler and perfectly defensible. In a large, complex domain, a separate domain model may earn its place, but that should be a decision, not a template default.

3Generate the Mechanical Mappings, With Strict Policies

Where mapping remains and is mostly field copying, people should not write it by hand. MapStruct generates the code at compile time, and with unmappedTargetPolicy set to ERROR a forgotten field becomes a build failure instead of a production bug. That single setting removes the most common class of mapping defects.

Java
@MapperConfig(componentModel = "spring",
unmappedTargetPolicy = ReportingPolicy.ERROR, // forgotten field fails the build
unmappedSourcePolicy = ReportingPolicy.WARN) // unused source fields are visible
public interface PlatformMapperConfig {}
@Mapper(config = PlatformMapperConfig.class)
interface OrderEventMapper {
@Mapping(target = "schemaVersion", constant = "3")
OrderPlacedV3 toEvent(Order order);
}
// Adding 'discountCode' to OrderPlacedV3 without a source:
// error: Unmapped target property: "discountCode".

Notice that the strictness is a platform decision, published once in a shared configuration that every team uses. That is what guidance means at company scale: not a wiki page asking people to be careful, but a default that makes the careful behaviour automatic. Logic that is not mechanical, such as deriving a status or applying permissions, belongs in named, tested methods rather than in mapping annotations.

4Keep Contracts Versioned and Tested

At real boundaries, the risk is not the copying itself but changing the contract by accident. If OrderDto is generated from the entity, or shares a base class with it, a refactoring of the database can silently change the API. So contracts should be explicit types, ideally defined schema-first (OpenAPI for HTTP, Avro or JSON Schema for events), with a version and compatibility rules.

Then we test the contract, not the mapper. Consumer-driven contract tests or schema compatibility checks in the build fail when a change would break a consumer. With that safety net, a mapping change becomes a deliberate act: the developer sees which contract changes, decides whether it is compatible, and bumps the version if not.

Step throughChanging a field that crosses a boundary
  1. Change the internal modelentity or domain
  2. Does a contract need the change?
    YesEdit the schema, check compatibilityNoMapper absorbs it, contract unchanged
  3. Compatibility check passes?
    YesRegenerate mapper, build verifies fieldsNoNew version or redesign
  4. Contract tests with consumersrun in CI

4 steps in order. Play them, or step through them one at a time.

Internal changes stop at the mapper; contract changes are explicit and checked.

5Measure Before and After

Guidance at this level should be driven by data, because opinions about layering are strong and evidence is rare. Two measures are practical. First, defects: tag incidents and bugs whose root cause was mapping, such as a missing field, a wrong conversion or a stale event, and count them per quarter. Second, change effort: for a sample of features, count how many files changed only to carry a field through layers. A feature that touches eight files to add one attribute is a visible signal.

With those numbers, we can show whether the guidance works. If teams that dropped an internal layer and adopted strict generated mappers see fewer mapping defects and smaller changes, others will follow because it is visibly better, not because a standard says so. If a team's complex domain really needs its separate model, the data will show that too.

6Follow-up questions

Does using the entity in more places not leak persistence concerns?

Inside the service it can, which is why the rule is limited to internal use in small services. The entity must still never be returned from a controller or published as an event, because that turns the database schema into a public contract and invites lazy-loading problems during serialisation.

How do you stop teams from simply copying the DTO into the event?

By making the event schema its own reviewed artifact, defined schema-first and owned with its consumers. When the event is generated from a schema in a registry, copying the DTO is no longer the path of least resistance, and the compatibility check catches accidental changes.

Is hexagonal architecture incompatible with this guidance?

No. Hexagonal architecture places ports at the boundaries, and the guidance agrees that real boundaries need their own types. It only warns against creating a separate type for every port when the data on both sides is identical and owned by the same team.

7Conclusion

In this answer, we saw that mapping cost comes from multiplying near-identical models across layers. The guidance is to add a layer only where it protects a real boundary such as a public API or event contract, to generate mechanical mappings with MapStruct under a strict platform-wide policy, to keep contracts versioned, schema-first and tested so changes are deliberate, to let the domain model travel further inside small services, and to measure mapping defects and change effort to steer the decisions.

What to say in the interviewAbout 80 seconds aloud

Open with

My first piece of guidance would be that every model must justify itself by the boundary it protects. A public API DTO and an event schema protect real boundaries, because other teams depend on them and cannot change in step with us, so they deserve their own versioned types.

Then, if they want more

Inside a small service, a separate domain class that mirrors the entity field by field usually protects nothing, so I would let the domain model or entity travel further internally, while never returning entities from controllers or publishing them as events.

Second, where mapping stays and is mechanical, nobody should write it by hand: MapStruct with a shared platform config that sets unmappedTargetPolicy to ERROR, so a forgotten field breaks the build instead of production. Real logic goes into named, tested methods.

Third, contracts are defined schema-first, OpenAPI or Avro, with compatibility checks and contract tests in CI, so a mapping change that alters a contract is deliberate and visible. And fourth, I would measure: tag defects whose cause was mapping and count how many files a typical feature touches just to carry a field through layers.

Those numbers tell us where the guidance is working and stop the discussion from being about taste.

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 "Across your company, mapping between layers (entity, domain, DTO, event) accounts for a...": https://scrivio-interview-library.netlify.app/java/utility-libraries/d3ef075cfa/