在实体框架中使用工作单元和存储库模式的好处 [英] Benefit of using Unit of Work and Repository Patterns with Entity Framework

查看:86
本文介绍了在实体框架中使用工作单元和存储库模式的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 MSDN DbContext 定义为:


表示工作单元和存储库模式是
,可让您查询数据库并将更改组合在一起,然后
将作为单元写回到商店。

Represents a combination of the Unit-Of-Work and Repository patterns and enables you to query a database and group together changes that will then be written back to the store as a unit.

由于 DbContext 实现了工作单位和存储库模式,那么为什么 ASP.NET教程和我在Internet上发现的其他资源都演示了 DbContext 具有工作单元和存储库模式的定制实现?

Since DbContext implements the Unit of Work and Repository patterns, then why does this ASP.NET tutorial and other resources that I have found on the Internet demonstrate the use of DbContext with custom implementations of the Unit of Work and Repository patterns? Isn't this redundant?

如果不是 ,那么在使用<时创建工作单元和存储库层的自定义实现有什么好处? code> DbContext ? (我可以看到在Test项目中这可能是有意义的。)

If not, what is the benefit of creating custom implementations of the Unit of Work and Repository layers when using DbContext? (I can see how this might make sense within a Test project.)

推荐答案

是的, DbContext 表示工作单元,而 DbSet 表示存储库,但是有些人会在其上创建抽象层。人们这样做的原因可能有以下几种:

Yes, DbContext represents a Unit of Work and DbSet represents a Repository, but some people will create a layer of abstraction over them. Here are some reasons people might do so:


  • 也许他们不希望自己的项目与Entity Framework及其架构紧密耦合。因此,它们将Entity Framework隐藏在这些抽象的后面,以便可以在不对数据访问层的接口进行任何修改的情况下,将Entity Framework替换为任何其他ORM。

  • 它们使用存储库来明确指出哪个某些实体允许进行操作。 (例如, CustomerRepository 可能允许添加和更新客户,但不能删除客户)。另一方面,它使客户开发人员可以轻松识别某些实体的可用操作。换句话说,它们使用与域语言兼容的命名约定和接口来创建存储库。

  • 将与数据库相关的操作移至存储库可让您拦截这些操作并执行日志记录,性能调整

  • 某些方法可以简化测试。假设我有一个带有三种方法的 ICustomerRepository 接口。然后,我可以轻松地模拟它,而不用太多的方法模拟 IDbSet< Customer>

  • 最后,有很多人 不要 DbContext DbSet 上创建抽象。他们只是直接使用它们,这样做是完全有效的。

  • Maybe they don't want their project tightly coupled to Entity Framework and its architecture. So, they hide Entity Framework behind those abstractions so they can substitute Entity Framework for any other ORM without any modification to the interface of the data access layer.
  • They use repositories to make it clear which operations are allowed for certain entities. (For example, CustomerRepository might allow adding and updating customers but not deleting them). On the other hand, it enables a client developer to easily recognize available operations for certain entities. In other words, they create repositories with naming conventions and interfaces that are compatible with the domain language.
  • Moving database-related operations to repositories allows you to intercept those operations and perform logging, performance tuning or any other operation you want.
  • Some do it to make testing easier. Say I have an ICustomerRepository interface with three methods. Then I can easily mock that up instead of mocking an IDbSet<Customer> with too many methods.
  • Finally, there are many who do not create an abstraction over DbContext and DbSet. They just use them directly and it is perfectly valid to do so.

这篇关于在实体框架中使用工作单元和存储库模式的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆