Wednesday, 21 February 2007

Repository Pattern in lieu of DAO Pattern

Domain Driven Design - Inject Repositories, not DAOs in Domain Entities

Instead of the usual domain-DAO-database, a Repository is added so that it becomes domain-Repository-DAO-database.

The Repository contains domain-centric methods and uses the DAO to interact with the database.

Domain Driven Design : Use ORM backed Repository for Transparent Data Access

With Hibernate or JPA taking care of persistence and simplifying the DAO, the Repository can interact directly with the ORM and drop the DAO. So it becomes domain-Repository-Hibernate/JPA.

The link gives a general implementation of the Repository, which adopts a Bridge pattern to separate the abstraction (which will be exposed to the client code) and the implementation (which can swapped between Hibernate or JPA). It shows how the client Repository can extend from the general implementation.

The design is attractive, with the general implementation available to be used in any project. However it requires more thought to use it and and the unfamiliarity requires that more groundwork needs to be done before one can be proficient in it.

No comments: