Skip to content
Ameen Abozaid
Planned lessonJNR-013-L05

DTOs vs domain models

  • Lesson 5 of 8
  • Junior
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

java
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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.