Repository pattern VS DAO pattern

A DAO is much closer to the underlying storage, it’s really data-centric. That’s why in many cases you’ll have DAOs matching db tables or views 1 on 1. A DAO allows for a simpler way to get data from storage, hiding the ugly queries. But the important part is that they return data as in object state.

A repository sits at a higher level. It deals with data too and hides queries and all that but, a repository deals with** business/domain objects**. That’s the difference. A repository will use a DAO to get the data from the storage and uses that data to restore a business object. Or it will take a business object and extract the data that will be persisted. If you have an anaemic domain, the repository will be just a DAO.
A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes. Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. The repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers.
In a nutshell, a Repository:
• Is not a data access layer
• Provides a higher level of data manipulation
• Is persistence ignorance
• Is a collection of aggregate roots

Toufiq Mahmud

Leave a Reply

Your email address will not be published.