为什么EF 6教程中使用异步调用? [英] Why does the EF 6 tutorial use asychronous calls?

查看:933
本文介绍了为什么EF 6教程中使用异步调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是通过如何使用EF 6 MVC 5推移最新的EF教程似乎倾向于使用asych调用数据库像瘦:

The latest EF tutorial that goes through how to use EF 6 with MVC 5 seems to lean towards using asych calls to the database like:

Department department = await db.Departments.FindAsync(id);

这是新标准/最佳做法?

Is this the new standard/best practice?

我不知道的好处是这种风格的发展与ASP.NET MVC。

I'm not sure what the benefit is for this style of development with ASP.NET MVC.

能否有人对这种模式发表评论,这是新标准的MS是推进?

Can someone comment on this pattern, is this the new standard that MS is promoting?

推荐答案

为了决定是否去异步或同步,比较收益和成本:

In order to decide whether to go async or sync, compare the benefits and costs:

异步:


  • 几乎从来没有耗尽线程池与异步(这种情况下就必须做到极致)

  • pretty并发多任意级别(并发请求和操作)

  • 保存每个线程保存1MB内存

  • 安全请求并发内多亏了的SynchronizationContext

  • 可通过低两位数百分比的高负载情况下,由于减少了操作系统的调度开销增加througput。这就是说,几乎没有生产程序是高CPU负载下,因为如果它是它是接近不可用(在负载的情况下,秒杀应用开始下降请求)。

同步:


  • 简单code:等待做的情况下,(几乎)作为同步code作为简单的99%。这就是说,每一天对堆栈溢出的10+异步问题,讲不同的语言。当你从简单的路径偏离​​出现边缘情况。同时使用,例如,需要你的遗产库时要交给他们一个同步回调。

  • 编码和调试工作少

  • 探查友好(你可以分析的应用程序,或只是暂停调试器,看看该程序在做现在。不可能的异步。)

  • 与传统code和库
  • 完全互操作

选择异步与ASP.NET如果要调用高延迟的服务。:一种Web服务很可能是高等待时间。 OLTP数据库几乎总是低延迟。

Choose async with ASP.NET if you are calling high-latency services. A web-service is likely to be high latency. An OLTP database is almost always low-latency.

选择异步如果从很高的水平并发(100 +)的应用程序的好处。大多数应用程序没有这么高的水平,或它们的后端服务就不能维持负荷的量。在使Web应用程序的规模是没有意义的,但超载后端。在调用链中的所有系统的必须的从高程度的并发性,以便异步的利益是有益的。

Choose async if your application benefits from very high levels of concurrency (100+). Most applications do not have such high levels, or their back-end services would not sustain such an amount of load. No point in making the web app scale but overload the back-end. All systems in the call chain must benefit from a high degree of concurrency in order for async to be beneficial.

典型的高延迟服务(良好的情况下,用于异步):


  • Web服务的

  • 等待(例如睡眠)

  • 节流( SemaphoreSlim ,...)

  • 有些云服务(天青)

  • 长时间运行的查询到数据库中(例如报告或ETL)

典型的低延时服务(良好的情况下,为同步):


  • 数据库调用:大多数OLTP查询是低延迟的,因为你可以假定数据库服务器不会过载。在它扔并发查询100S没有意义的。不使他们完成任何更快。

  • 文件系统:同为数据库

这是由的典型的情况进行分类。所有这些都可以在相对的类别以及

These are categorized by the typical case. All of these can be in the opposite category as well.

当它在它的甜蜜点,您可以在同一应用程序混合同步和异步。使用异步。

那么,为什么微软和实体框架团队促进异步使用情况如何?这里谈到这个答案的主观部分:可能我的微软内部政策。他们可能预见的客户端应用程序(用于异步这是伟大的),EF使用。或者,他们没有意识到,异步数据库调用pretty多几乎总是开发商时无裨益的浪费。因为异步是去这些天的方式大多数人都没有意识到这一点。

So why are Microsoft and the Entity Framework team promoting async usage? Here comes the subjective part of this answer: It might me Microsoft internal policy. They might anticipate EF usage in client apps (for which async is great). Or, they don't realize that async database calls are pretty much almost always a waste of developer time without benefits. Most people don't realize this because async is the way to go these days.

这篇关于为什么EF 6教程中使用异步调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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