I used them to write a system in ASP.NET for creating a series of linked page interactions. If you imagine a user's conversation with a website as a series of requests and responses, you can model an interaction as an IEnumerable
. Conceptually, like this;
IEnumerable<PageResponse> SignupProcess(FormValues form)
{
// signup starts with a welcome page, asking
// the user to accept the license.
yield return new WelcomePageResponse();
// if they don't accept the terms, direct
// them to a 'thanks anyway' screen
if (!form["userAcceptsTerms"])
{
yield return new ThanksForYourTimePageResponse();
yield break;
}
// On the second page, we gather their email;
yield new EmailCapturePage("");
while(!IsValid(form["address"]))
{
// loop until we get a valid address.
yield return new EmailCapturePage("The email address is incorrect. Please fix.");
}
}
You can store the iterator in session state, so that when the user returns to the site you just pull the iterator out, move the iterator onto the next page, and yield it back for rendering. Complex site interactions are coded in a single place.