20 בדצמבר 2007

ADO.Net Data Services Part 3 – Querying a Web Data Service and WebDataGen.exe

This post is part of a series of posts about ADO.Net Data Services, a part of the ASP.Net 3.5 Extensions. In ADO.Net Data Services Part 1 – Building a Simple Web Data Service I had a step by step guide for building a simple web data service, that exposed resources as IQueryable properties of the DataContext. In ADO.Net Data Services Part 2 – Using Service Operations with WebGet I added some Service Operations to the Data Service, and worked with them. In both of the previous posts I used the browser to request data from the Data Service which was very nice, but not useful in real applications. In this post, I will talk about how we can query the Data Service from a client application. This post is a step by step guide for creating a client application that queries the Blog Data Service I build in the previous posts.

2. Add a new Console Application to the solution, and add a reference the the client side components for ADO.Net Data Services assembly (Microsoft.Data.WebClient.dll that is one of the assemblies that came with the ASP.Net 3.5 Extensions Preview, and can be found in C:\Program Files\Reference Assemblies\Microsoft\Framework\ASP.NET 3.5 Extensions).

Using the WebDataGen.exe tool

3. Generate the client side entities. Open a command windows and navigate to C:\Program Files\Microsoft ASP.NET 3.5 Extensions folder. In order to generate the client classes, we use the WebDataGen.exe tool with the following command:

The WebDataContext.CreateQuery<T> takes the relative path of the resource we want to query, and returns an instance of WebDataQuery<T> which is IQueryable. This means that only when we first enumerate the results, the query will be translated into a web request and be sent to the Data Service. If we hover the query object while debugging, we can see the URI of the request that will be sent to the service in order to perform the query.

6. Query using the generated WebDataContext. One of the classes that WebDataGen.exe has generated is BlogDataContext, which is very similar to the approach taken by the LINQ to SQL DataContext and Entity Framework's ObjectContext.

It is a partial class that inherits from the "untyped" WebDataContext and exposes a strongly typed WebDataQuery<T> objects for the data service resources as properties. The constructors takes the base URL of the service as a "connection string".

LINQ to ADO.Net Data Services

8. Use LINQ to ADO.Net Data Services to query the service. Again, since the WebDataQuery is an IQueryable, you can use LINQ syntax to query it:

var query = (from p in ctx.Posts

where p.BlogID == "bursteg"

orderby p.PublishDate

select p).Skip(2).Take(2);

Conclusion

ADO.Net Data Services provides a rich mechanism for querying a data service from a client application. We start by creating the WebDataContext, and using WebDataQuery objects to perform queries. The WebDataGen.exe tool can generate the client side entities for us.