I’m looking for a pattern to solve the following problem, which I imagine is common.
I am using WCF RIA Services to return multiple entities to the client, on initial load. I want both entities to load asyncrhonously, so as not to lock the UI, and I’d like to leverage RIA Services to do this.
My solution, below, seems to work. Will I run into problems/limitations with this approach? Is there a better pattern for this?
Thanks!
//create proxy to Domain Service
var proxy = new RIAService.Web.DomainContext();
//call service; fire event when Presentation entities have been returned
var loadPresentations = proxy.Load(proxy.GetPresentationsQuery());
loadPresentations.Completed += new EventHandler(loadPresentations_Completed);
//call service; fire event when Topics entities have been returned
var loadTopics = proxy.Load(proxy.GetTopicsQuery());
loadTopics.Completed += new EventHandler(loadTopics_Completed);
void loadTopics_Completed(object sender, EventArgs e)
{
//bind topic entities to XAML
}
void loadPresentations_Completed(object sender, EventArgs e)
{
//bind presentation entities to XAML
}
Your solution should work as is. There is one little catch in your code – you are calling the async method on server, and after that you are binding the OnCompleted event. If the call is superfast and ends before the event is bound, you won’t see the entities.
In my experience this has never been a problem (in 99.99% cases it works fine), but just to have clean code, you can provide the callback inside the Load method, like
Hint: In order to load entities into ObservableCollection, I created custom class deriving from ObservableCollection, which takes DomainContext and DomainQuery as parameters in ctor and is able to load the items from server itself. In addition it is possible to bind the collection in XAML and loaded entities are automatically updated in GUI.