如何配置工作单元

发布于 2024-12-18 22:56:19 字数 1007 浏览 2 评论 0 原文

我正在取得进展,但仍在多层 MVC 应用程序中的工作单元方面苦苦挣扎。查看此处的示例: http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application< /a> UoW 包装所有存储库,并为每个存储库提供相同 dbcontext 的副本。然后控制器可以使用存储库,例如:

var courses = unitOfWork.CourseRepository.Get(includeProperties: "Department");

现在假设您有一个访问存储库的服务层。您可以对其进行配置,使其依赖于 IUnitOfWork 实现,然后通过 Unity 传入 EfUnitOfWork 实现。然后,当服务完成某些任务时,它可以调用unitOfWork.context.SaveChanges()。但这种方法隐藏了服务的真正依赖关系;它需要的存储库。这也意味着测试服务需要您构建完整的 UoW。

因此,我认为必须有一种不同的方法,并且想知道以下方法之一或我上面提到的(或其他方法!)是否是正确的方法:

  • 服务采用相同的存储库参数和 IUnitOfWork。这些存储库与 Unity 提供的 dbContext 副本连接起来。 EfUnitOfWork 也与相同的副本连接。然后,服务可以像以前一样使用存储库,并在完成后使用 EfUnitOfWork 进行提交。
  • 服务仅接受 IUnitOfWork,但通过向其传递 IUnitOfWork.dbcontext 中传递的副本来设置其所需的存储库

请帮忙!

詹姆斯

I am making progress but still struggling with Unit of Work in a multi layer MVC app. Looking at the example here: http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application the UoW wraps all of the Repositories and provides each with a copy of the same dbcontext. Then the controller can use the Repositories with something like:

var courses = unitOfWork.CourseRepository.Get(includeProperties: "Department");

Now suppose You have a Service layer which accesses the Repositories instead. You could configure it so that it has a dependency on an IUnitOfWork implementation, then pass in an EfUnitOfWork implementation via Unity. Then when the Service completes some task it can call unitOfWork.context.SaveChanges(). But this approach hides the real dependencies for the Service; the repositories it needs. It also means that testing the Service requires you build a full UoW.

So I was thinking there must be a different approach and am wondering if one of the following or what I mentioned above (or something else!) is the correct approach:

  • Service takes in the same repository arguments and also an IUnitOfWork. The repositories are wired up with a copy of dbContext courtesy of Unity. The EfUnitOfWork is also wired with the same copy. The Service can then use the Repositories as before and once finished use EfUnitOfWork to commit.
  • Service just takes in an IUnitOfWork but sets up its required Repositories by passing to them a copy of the passed in IUnitOfWork.dbcontext

Please help!

James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

孤独难免 2024-12-25 22:56:19

服务层通常设计为让每个方法执行完整的操作。服务层方法负责处理工作单元。使用这种方法,工作单元不应跨越对服务层的多个调用。

如果您想要一起执行较大的更新块,则可以使用事务。创建一个新的 TransactionScope ,然后调用其中的几个服务层方法:

using(TransactionScope ts = new TransactionScope())
{
  ServiceLayer.DoSomething();
  ServiceLayer.DoSomethingElse();
  ts.Commit();
}

A service layer is normally designed to have each method doing a complete operation. The service layer method is responsible for handling the unit of work. Using this approach the unit of work should not span multiple calls to the service layer.

If you have larger blocks of updates that you want to do together you can use transactions. Create a new TransactionScope and then call several service layer methods within it:

using(TransactionScope ts = new TransactionScope())
{
  ServiceLayer.DoSomething();
  ServiceLayer.DoSomethingElse();
  ts.Commit();
}
你的背包 2024-12-25 22:56:19

是的,在进一步探讨这个问题后,我得出了以下结论,所以我想我会在这里记录下来以帮助其他人,或者这样如果我的发现是错误的,我可以得到纠正。

DbContext一个工作单元。我只需将此工作单元传递到已实现的 EFRepository 类中。它不需要进入服务类。那么,当 Service 类没有 DbContext 实例时,如何调用 context.SaveChanges() 来确保所有相关更改得到协调?它调用 EFRepository.Save(),如下所示:

public void Save()
    {
        context.SaveChanges();
    }

使用这种方法,服务类仅依赖于存储库。这将是清楚的并且可以被模拟以进行测试。当Unity将所需的Repository对象注入到Service中时,它可以为每个Repository提供相同的DbContext。此外,只有存储库可以访问 DbContext。

所有这些可能是显而易见的,但它让我难住了。或者它可能是完全错误的,在这种情况下请告诉我!

詹姆斯

Right so having explored the issue futher I have come to the following conclusion so thought I would document here to help others or so that i can be corrected should my findings be wrong.

DbContext is a Unit of Work. I only need to pass this Unit of Work into the implemented EFRepository classes. It does not need to go into the Service classes. So how does a Service class call context.SaveChanges() to ensure all related changes are coordinated when it does not have an instance of DbContext? Well it calls EFRepository.Save() which looks like the following:

public void Save()
    {
        context.SaveChanges();
    }

With this approach, Service classes depend only on Repositories. This will be clear and can be mocked for testing. When Unity injects the required Repository objects into a Service, it can provide each Repository with the same DbContext. In addition, only Repositories have access to the DbContext.

All of this may be obvious but it had me stumped. Or it may be plain wrong, in which case please let me know!

James

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