WCF RIA服务-加载多个实体 [英] WCF RIA Services - Loading multiple entities

查看:70
本文介绍了WCF RIA服务-加载多个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决以下问题的模式,我认为这很常见.

I'm looking for a pattern to solve the following problem, which I imagine is common.

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

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?

谢谢!

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

推荐答案

您的解决方案应该可以正常工作.代码中有一个小问题-您正在服务器上调用async方法,然后绑定OnCompleted事件.如果呼叫超快并且在绑定事件之前结束,那么您将看不到实体.

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.

根据我的经验,这从来都不是问题(在99.99%的情况下,它可以正常工作),但是只要有干净的代码,就可以在Load方法中提供回调,例如

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 });

提示:为了将实体加载到ObservableCollection中,我创建了从ObservableCollection派生的自定义类,该类将DomainContext和DomainQuery作为ctor中的参数,并且能够从服务器本身加载项.另外,可以在XAML中绑定集合,并且加载的实体会在GUI中自动更新.

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.

这篇关于WCF RIA服务-加载多个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆