“产量”有什么好处?从WEB断开架构的角度来看关键词
在阅读了各种帖子和 SO 问题之后,我明白,如果我们有很长的数据链,那么使用 Yield 可能会很好,但请告诉我就 ASP.NET 而言,它是如何熟练的,或者你可以说断开连接的 Web 架构的观点。例如,我通过 DAL 从数据库获取数据,现在连接已断开。现在我的 BL 中有来自 DAL 的数据,现在我必须为该数据申请 foreach (我需要一一处理它们)并将集合返回到我的 UI 。您认为在这里使用yield 会好吗?
谢谢 维杰
After reading various posts and SO questions, I understand that If we have very long chain of data then going with yield can be good but please tell me how it is proficient as far as ASP.NET or you can say disconnected web architecture point of view. For example I am bringing my data from database through DAL and now the connection is disconnected . Now I have data in my BL from my DAL now I have to apply foreach for that data (I need to process each of them one by one) and return the collection then to my UI . Will you think it will be good to use yield here ?
Thanks
Vij
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Yield 基本上指示编译器生成一个枚举器(这是一个简单的状态机),根据需要流式传输数据,foreach 循环遍历 IEnumerable“拉动”每个元素。
因此,yield 语句在特定上下文中所能做的就是提供惰性流语义,这意味着接收器可以随时停止迭代,从而减少传输的数据量。与此相反,在一批中返回完全填充的集合,这是不使用
yield
时得到的结果。这是否有效取决于很多因素,您没有提供任何信息,所以我无法进一步帮助您。Yield basically instructs the compiler to generate an Enumerator (which is a simple state machine) that streams your data on demand, the foreach loop itearing over the IEnumerable "pulling" each element.
So all that a yield statement can do in your particular context is providing lazy streaming semantics, which means that a receiver can stop iterating any time and therefore reduce the amount of data transferred. Contrast that with returning a completely filled collections in one batch, which is what you get when not using
yield
. If thats an efficient thing or not depends on a lot of factors you didn't give any information about, so I can't help you any further.根据数据源的布局,如果您有分页列表视图,并且只想显示每个列表视图的前 10 个元素。这样您就不必将所有 3000 个项目发送到断开连接的网站,这实际上应该会导致更快的渲染。
当然,这可以使用 SQL 来完成,但所有数据库都会返回一个指向您希望显示的集合中第一行的指针(迭代器)。因此,无论哪种方式,您都可以获得性能,但是获得多少取决于您的架构,以及您如何设置数据层并通过视图模型和存储库使用它。
Depending on the layout of your datasource, if you had a paged listview, and only wanted to display the first 10 elements on each. Then you wouldn't have to send all 3000 items to your disconnected website, which effectively should result in a faster rendering.
Of course, this can be done with SQL, but all databases returns a pointer (iterator) to the first row in the set you wish to display. So you gain performance either way, but how much you gain depends on your architecture, and how you setup your data-layer and used it though out your viewmodels and repositories.
收益率与你问的问题无关。它与性能没有任何关系。它并不意味着“让出 CPU”或“让出线程”。它只是创建自定义枚举器的语法的一部分。
Yield has nothing to do with the question you're asking. It doesn't have anything to do with performance. It doesn't mean, "yield the CPU", or "yield the thread". It's just part of the syntax for creating a custom enumerator.