Repaso
EspaƱol
Design & PracticesIntermediateDP-52

What is the difference between a DAO and a Repository?

Options

  1. They are identical in all cases and can always replace each other
  2. A DAO is table-oriented; a Repository is domain-oriented (Correct option)
  3. DAO is a web framework and Repository is a type of database
  4. Repository only works with NoSQL databases, never with SQL ones

Answer

A DAO is close to the database tables (CRUD per table); a Repository is closer to the domain, behaving like an in-memory collection of aggregates

Why

DAO (Data Access Object): a class that hides the persistence details of one table or entity: UserDao.insert, findById.

Repository (from Domain-Driven Design): a collection-like interface for domain objects: users.add(user), users.findByEmail(e); it may combine several tables and speaks the language of the domain.

In practice the two terms overlap a lot. Spring Data's JpaRepository is a repository abstraction on top of JPA.

Remember

DAO = table-oriented. Repository = domain-oriented collection.

Key terms

Practice this question in Repaso