开发者

EF4 - Repositories and Unit of Work

I've been search开发者_如何学编程ing for the 'right' way to integrate repositories and unit of work into my project, yet I keep running across different variations. Some have the repositories as members of a uow object. Others have the repositories implementing an IUnitOfWork interface. I've seen some where the uow object is passed into the repositories as an argument to their constructors.

Is there any benefit to doing it one way over the others? What's the consensus, if there is one?


I can't say that there is a consensus out there, but on top of my having spoken to many architects about the topic, I have spent a considerable amount of time evaluating this very thing. What I've come up with is that you want to keep your object management (Repositories) and transactions (UnitOfWork) loosely coupled and independent of each other. Your Repository should be able to run without having to use a transaction and vise versa. In terms of its relation to a Repository, a Unit of Work is really just a wrapper for a transaction. In your case, you will probably be wrapping EF2 Repository operations using the TransactionScope implementation. In our framework, we've put transaction management into a DataServices namespace/project and our base Repository classes in an ObjectAccess namespace/project. From there we created an EF2 implementation for both the Repository operations and the Unit of Work operations. I can't give you the source, but basically what I did was the following:

  • Used NCommon 1.1 Beta as my starting point: NCommon
  • Decoupled the Unit of Work from the Repository operations in NCommon
  • Then I moved on to how my ObjectContext would need to be managed.

We are just about to release our first stable version of this framework. Good luck!


Given the following pages I'd say that the MS consensus is UnitOfWork exposing repositories:

http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

http://msdn.microsoft.com/en-us/library/ff714955.aspx

Both seem to be MS guidance, rather than being 1 guy's simplfied blog post example.


There is no 'right' way to do it. Find an implementation that works for your situation and use it. I personally like to keep things simple so I usually just go for a generic repository in my all projects (no UoW interface/pattern at all).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜