Sunday, July 27, 2008

ADO.NET Data Services

In Traditional web applications, you request a page and the the server, process the request, makes some database calls if needed and put your data and HTML together and pass on to the client.This model has now changed with RIA, using AJAX, Silverlight and others

Take for an example a hotel reservation page that uses AJAX for user interactions. Maybe the intital page loads with all available countries in a combo box. and when the user selects a country, all the hotels for that country is retrived through a AJAX call.

Now, this is different form the typical web model, initially by requesting this page, the server send out HTML and presentation logic (javascript in this case for AJAX) to the client. The request for the hotels for a particular country is a pure data call, where only data flows in the form of XML.

This example was just used to demonstrate the change from the traditionl web model, but you would find a lot more pure data calls then just one (like in this case) in real world web applications.

The ADO.NET Data Serveice (formally called Astoria), helps developers in this area, providing a simple but yet a powerful way to expose data centric operations.
Data services, exposes your data as a RESTful services, so that your data could be accessed as a web resource, i.e through URLs.
As RESTful services support the CRUD operations, you can use HTTP verbs like POST to insert data, PUT to update data, GET to retrive data and DELETE to delete data.

for an example, you can access all the hotels of a specific country like this,

Tthis would retrive all the hotels in Sri Lanka. The Country and Hotel in the above URL are conceptual entites in your data source of your domain.

The data from the data services can be retrived in multiple represnetation,presently the data service framework exposes data in JSON, ATOM and in POX (plain old XML).

Building a ADO.NET data services is simple. Listed below are some of the common steps when creating a ADO .NET data service.

1) Create a Entity Data Model(EDM) representing your data. The EDM was introduced part of the ADO.NET Entity Framework.
2) Create a data service class representing your entites created in the EDM.

So, Lets make this concept more concrete, lets say my database contains two table, Country and Hotel. I can create an EDM model pointing to my database.
Now, my generated model would contain 2 conceptual entities called Country and Hotel.
Next step, I create a data service (called hotelservice.svc ) based on the generated EDM model.
Now I can access all the Countries using the URL
You can see that I am actually pointing to the entity that is in my EDM model, which in turns points to the Country table.
I can access the Country with the name India like this (country name is my primary key)

I can list all the Hotels for the country India like this
and so on..

What makes the ADO.NET data services very simple is that, you dont need to write code for each and every operations you provide, think about it, you would have to write a service opeartion, write your SQL query, connect to a database, execute it, get the result set, convert it into a format your client understands and send it back to the client, but with data services, all this is done for you, only things
you might have to do when exposing data in a database would be to create an EDM model and create a data service (just a few line of code, minimum of 2 lines) and the rest is done by the framework for you.

You can expose data that does not inherantly come from databases, by exposing the data to the data service framework through the IQueryable interface (introduced with LINQ).
The point of this artical is just to introduce ADO.NET data services and not to give you an example of a working sample, you can find more about how to create data services here.

What makes data services easy to use is there capabilty to provide common functionalites like filtering,sorting, ordering of data and paging, through request parameters in the url, for an example, If I want all the 3 star hotels in Sri Lanka, I can send out a request like this,
http://www.hotelservice.svc/Country(SriLanka)/Hotels?$filter=Type eq '3star'

The other operations are as easy as this, the framework even provides a parameter called $value, that allows you to retrive only data without the surrounding XML markup.

You can also restrict the types of operations that users can do on the service, like providing user only with read only access to your data.
You can download the CTP version of the ADO.NET here...try it out.


  1. So thats great I got some idea for the project ...thanks bro

  2. thanks bro, i got some idea ,,,nice work