简体   繁体   English

WCF RIA服务-加载多个实体

[英]WCF RIA Services - Loading multiple entities

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. 我正在使用WCF RIA服务在初始加载时将多个实体返回给客户端。 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. 我希望两个实体都异步加载,以免锁定UI,并且我想利用RIA Services来做到这一点。

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. 您的代码中有一个小问题-您正在服务器上调用async方法,然后绑定OnCompleted事件。 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 以我的经验,这从来都不是问题(在99.99%的情况下,它可以正常工作),但是为了获得清晰的代码,您可以在Load方法内部提供回调,例如

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. 提示:为了将实体加载到ObservableCollection中,我创建了从ObservableCollection派生的自定义类,该类将DomainContext和DomainQuery作为ctor中的参数,并且能够从服务器本身加载项。 In addition it is possible to bind the collection in XAML and loaded entities are automatically updated in GUI. 另外,可以在XAML中绑定集合,并且已加载的实体会在GUI中自动更新。

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. 祝好运。

This is the same pattern I have been using on a Silverlight app that has been in production since June. 这与我自6月以来一直在生产中的Silverlight应用程序所使用的模式相同。 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. Rlodina建议按顺序加载实体,这也有效。 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. 我始终主张做可能可行的最简单的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM