tags:

views:

75

answers:

3

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

+2  A: 

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.

Johannes Rudolph
How can a receiver stop iterating any time ? since we have brought the data from my DAL with datareader which means we have not yet reached to BL layer back until all the data has come from DAL in the form of collection.
Vijjendra
@Vijjendra: the assumption here is that the client can work with the stream while the data is being transfered. Your client could continue to fill up some listview while records are streamed in. I'ts not impossible without the yield syntax, but it helps....
jdv
+1  A: 

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.

Claus Jørgensen
A: 

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.

John Saunders
Agreed. I think of it more as a shortcut, less code, better readability.
Jeroen
@Downvoter: you should explain your reason for downvoting if you want anyone to care.
John Saunders