ADO.NET Data Services : Tips & Tricks


In this post, I’ll give you some tips interesting that helped me in my several projects.

For more information on Data Services, I suggest you to read my previous posts:

TIPS 1 : ADO.NET Data Services CTP 1.5 CTP 2

Accessible here, this new release targeting .net 3.5 & Silverlight 3 provides some great new features (some more details about this release):

  • Projections;
  • Better data binding;
  • Row count;
  • Server Page Driven;

-> One of the major feature of this version is to allow cross domain access to your Data Services from your Silverlight application.

Here you can access to a description of this feature written in the ADO.NET Data Services Team Blog.

TIPS 2 : Returning hierarchical data

A great advantage of returning hierarchical in advance to reduce the number of server calls especially when you know that the hierarchical data is needed.

In the following example, the request will return a specific product:

Code Snippet

  1. public DataServiceQuery<Product> ReturnHierarchicalData(int productId)
  2. {   // Create a hierachical data query
  3.     DataServiceQuery<Product> request = (from product in _adventureWorksProxy.Products
  4.          .Expand("ProductSubcategory")
  5.          .Expand("ProductSubcategory/ProductCategory")
  6.     where product.ProductID == productId
  7.     select product) as DataServiceQuery<Product>;
  9.     return request;
  10. }

This request will returns the specific ProductSubCategory entity of the product and the ProductCategory of this ProductSubcategory.

TIPS 3 : Deferred loading

In opposition to hierarchical data return in a single call, when the size of data to return is too heavy, you should use deferred loading.

In this example, I will return the ProductSubcategory of the product only when needed.

Code Snippet

  1. void ReturnDeferredData(Product product)
  2. {   
  3.     // Deferred load
  4.     _adventureWorksProxy.BeginLoadProperty(product, "ProductSubcategory", ReturnDeferredDataCallBack, product);
  5. }
  6. void ReturnDeferredDataCallBack(IAsyncResult ar)
  7. {
  8.     _adventureWorksProxy.EndLoadProperty(ar);
  9.     Product product = ar.AsyncState as Product;
  10. }


BeginLoadProperty requires four parameters, the entity used for deferred loading, the name of the property used in the deferred mechanism, the callback ant the context.

The callback method will end the asynchronous system.

TIPS 4 : Batching requests

One other great feature of Data Services is batching. it allows you to request in a same time several requests to the server and to return the result only in one time.

This example shows how to execute two requests on the server with only one server access. You can check using fiddler to confirm :-)

Code Snippet

  1. // Creation of two requests
  2. var request1 = new DataServiceRequest<Product>(new Uri("Products",UriKind.Relative));
  3. var request2 = new DataServiceRequest<ProductSubcategory>(new Uri("ProductSubcategory", UriKind.Relative));
  5. // Execution of those two requests on the server
  6. DataServiceResponse responses = adventureWorksProxy.ExecuteBatch(request1, request2);
  7. foreach (QueryOperationResponse response in responses)
  8. {
  9.     if(response.Query.ElementType == typeof(Product))
  10.     {
  11.         // ...
  12.     }
  13.     else if (response.Query.ElementType == typeof(Product))
  14.     {
  15.         // ...
  16.     }
  17. }

Here is an interesting post from ADO.NET Data Services Team Blog.