Monday, November 18, 2013

Repositories vs. DAOs

Dealing with DDD for some time now, I asked myself the following question:

What is the difference between a Repository and a DAO (Data Access Object) ?

The reason for the question was that in many repositories (real world projects and tutorials on the web) I could not see much difference to good old DAOs.

Let's say you we have a functionality which is grabbing customers out of your database - seems to be modern to call stuff like this "CustomerRepository" - I personally doubt that this is what DDD is aiming at.
I started searching on the web but it was not so easy to find a precise answers but finally I stumbled over a very good blog post with illustrative examples. You definitely should check this out. I just would like to abstract it by opposing different aspects of repositories and DAOs:



Repository DAO
"business interface" speaking ubiquitous domain language "technical interface" contracting between data source and OO application
Close to domain Close to data source
typically one per aggregate root typically one per database table (or web service operation)
containing one or multiple DAOs used by a repository
interfaces in domain layer interfaces in infrastructure layer
parameters of interface methods are domain types parameters of interface methods are reflecting the data source
implementations in infrastructure layer (lots of technical plumbing) implementations in infrastructure layer (purely technical)