Ordering the calling of asynchronous methods in c#
- by Peter Kelly
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?