存储库 +实体框架的 UnitOfWork 模式
我在网上上下搜索,但没有找到适合我的应用程序的设计。
我正在寻找 Repository+UnitOfWork 模式,它将管理连接并在完成后自动处理它们。
我需要支持 Web 应用程序(其中每个请求都有自己的 UnitOfWork)和 Windows 应用程序(其中每个线程都有自己的 UnitOfWork)。我需要模式来自动处理请求/线程完成时的工作单元。 我也想支持异常情况下的回滚。
现在我使用 StructureMap,所以我不想在建议答案中继续使用它。
我需要存储库模式的原因是为了实现我所有实体所需的所有能力。 我需要 UnitOfWork 的原因是允许对多个实体进行更改。
我真的会感谢任何帮助。
谢谢。
I was searching the net up and down and I didn't manage to find a suitable design for my application.
I am looking for Repository+UnitOfWork pattern that will manage connections and dispose them automatically when done.
I need to support both web application where each request will have its own UnitOfWork and windows application where each thread will have its own UnitOfWork. I need the patters to dispose automatically the UnitOfWork whrn request/thread is done.
I also would like to support rolback in case of exception.
Right now I use StructureMap so I don't care to continue use it in the suggest answers.
The reason I need Repository pattern is to achieve all the abilities I need to all my entities.
The reason I need UnitOfWork is to allow changes in more then one entity.
I will really appriciate any help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我用这个博客作为一个非常好的起点:
http://www.primaryobjects.com/CMS/ Article122.aspx
它从头开始,并在最后为您提供源代码。它还使用 StructureMap,因此您可能有点熟悉。
I used this blog as a really good starting point:
http://www.primaryobjects.com/CMS/Article122.aspx
It starts at the VERY beginning, and provides source code at the end for you. It also uses StructureMap, so it might be somewhat familiar to you.
我推荐 NCommon 框架。您可以在这里找到有关它的博客:http://www.codeinsanity.com/
I would recommend the NCommon framework. You can find a blog about it here: http://www.codeinsanity.com/
我去年写了一篇关于编写支持 LINQ 的存储库的文章,这些存储库可以轻松伪造以进行单元测试,并且可以很好地与依赖项注入配合使用。 这是这篇文章。简而言之,本文描述了一个工作单元,它模仿 LINQ to SQL,它允许您对其进行 LINQ。
DataContext
并包装一个抽象实际 O/RM 工具的IDataMapper
接口。工作单元包含Repository
类型的属性,例如Repository
或Repository
并且存储库类实现 < code>IQueryableIDataMapper
是一个简单的界面,如下所示:本文中描述的解决方案设计为单元测试友好且 DI 友好。事实上,您需要的唯一配置如下:
I wrote an article last year about writing LINQ enabled repositories that can be faked easily for unit testing and works well with dependency injection. Here is the article. In short the article describes a unit of work that mimics the LINQ to SQL
DataContext
and wraps anIDataMapper
interface that abstracts the actual O/RM tool. The unit of work contains properties of typeRepository<TEntity>
such asRepository<Customer>
orRepository<Order>
and the repository class implementsIQueryable<T>
, which allows you to LINQ over it.The
IDataMapper
is a simple interface that looks like this:The solution described in the article is designed to be unit test friendly and DI friendly. In fact, the only configuration you need is the following:
如果您已经有了工作单元和存储库并且正在使用 StructureMap 那么问题出在哪里?
您可以简单地将您的类配置为:
您可以使用依赖项注入将工作单元传递到存储库。
If you already have unit of work and repositories and you are using StructureMap so where is the problem?
You can simply configure your classes as:
And you can use dependency injection to pass unit of work to repositories.