Jul 27

Repository or DAO?

Tag: warren @ 2:03 pm

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.

TreeDomainModel

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.

2 Responses to “Repository or DAO?”

  1. Koert says:

    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. JAOO Brisbane: Goldilocks and the Concurrent Processes at A Few More Words says:

    [...] complexity. Getting your design just right is a primarily a craft. It may be helped by things like Domain Driven Design or reducing irreversible architecture choices by removing them or delaying them. Software [...]

Leave a Reply