Compartilhar via


Update on the Windows Phone 7 OData Client Libirary

Team blog post here :

WCF Data Services Client Library and Windows Phone 7 – Next Steps

And some sample code for more context.

With the Linq pattern, here is what the code for downloading the first 10 customers would look like :

 DataServiceCollection<Customer> customerCollection = new DataServiceCollection<Customer>(Context);

customerCollection.Load(Context.CreateQuery<Customer>("Customers").Take(10));

Now, with the URI pattern, this is what it looks like :

 DataServiceCollection<Customer> customerCollection = new DataServiceCollection<Customer>(Context);

 customerCollection.Load(Context.CreateQuery<Customer>("Customers").Take(10));

 var top10CustomersUri = new Uri("Customers?$top=10", UriKind.RelativeOrAbsolute);
 Context.BeginExecute<Customer>(top10CustomersUri,
      (asResult) =>
      {
        var top10CustomersResponse = Context.EndExecute<Customer>(asResult) as QueryOperationResponse<Customer>;
        customerCollection.Load(top10CustomersResponse);
      }, null);
 We can make code a lot easier to read with an Extension method that takes a URI on the LoadAsync method.
 public static class DataServiceCollectionExtensions
    {
        public static void LoadAsync<TEntity>(this DataServiceCollection<TEntity> collection, 
                              DataServiceContext context, Uri requestUri)
        {
            context.BeginExecute<TEntity>(
                requestUri,
                (asResult) =>
                {
                    var results = context.EndExecute<TEntity>(asResult);
                    collection.Load(results);
                }, null);
        }
    }

Which converts our code to :

 var top10CustomersUri = new Uri("Customers?$top=10", UriKind.RelativeOrAbsolute);
customerCollection.LoadAsync(Context, top10CustomersUri);

I’ll make a more detailed blog post later about the WP7 client library for OData and a sample application too.