使用 ASP.NET Core 的 EF Core 中的 Repository 和工作单元模式(重新访问) [英] The use of Repository and unit of work patterns (revisited) ... in EF Core with ASP.NET Core

查看:123
本文介绍了使用 ASP.NET Core 的 EF Core 中的 Repository 和工作单元模式(重新访问)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 本教程适用于带有 ASP.NET Core 的 EF Core.有趣的是,这篇文章指出使用存储库和工作单元模式并不总是使用 EF 的应用程序的最佳选择",但在 EF 5 的教程中,存储库和工作单元模式的使用仍然是即使有单独的教程部分也能得到提升.

I was following this tutorial for EF Core with ASP.NET Core. The interesting thing is that this article states that 'the use of Repository and unit of work patterns is not always the best choice for applications that use EF', but in the tutorial of EF 5 the use of repository and unit of work patterns still is promoted even with a seperate tutorial part.

阅读有关 stackoverflow 的旧文章据悉,应用它的原因主要是为了测试(依赖注入).

Reading old articles on stackoverflow it is reported that the reasons one would apply it is mainly for testing (dependency injection).

进一步阅读教程中提到实体框架核心实现了可用于测试的内存数据库提供程序".我认为这是一个在 EF5 时还没有的新功能.这是否意味着几乎没有任何理由在 EF Core 和 ASP.NET Core 中应用存储库和工作单元模式的使用?

Reading further in the turial it is mentioned that 'The Entity Framework Core implements an in-memory database provider that can be used for testing'. I presume this is a new feature that was not there at the time of EF5. Does this mean that there is hardly any reason anymore to apply the use of repository and unit of work patterns in EF Core with ASP.NET Core?

推荐答案

造成如此混乱的原因有很多,而且历史悠久.EF 的 DbContext 过去没有实现接口,因此它使模拟很难用于测试目的.但是,这在 EF 6 中得到了纠正,因此从那时起,使用存储库/工作单元模式绝对没有任何好处,即使是出于测试目的.

There's many reasons and a long history for why there's so much confusion here. EF's DbContext used to not implement an interface, so it made mocking it difficult for testing purposes. However, that was corrected in EF 6, so since that time there's been absolutely no benefit, even for testing purposes, in using the repository/unit of work patterns.

EF Core 是完全可端到端测试的,并且现在也有一个内存数据库提供程序,这意味着您甚至不需要模拟它,尽管您可以根据需要进行模拟.

EF Core is completely testable end-to-end and has an in-memory database provider as well now, which means you don't even need to mock it, though you very much can if you want.

总而言之,转储存储库和工作单元模式.即使它们有一些用于测试的用途,它们也从来不是一个好的解决方案,而且从 EF 6 开始,它们就完全没有用了.

Long and short, dump the repository and unit of work patterns. They've never been a good solution even when they had some use for testing, and since EF 6, they have been completely useless.

现在……让火焰战争开始.

Now... let the flame war begin.

这篇关于使用 ASP.NET Core 的 EF Core 中的 Repository 和工作单元模式(重新访问)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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