views:

446

answers:

3

Hello,

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
}
A: 

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.

rlodina
A: 

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.

Mark Ewer
A: 

I agree with all your comments on this thread.

One question: sometimes I have a two of requests to different Load operations but I need to wait for the two loads before I proceed with my flow. I do not want them to do it sequentialy.

How can I verify if the two are finished loading?

Alfredo De Regil