工作单位的效益和信息库模式与实体框架 [英] Benefit of Unit of Work and Repository Pattern with Entity Framework

查看:141
本文介绍了工作单位的效益和信息库模式与实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据<一href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext%28v=vs.103%29.aspx">MSDN,的DbContext被定义为

According to MSDN, DbContext is defined as

重presents单位-OF-工作和库的模式的组合,使您能够查询数据库,并组合在一起的将被写回到店里为单位的变化。

"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已经申请Unit_Of,工作和库的模式,那么为什么asp.net教程,并发现所有在互联网上的例子进一步对的DbContext应用这些彭定康像<一个给定的href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application">this tutotial。这难道不是一些东西,已经实施的执行?如果它不是那么什么是使用单位-OF-工作和库模式进一步上的DbContext(除非将它们用在测试项目)?

I want to know if DBContext is already applying Unit_Of-Work and Repository pattern then why does asp.net tutorial and all the examples found over the internet apply these patten further on the DBContext like given in this tutotial. Isn't this the implementation of something which is already implemented? If it isn't then what is the benefit of using Unit-Of-Work and Repository patterns further on DBContext (except using them in Test project)?

推荐答案

是的,的DbContext重新presents一个的UnitOfWork并DbSet重新preseets存储库,但人们往往甚至创建一个抽象了那些因为某些原因:

Yes, DbContext represents a UnitOfWork and DbSet represeets a Repository but people tend to even create an abstraction over those for some reasons:

  • 也许他们不希望自己的项目紧密结合,以实体框架和架构,使它们隐藏起来这些抽象的背后,使他们能够取代EF任何其他的ORM没有任何修改的数据访问层的界面。
  • 在他们使用的存储库其他时候要分清楚哪些操作是允许对某些实体的(如CustomerRepository允许添加和更新客户,但不删除它们)的。在另一方面,它可以让客户端开发人员很容易地识别可用的操作的某些实体,换句话说,它们创建储存库命名约定和接口,这是与域语言兼容。
  • 在移动数据库相关的操作库,您可以拦截这些行动并进行日志记录,性能调整或所需的任何其他操作。
  • 在某些这样做是为了使测试更加简单,说我有3种方法的ICustomerRepository接口,那么我可以很容易地嘲笑嘲笑的IDbSet有很多很多方法,高达代替。
  • 有人不要不可以甚至创建了一个抽象了的DbContext DbSet ,他们只需直接使用它们,这是完全有效的做到这一点。
  • Maybe they don't want their project tightly coupled to entity framework and its architecture so they hide them behind those abstractions so they can substitute EF for any other ORM without any modification to the interface of the data access layer.
  • Other times they use repositories to make it clear which operations are allowed for certain entities, (e.g. CustomerRepository allows adding and updating customers but not deleting them). On the other hand, it lets client developer easily recognize available operations for certain entities, in other words they create repositories with naming conventions and interface that is compatible with the domain language.
  • Moving db 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 3 methods then i can easily mock that up instead of mocking an IDbSet with many many methods.
  • Some people don not even create an abstraction over DbContext and DbSet, they just use them directly and it is perfectly valid to do so.

这篇关于工作单位的效益和信息库模式与实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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