DAO and DTO Patterns

A Data Access Object hides how data is stored; a Data Transfer Object carries data across a boundary and nothing else. Know what each one hides, where the mapping between them lives, and why an ORM entity should never be your API.

Overview

Two patterns with similar names solve two different problems. A Data Access Object (DAO) is the one class that knows how to read and write a kind of record in a store: it owns the SQL, the connection and the row-to-object code, so nothing else does. A Data Transfer Object (DTO) is a plain bag of fields that carries data across a boundary, such as an HTTP request or a message on a queue, and has no behavior of its own.

They show up together because a typical request crosses both boundaries. JSON arrives and becomes a request DTO; a mapper turns it into a domain entity; a DAO saves the entity; the saved entity is mapped to a response DTO, which goes back out as JSON. Each step has one job, and each object is shaped for the side it faces.

Interviewers use these patterns to test layering. They listen for three things: that you keep SQL in one place, that you never bind a request straight onto a database entity (the mass-assignment bug), and that you can say where the mapping code lives and why.

The Data Access Object

The pattern comes from Sun's Core J2EE Patterns catalog, now hosted by Oracle. Its problem statement is that storage APIs differ by product and by kind of store (relational database, LDAP directory, flat files, a partner system), and code that talks to a store directly becomes tied to it. The fix is to put all access to a data source behind a DAO, so the business code calls a simple interface and never sees the access mechanism. The catalog describes the DAO as acting like an adapter between the component and the data source.

The catalog names four participants, and each appears in the diagram under a concrete name. The BusinessObject is OrderService: it needs data and never writes SQL. The DataAccessObject is CustomerDao: it loads and stores, and hides the access code. The DataSource is Database, the store itself, which may be an RDBMS, a file or another system. The TransferObject is Customer, which carries the values between the two.

A DAO is shaped like its table: one per table or record type, with create, read, update and delete methods and finders named after columns. The catalog lists the consequences plainly: callers no longer depend on the store, a migration touches only the DAO layer, and the cost is one more layer of classes to write.

Interview questions

  • When would you choose a DAO over a Repository?
  • Why should an API not accept or return the ORM entity directly?
  • Where does the mapping between DTOs and entities live, and who calls it?
  • Should DTOs be immutable? Show why.
  • How do you change a response DTO without breaking existing clients?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio