Search code examples
c#wcfsilverlightsilverlight-4.0wcf-ria-services

Batch queries for DomainContext with RIA services


I have a simple Silverlight web page that is displaying dtaa from a remote database using RIA services. I have a DomainContext in which I make queries on the database through.

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

Notice that I am listening for the query to finish. The problem here is that I need to make at least 20 different queries, with each query involving a different entity object. The application really can't do much until all the data is loaded either. So, I would really only like to know when all the queries are finished. Is there an easy way to create a batch of queries?

I tried this on my own, but I ran into a problem due to the fact that each query involves a different entity. I created a list of EntityQuery<Entity> and figured I could iterate over it and execute all the queries, but the Load method either complains about having the wrong parameters or it fails during runtime.


Solution

  • We have accomplished what you are meaning to do by tracking the number of pending load operations. When it reaches 0, you are effectively done.

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