2
votes

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
}
3

3 Answers

1
votes

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

proxy.Load(proxy.GetPresentationsQuery(), op => { here work with op.Value });

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.

0
votes

Not brilliant solution - but works.

Load all operation in sequential order. Next load start when previous load event is completed.

MyDomainContext proxy;

public void Initialize()
{
    //create proxy to Domain Service  
    proxy = new RIAService.Web.DomainContext();

    //load Presentation - LOAD STEP 1
    Load(proxy.GetPresentationsQuery(), LoadPresentations_Completed, null);
}


void LoadPresentations_Completed(LoadOperation<PresentationEntity> loadOp)
{
  if (loadOp.HasError)
  {
     //process error here
     loadOp.MarkErrorAsHandled = true;
  }
  else
  {
      - LOAD STEP 2
     var loadTopics = proxy.Load(proxy.GetTopicsQuery());
     loadTopics.Completed += new EventHandler(loadTopics_Completed);
  }
}


void loadTopics_Completed(object sender, EventArgs e)
{

  //bind presentation entities to XAML      
}

Good luck.

0
votes

This is the same pattern I have been using on a Silverlight app that has been in production since June. It seems to work well for me. In particular, it takes advantage of multi-threaded back end servers quite well since each entity request will execute in parallel on the server.

Rlodina suggested loading the entities sequentially which also works. I avoided this in favor of the performance boost of parallel operations. But, there was a case when I was forced to use the sequential operations. That case was when the second query needed to be constrained by the results of the first.

So, to be blunt in answering your question, this pattern worked well for me. It is functional and simple. I always advocate doing the simplest thing that could possibly work.