find a location for property in a new city

Thursday 14 October 2010

How to set up the Repository pattern using Unity

Recently moved from Spring.NET to Unity 2.0 over here in the London based We were already using the Repository pattern and Unit of Work pattern with Entity Framework. Now all there is to do is scrap Spring.NET and bring in Unity. Easy right?

I actually found it very difficult to plagiarise this work off some documentation so I thought I'd help the cause by spilling how I managed it. I'm not going to talk about how to set up the Repository pattern, Unit of Work pattern, or Unity in general. Just how to make a delicious combination of all three.

In my inventory of classes to wire up I have:
  • ContentEntities - this is my ObjectContext
  • UnitOfWork - this is my implementation of IUnitOfWork (Google that for guidance)
  • EntityRepostory<T> - this is my implementation of IRepository<T> where T:class (again for guidance on this pattern Google it)
  • ContentService - This has a dependency property for the ContentRepository of type IRepository<Content>

To wire these all up for the RepositoryPattern to be correctly implemented so that the UnitOfWork is reused correctly I used the following code in my Application_Start() method:
//new container
var container = new UnityContainer();

//set up the unit of works to use the singleton pattern (AKA ContainerControlledLifetimeManager)
container.RegisterType<IUnitOfWork, UnitOfWork>("contentUow", new ContainerControlledLifetimeManager(), new InjectionConstructor(new SocialEntities()));
//get the instance (i have named it to disambiguate in the event of having multiple UOWs (quite likely))
var contentUow = container.Resolve<IUnitOfWork>("contentUow");

//now we have the reference start up a new container
container = new UnityContainer()
//content repository (any other repositories within the model can use this same contentUow instance)
.RegisterType<IRepository<Content>, EntityRepository<Content>>(new InjectionConstructor(contentUow))

Note I used the ContainerControlledLifetimeManager for the UnitOfWork. There are more for you to choose from, I decided from the description that this is the best for me. Check here for a full list of LifetimeManagers.

This is interesting reading if you are interested in what happens when you ignore the UnitOfWork part of the Repository pattern. Bad things!

Follow britishdev on Twitter


  1. any chance you could post your full implementation of IUnitOfWork with unity and EF?

    big ask I guess with work code, maybe a "personal" implementation?

  2. I find generic interfaces to be a double-edge sword. Sure, the generic interface allows you to write less code, but at what cost? Every repository would need to implement that generic interface whether or not you actually need it. For example, what if your repository was read-only? Well, you can just throw a NotImplementedException for those operations not used, but wouldn't the caller need to know that? Now the caller would need to handle such cases. One can really paint themselves into a essay writers

  3. Excellent read, Positive site, where did u come up with the information on this posting? I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work,If anyone interested similar one's have a look here thanks.

    shih tzu puppies for sale near me
    buy puppy online