Shall, I implement Repository, Unit of Work for EF6?

时间:2020-12-11 14:31:32

Since MSDN says about DbContext:

由于MSDN说的是关于DbContext:

A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.

DbContext实例表示工作单元和存储库模式的组合,以便它可以用于从数据库进行查询并将更改组合在一起,然后将更改作为一个单元写回到存储中。 DbContext在概念上类似于ObjectContext。

Is it not redundant to implement these two (Unit of Work & Repository) when using EF5+?

使用EF5 +时,实现这两个(工作单元和存储库)是不是多余的?

Can somebody shed more light on this subject?

有人可以对这个问题有所了解吗?

I intend to build an MVC based application using SQL server and after reading a lot about data access techniques with unit testability, I am kind of lost with the above info!

我打算使用SQL服务器构建一个基于MVC的应用程序,在阅读了大量关于具有单元可测试性的数据访问技术之后,我对上述信息感到很失望!

2 个解决方案

#1


1  

That depends on the complexity of your project and its requirements. For instance, these two questions might help your decision making:

这取决于项目的复杂性及其要求。例如,这两个问题可能有助于您做出决策:

  1. Will you use any other data sources besides EF that must work along it?
  2. 除了EF之外,您是否会使用除此之外必须使用的任何其他数据源?

  3. How likely it is that you swap EF for a different ORM or data source in the future?
  4. 将EF替换为不同的ORM或数据源的可能性有多大?

If you can't foresee changes or you don't need to work with more than just EF then it's probably not worth the trouble.

如果你无法预见到变化,或者你不需要使用的不仅仅是EF,那么它可能不值得麻烦。

#2


1  

I would create a Generic repository so you can mock it in your tests more easily than mocking Entity Framework's context directly. But, yes, EF 5+ does implement these patterns as MSDN states.

我将创建一个Generic存储库,这样您就可以比直接模拟Entity Framework的上下文更容易在测试中模拟它。但是,是的,EF 5+确实将这些模式实现为MSDN状态。

It's a layer of abstraction. The repository pattern is a collection of objects and a thing to get a collection of objects. Entity Framework knows HOW to get that collection of objects, the repository does not know HOW.

这是一个抽象层。存储库模式是对象的集合,是获取对象集合的东西。实体框架知道如何获取对象集合,存储库不知道如何。

Entity Framework has a lot features that you potentially loose by wrapping it in a repository or a thinner service. If you practise TDD coding against your own classes is often more comfortable than mocking third-party code.

实体框架具有许多功能,您可以将它包装在存储库或更薄的服务中。如果您针对自己的类练习TDD编码通常比模拟第三方代码更舒服。

Ayende has a blog post about this.

Ayende有一篇关于此的博客文章。

#1


1  

That depends on the complexity of your project and its requirements. For instance, these two questions might help your decision making:

这取决于项目的复杂性及其要求。例如,这两个问题可能有助于您做出决策:

  1. Will you use any other data sources besides EF that must work along it?
  2. 除了EF之外,您是否会使用除此之外必须使用的任何其他数据源?

  3. How likely it is that you swap EF for a different ORM or data source in the future?
  4. 将EF替换为不同的ORM或数据源的可能性有多大?

If you can't foresee changes or you don't need to work with more than just EF then it's probably not worth the trouble.

如果你无法预见到变化,或者你不需要使用的不仅仅是EF,那么它可能不值得麻烦。

#2


1  

I would create a Generic repository so you can mock it in your tests more easily than mocking Entity Framework's context directly. But, yes, EF 5+ does implement these patterns as MSDN states.

我将创建一个Generic存储库,这样您就可以比直接模拟Entity Framework的上下文更容易在测试中模拟它。但是,是的,EF 5+确实将这些模式实现为MSDN状态。

It's a layer of abstraction. The repository pattern is a collection of objects and a thing to get a collection of objects. Entity Framework knows HOW to get that collection of objects, the repository does not know HOW.

这是一个抽象层。存储库模式是对象的集合,是获取对象集合的东西。实体框架知道如何获取对象集合,存储库不知道如何。

Entity Framework has a lot features that you potentially loose by wrapping it in a repository or a thinner service. If you practise TDD coding against your own classes is often more comfortable than mocking third-party code.

实体框架具有许多功能,您可以将它包装在存储库或更薄的服务中。如果您针对自己的类练习TDD编码通常比模拟第三方代码更舒服。

Ayende has a blog post about this.

Ayende有一篇关于此的博客文章。