2012-03-21 101 views
0

我有一个简单的Silverlight网页,使用RIA服务从远程数据库显示dtaa。我有一个DomainContext我在其中通过查询数据库。使用RIA服务批量查询DomainContext

context.Load(context.GetSitesQuery()).Completed += new EventHandler(Query_Completed); 

请注意,我在等待查询完成。这里的问题是我需要做至少20个不同的查询,每个查询涉及不同的实体对象。在加载所有数据之前,应用程序真的不能做很多事情。所以,我真的只想知道所有查询何时完成。有没有简单的方法来创建一批查询?

我自己试过,但由于每个查询涉及不同的实体,我遇到了一个问题。我创建了一个EntityQuery<Entity>的列表,并认为我可以迭代它并执行所有查询,但Load方法要么抱怨参数错误,要么在运行时失败。

回答

0

我们已经完成了通过跟踪待处理装载操作的数量来完成的任务。当它达到0时,你就完成了。

using System.ServiceModel.DomainServices.Client; 

... 

private int _loadCounter; 
private TheDomainContext _domainContext; 

private void Load<TEntity>(EntityQuery<TEntity> query, 
          Action<LoadOperation<TEntity>> callback) 
         where TEntity : Entity 
{ 
    BeginLoading(); 
    Action<LoadOperation<TEntity>> internalCallback = 
      loadOp => { 
          callback(loadOP); 
          EndLoading(); 
         }; 
    _domainContext.Load(query, internalCallback , null); 
} 

private void BeginLoading() 
{ 
    _loadCounter++; 
    // You could add logic to indicate the app is busy 
} 

private void EndLoading() 
{ 
    _loadCounter--; 
    if (_loadCounter == 0) 
    { 
     OnLoadComplete(); 
    } 
} 

private void OnLoadComplete() 
{ 
    // TODO Everything is loaded 
} 

private void BeginLoadingTheQueries() 
{ 
    // Increment once here to prevent the OnLoadComplete from occurring 
    // before all queries have started 
    BeginLoading(); 
    Load(_domainContext.GetSitesQuery(), Query_Completed); 
    Load(_domainContext.GetOtherQuery(), OtherQuery_Completed); 
    EndLoading(); 
} 
+0

您如何获得对'TEntity'的参考? (感谢您添加C#标签) – 2012-03-22 13:43:37

+0

我加了'where TEntity:Entity'和必要的'using'语句。 – 2012-03-22 17:52:25

+0

谢谢。这基本上是我已经有的。我希望有一个更好的方式,但我想这没问题。 – 2012-03-23 12:56:41