DTOs vs domain models
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
keep the API shape separate from the domain.
Planned topics
- A DTO (data transfer object) is the shape of a request or response.
- The domain model holds business data and rules; it may have fields clients must never see.
- If you serialize the domain object directly, any internal change breaks API clients.
- Use different DTOs for create, update, and response.
- Map in one place with a mapper (manual method or a library).
Draft outline details
Demonstration
record ProductResponse(long id, String name, BigDecimal price) {}
class Product { long id; String name; BigDecimal priceNet; int costCents; /* internal */ }Check your understanding
Why not return Product directly?
Show answer
It may expose internal fields and ties the API to internals.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.