Chaining Smart Web Services in the Presenter

Topics: General Discussion Forum, July and December Releases Forum
Feb 19, 2007 at 11:51 PM
I've created a presenter that needs to access data from three separate smart web services. Does anyone have any suggestions about combining results from multiple asynchronous requests. Currently I'm using thread wait handles to signal that all requests are complete. However, I'm quite sure that my code is error prone. Any suggestions appreciated.

public class ClientViewPresenter : Presenter<IClientView>
private AutoResetEvent _productsReady = new AutoResetEvent(false);
private AutoResetEvent _groupsReady = new AutoResetEvent(false);
private WaitHandle _dataReady = new WaitHandle[] { _productsReady, _groupsReady };

// ... Service dependencies

public override OnViewReady()
_referenceService.GetAllProducts(delegate(Product[] products) {
_products = products;

_referenceService.GetAllGroups(delegate(Group[] groups) {
_groups = groups;

clientService.GetClient(clientID, delegate(ClientSummary client) {
_client = client;
// update the view once we have all the data
View.PopulateClient(_client, _products, _groups);
Feb 21, 2007 at 5:12 PM
I think this is a valid approach. However, you may want to check some testing scenarios that may test your implementation.
1) Consider that the user may "cancel" the requested information and simply close the UI (wizard page in a GP or WinForm). IN that case, you may cancel your running thread or at least try to avoid sending events/callbacks to objects that were already disposed.
2) Perhaps some refactoring of the "integration" async code will help to reduce complexity in the presenter and at the same time allow for easy unit test the async calls. For example moving the service gathering logic to an "agent" class that will integrate the result of the three (n) calls into one with a standard async pattern (Begin/End) that will be consumed by the presenter class. In order to implement an async scenario inside this agent, this post may be of some help Also it would be nice to have somthing similar to the System.Web.UI.Page.RegisterAsyncTask method to register async operations and get the response back when all of them are done You may also take a look at this good article from Juval