To Use Repositories Or NOT

I have been using NHibernate for a while now and am happy writing test cases / mocks for Repository implementations such that my code never need hit a database. I have also used Entity Framework in the past and have a soft spot for it, and recently came across a post by the Entity Framework team saying that EF v6 is actually very testable and we can/could use it directly in our codebase.

Naturally I wanted to try this out, so I tried it out using EF6 using Repositories and directly and was pleasantly surprised with the results which I have documented in this article:

Hope that helps someone out. Enjoy



  1. Entity Framework (and most other O/RMs) already implement the repository design pattern..
    DbContext _is_ a repository.


    1. No I don’t agree, DbContext and most other ORMs are a UnitOfWork.

      I do agree they can do Repository type things too, but the reason for the Repository pattern (in my opinion) is to be able to abstract the ORM away


      1. I think that the purpose of the repository pattern is to ‘mediate between the domain and data mapping layers using a collection-like interface for accessing domain objects’
        ( )
        ‘A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection.’ – DbSets

        ‘Client objects construct query specifications declaratively…’ – IQueryable

        ‘..and submit them to Repository for satisfaction’

        Indeed, the fact that EF since recently was not easy to mock, and adding a repository on top of it helps with this, that’s another story..

      2. Now that I do agree with, we are basically agreeing here I feel so yeah all good stuff

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s