Repository or DAO?

A few months ago, while considering the design of a new project…the issue occuping my thoughts was in the differences in possible design technologies. Two directions: one a relational database, two an object model. Why was it an issue? How are the designs kept in sync? Where does a developer turn to first if they want to make a change to the core design of an application that is expressed in many places (at least the relational database and the objects)?

So, I considered the idea of a ‘domain model’, turned it around a few times and thought it could be a good thing. Not only would it be the common language between various technical areas, if it was expressed in business terms, the testers and business people could also be involved in a discussion around it, or use it. Convinced this was something good, I Googled ‘domain model’ and found that 1. yes it was a good idea and 2. someone had written a book about it. I ordered Domain Driven Design by Eric Evans and have been reading through it.

Good book (pity it goes for the fashionable ‘xxx driven xxx’ title, which also sounds confusingly similar to Model Driven Development). It has clarified many things for me, and supported other thoughts, like the ‘Ubiquitous Language’ pattern… “A project faces serious problems when its language is fractured.”

Many standard patterns for domain components are listed and discussed in the book. One pattern is called a Repository. It troubled me because elsewhere in the world of development there is a DAO (Data Access Object) pattern. Why was I concerned? Because the two patterns initially seemed to be the same, which should be used? This is what I understand now.

Repository Responsibilities

  • Provide a common language to all team members (including business representatives to whom DAO would need constant explanation)
  • Provide a higher level of data manipulation – something that may be common to the data regardless of how it is retrieved.
  • Provide a mechanism to manage existing entities (where a Factory might create you a new one).

DAO Responsibilities

  • Provide data access. Perhaps you could say it is about persistence strategies, underneath a DAO interface there is one or more implementations – eg JDBC, Hibernate.

Example: the domain model might represent plants, one entity may be a Tree which has an associated TreeRepository.


Implementation 1:

There is a database table in the background with TREE_ID, LATITUDE, LONGITUDE, HEIGHT columns. Perhaps we code TreeRepository as an interface which a JDBC DAO implements.

Implementation 2:

We now access to a location Service which stores the location of things. We also have a database table with TREEID, LOCATIONID, HEIGHT columns. Perhaps we implement a TreeRepository that accesses both the location service and JDBC DAO to construct a full Tree object.

Facebooktwitterredditpinterestlinkedinmailby feather

6 thoughts on “Repository or DAO?

  1. Koert

    I think the comment “including business representatives to whom DAO would need constant explanation” is the most compelling reason to use Repository instead of DAO. Otherwise, I see little difference between them.

    You could just use Hibernate directly in the Repository, since it provides an abstraction to the persistence anyway. I think it’s theoretically a good idea to layer the persistence in a DAO layer so you can replace it easily, but it requires more work and costs.

    I would prefer to use Location as parameter to find: find(Location location).

  2. Pingback: JAOO Brisbane: Goldilocks and the Concurrent Processes at A Few More Words

  3. Pingback: ?????????????? – CodingBlog

  4. Pingback: dao - DAO, Repositorios y Servicios en DDD

Leave a Reply

Your email address will not be published. Required fields are marked *