Ordering the calling of asynchronous methods in c#

Posted by Peter Kelly on Stack Overflow See other posts from Stack Overflow or by Peter Kelly
Published on 2010-05-07T09:13:55Z Indexed on 2010/05/07 9:18 UTC
Read the original article Hit count: 322

Filed under:
|
|

Hi,

Say I have 4 classes

  • ControllerClass
  • MethodClass1
  • MethodClass2
  • MethodClass3

and each MethodClass has an asynchronous method DoStuff() and each has a CompletedEvent.

The ControllerClass is responsible for invoking the 3 asynchronous methods on the 3 MethodClasses in a particular order.

So ControllerClass invokes MethodClass1.DoStuff() and subscribes to MethodClass1.CompletedEvent. When that event is fired, ControllerClass invokes MethodClass2.DoStuff() and subscribes to MethodClass2.CompletedEvent. When that event is fired, the ControllerClass invokes MethodClass3.DoStuff()

Is there a best practice for a situation like this? Is this bad design?

I believe it is because

  • I am finding it hard to unit test (a sure sign)
  • It is not easy to change the order
  • I have an uneasy, code-smell feeling about it

What are the alternatives in a situation like this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about design