存储库 +实体框架的 UnitOfWork 模式

发布于 2024-11-08 01:25:58 字数 359 浏览 3 评论 0原文

我在网上上下搜索,但没有找到适合我的应用程序的设计。
我正在寻找 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

内心旳酸楚 2024-11-15 01:25:58

我用这个博客作为一个非常好的起点:

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.

清晨说晚安 2024-11-15 01:25:58

我推荐 NCommon 框架。您可以在这里找到有关它的博客:http://www.codeinsanity.com/

I would recommend the NCommon framework. You can find a blog about it here: http://www.codeinsanity.com/

旧伤慢歌 2024-11-15 01:25:58

我去年写了一篇关于编写支持 LINQ 的存储库的文章,这些存储库可以轻松伪造以进行单元测试,并且可以很好地与依赖项注入配合使用。 这是这篇文章。简而言之,本文描述了一个工作单元,它模仿 LINQ to SQL DataContext 并包装一个抽象实际 O/RM 工具的 IDataMapper 接口。工作单元包含 Repository 类型的属性,例如 RepositoryRepository 并且存储库类实现 < code>IQueryable,它允许您对其进行 LINQ。

IDataMapper 是一个简单的界面,如下所示:

public interface IDataMapper : IDisposable
{
    Repository<T> GetRepository<T>() where T : class;

    void Save();
}

本文中描述的解决方案设计为单元测试友好且 DI 友好。事实上,您需要的唯一配置如下:

string northwindConnection = GetConStr("Northwind");

container.RegisterSingle<IUnitOfWorkFactory<NorthwindUnitOfWork>>(
    new LinqToSqlNorthwindUnitOfWorkFactory(northwindConnection));

container.RegisterSingle<IUnitOfWorkFactory<SalesUnitOfWork>>(
    new EntityFrameworkSalesUnitOfWorkFactory());

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 an IDataMapper interface that abstracts the actual O/RM tool. The unit of work contains properties of type Repository<TEntity> such as Repository<Customer> or Repository<Order> and the repository class implements IQueryable<T>, which allows you to LINQ over it.

The IDataMapper is a simple interface that looks like this:

public interface IDataMapper : IDisposable
{
    Repository<T> GetRepository<T>() where T : class;

    void Save();
}

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:

string northwindConnection = GetConStr("Northwind");

container.RegisterSingle<IUnitOfWorkFactory<NorthwindUnitOfWork>>(
    new LinqToSqlNorthwindUnitOfWorkFactory(northwindConnection));

container.RegisterSingle<IUnitOfWorkFactory<SalesUnitOfWork>>(
    new EntityFrameworkSalesUnitOfWorkFactory());
蓝眸 2024-11-15 01:25:58

如果您已经有了工作单元和存储库并且正在使用 StructureMap 那么问题出在哪里?

您可以简单地将您的类配置为:

ObjectFactory.Configure(x => x.For<IUnitOfWork>()
                              .HybridHttpOrThreadLocalScoped()
                              .Use<EFUnitOfWork>());

您可以使用依赖项注入将工作单元传递到存储库。

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:

ObjectFactory.Configure(x => x.For<IUnitOfWork>()
                              .HybridHttpOrThreadLocalScoped()
                              .Use<EFUnitOfWork>());

And you can use dependency injection to pass unit of work to repositories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文