Coroutines without implementing extra classes

Topics: Actions & Coroutines
Jun 20, 2011 at 2:46 AM

Caliburn Micro Coroutines are sweet.  I just blogged about how I'm sometimes using them without creating individual IResult-implementing classes for each step in the sequence.  Clearly this isn't the right approach all the time.  But sometimes all I want to do is one or two lines of code for a step in a series of asynchronous steps and I'm finding this handy.  I'd welcome feedback, especially if I missed a better way to do this.

http://blog.jeffcodes.net/2011/06/caliburn-micro-coroutines-without-individual-iresult-implementing-classes/

 

Jun 20, 2011 at 9:38 AM

This technique could be useful, but has a drawback: it's difficult to unit-test the coroutine, because the IResult behavior is defined as a delegate. 
The only way to test it is to actually execute each step of the coroutine.
Using specific IResult-s, instead, you can test the coroutine verifying that it returns the expected sequence of results.
Returned results doesn't have to be actually executed: you could just check their type and configuration.

Also, if you need to execute some synchronous step among other asyncronous operation, you can just invoke them without yielding any result. For example:

public IEnumerable<IResult> SaveButton() 
{
	var someOp = new SomeCustomAsyncOp();
	yield return someOp; 

	this.AProperty= "Almost done";

	if (someOp.Response == "OK") { 
		this.TryClose(); 
	} else {
		yield return new SomeOtherAsyncOp();  
	} 

        this.AProperty= "Done";
}