What is the difference between Data Access Objects (DAO) and Repository patterns? I am developing an application using Enterprise Java Beans (EJB3), Hibernate ORM as infrastructure, and Domain-Driven Design (DDD) and Test-Driven Development (TDD) as design techniques.
Hibernate – the difference between DAO and Repository patterns
data-access-layerdomain-driven-designejb-3.0hibernaterepository-pattern
Best Answer
DAO
is an abstraction of data persistence.Repository
is an abstraction of a collection of objects.DAO
would be considered closer to the database, often table-centric.Repository
would be considered closer to the Domain, dealing only in Aggregate Roots.Repository
could be implemented usingDAO
's, but you wouldn't do the opposite.Also, a
Repository
is generally a narrower interface. It should be simply a collection of objects, with aGet(id)
,Find(ISpecification)
,Add(Entity)
.A method like
Update
is appropriate on aDAO
, but not aRepository
- when using aRepository
, changes to entities would usually be tracked by separate UnitOfWork.It does seem common to see implementations called a
Repository
that is really more of aDAO
, and hence I think there is some confusion about the difference between them.