开发者

What is the benefit of "yield" keyword from WEB disconnected architecture point of view

开发者 https://www.devze.com 2023-01-06 03:58 出处:网络
After reading various posts and SO que开发者_StackOverflow中文版stions, 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 a

After reading various posts and SO que开发者_StackOverflow中文版stions, 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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号