find a location for property in a new city

Thursday 14 October 2010

Don't fuck with the repository or unit of work pattern

I am one for not taking patterns as gospel so I have a tendency to use the bits I want and discard the bits I don't. Today I discovered I don't necessarily know best.

At lovemoney.com I have just started using Unity 2.0 to replace Spring.NET as our dependency container. This has meant that I have had to reimplement some of the wiring of the dependencies. This includes our implementation of the Repository pattern for use with Entity Framework.

Now, my problem with the Repository pattern is that I have always struggled to see the point in the UnitOfWork part of the pattern. Just extra typing for me with no/little benefit. I decided that (as with most things I don't understand) they should prove their worth. Reading didn't help their cause so I went for the more drastic way of not using and see what goes wrong. Well, nothing did! So there it is I will never use the Unit of Work pattern again.

Until today that is.

Here is a snippet from my EntityRepository:
public ObjectContext Context { get; private set; }
//in an orthodox implementation this would accept a UnitOfWork instead of an ObjectContext
public EntityRepository(ObjectContext context)
{
    Context = new UnitOfWork(context).Context;
}

And here is the wiring in my Unity configuration:
var container = new UnityContainer();

//content repositories
container.RegisterType<IRepository<LoveContent>, EntityRepository<LoveContent>>(new InjectionConstructor(new ContentEntities()))

Something just wasn't right. I first noticed it when I created a new item and added it to the repository and then tried to delete it. Well deleting the item wasn't the problem, it was the repository knowing what had happened. I deleted the item and it was gone, gone from the database, gone from the data returned from the database but not gone from the Repository when it came to display the results on the page.

So, I figured this was something to do with my bastardisation of the Repository and Unit of Work pattern. I configured Unity correctly to implement this pattern and lo it worked as expected.

So I won!

Well, sort of. I know I thought best, it turned out I didn't, then I came crawling back to convention. But I think I am better off for it. If I had just done what all of the blogs had told me I wouldn't have understood why I was doing it.

Being a curious chap I like to understand everything I use and this has helped me. Hopefully it will help you too if you are scratching your head at the Unit of Work pattern.

But seriously, how do you solve it?

If you are/were struggling with this I have written another blog post that will tell you how to set up the repository pattern with Unit of Work using Unity 2.0.

Follow britishdev on Twitter

6 comments:

  1. This post is just beyond belief. Blogs that have huge and shrewd comments are more pleasing at any rate to me. It's charming to examine what unlike people thought and how it relates to them or their clients as their point of view could help you later on. Try to check out this best essay writing service for any kind of academic writing work.

    ReplyDelete
  2. The use of one uniform can help to create a sense of identity and honor for a particular institution. When learners from one school wear the uniform, it becomes so easy to identify the school they belong to. In other words, the image of the institution can be got from the students who use its uniform and badges. I totally support the usage of school uniforms. See my persuasive essay on school uniform.

    ReplyDelete
  3. Hi to everyone! I decided to share with you one cool writing service. There you can find a lot of useful information the can help you to make your writing better. For example, philosophy essay example

    ReplyDelete
  4. So, I figured this was something to do with my bastardisation of the Repository and Unit of Work pattern. I configured Unity correctly to implement this pattern and lo it worked as expected. cheap t shirts wholesale , men's v neck t shirts bulk , wholesale blank t shirts canada , printable t shirts wholesale , bulk t shirt deals , best price bulk wholesale t shirts , bulk childrens clothing suppliers , baby designer clothes wholesale , baby dress , buy baby clothes

    ReplyDelete
  5. 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
    fuhrerschein-kaufen-schweiz

    ReplyDelete