Mark Needham

Thoughts on Software Development

DDD: Repository pattern

with 4 comments

The Repository pattern from Domain Driven Design is one of the cleanest ways I have come across for separating our domain objects from their persistence mechanism.

Until recently every single implementation I had seen of this pattern involved directly using a database as the persistence mechanism with the repository acting as a wrapper around the Object Relational Mapper (Hibernate/NHibernate).

Now I consider there to be two parts to the repository pattern:

  1. The abstraction of the persistence mechanism away from our other code by virtue of the creation of repositories which can be interacted with to save, update and load domain objects.
  2. The need for these repositories to only be available for aggregate roots in our domain and not for every single domain object. Access to other objects would be via the aggregate root which we could retrieve from one of the repositories.

This pattern can also be useful when we retrieve and store data via services which we have been doing recently. Of course eventually the data is stored in a database but much further up stream.

To start with we were doing that directly from our controllers but it became clear that although we weren't interacting directly with a database the repository pattern would still probably be applicable.

The way we use it is pretty much the same as you would if it was abstracting an ORM:

repository.gif

I think with an ORM the mapping would be done before you got the data back so that's an implementation detail that is slightly different but as far as I can see the concept is the same.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • HackerNews
  • StumbleUpon
  • Twitter

Written by Mark Needham

March 10th, 2009 at 10:31 am

4 Responses to 'DDD: Repository pattern'

Subscribe to comments with RSS or TrackBack to 'DDD: Repository pattern'.

  1. DDD: Repository pattern – Mark Needham…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

    DotNetShoutout

    10 Mar 09 at 11:38 am

  2. [...] I mentioned in a previous post we have been using the repository pattern to achieve this in our [...]

  3. [...] interact with the other team's service and isolate all that code into one area, possibly behind a repository. The benefit here is that we can isolate all changes in the supplier's model in one place which [...]

  4. [...] changed our code to follow this approach and created repositories and mappers which were the main places in our code base where we cared about this external [...]

Leave a Reply