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

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

问题描述

我一直遵循本教程适用于带有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.

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

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