The Asynchronous Command Pattern for HTTP in Silverlight and GWT
When you’re writing RIA applications in an environment like Silverlight or GWT, you’re restricted to doing asynchronous http calls to the server — this leaves you with a number of tricky choices, such as, where to put your callback functions. To be specific, imagine we’ve created a panel in the user interface where a user enters information, then clicks on a form to submit it. The first place you might think of putting the callback is in the class for the panel, something like
public class MyPanel:StackPanel { ... other functions ... void SubmitButton_Click(Object sender,EventArgs e) { ... collect data from forms ... ServerWrapper.DoSubmission(formData,SubmissionCallback); } void SubmissionCallback(SubmissionResult result) { ... update user interface ...
}
}
(Although code samples are in C#, the language I’m using now, I developed this pattern when working on a Java project.) This is a straightforward pattern for the simplest applications, but it runs out of steam when your application becomes more complex. It can become confusing to keep track of your callback functions when your object does more than one kind of asynchronous call: for instance, if it has multiple buttons. If the same action can be done on the server from more than one place in the UI, it’s not clear where the callback belongs.
One answer to the problem is to use the Command Pattern, to organize asynchronous activities into their own classes that contain both the code that initiates an asynchronous request and the callback that runs when the request completes. Continue Reading »
Paul Houle on April 11th 2008 in Asynchronous Communications, Dot Net, GWT, Silverlight