ADO.NET Data Services Introduction
From the early ages of development, the separation
of presentation and data was very necessary and was
considered (and is considered) a good practice. As you
all know, the current trend in the web development
world is building Rich Internet Applications (RIA) and
buzz words like Ajax and Silverlight are very popular today. The RIA technologies
(Ajax, Silverlight and etc) are built on the concept of separation between the
presentation and data in order to build more interactive and responsiveness web
applications. Another trend that is very popular today is building RESTful systems.
REST, Representational State Transfer, “is a style of software architecture for
distributed hypermedia systems such as the World Wide Web” (taken from Wikipedia).
REST isn’t the issue of the post and you can read about the it here if you like.
These trends were the starting point of project Astoria which in days became the
ADO.NET data services project.
ADO.NET Data Services – Project Astoria
”The ADO.NET Data Services framework consists of a combination of patterns and
libraries that enable the creation and consumption of data services for the web” –
(taken from the ADO.NET data services site). The services use URIs to be consumed by
their clients. The data is represented as Atom/APP (Atom Publishing Protocol) or
header. For example, if the application sends a JSON request the response will be built
in JSON notation. The default representation is Atom and it uses a fixed mapping of
entities to XML elements in Atom.
The data service results are built in REST style resource collection
of data that can be address by their URIs. The clients can interact with this data
using HTTP commands such as GET and POST which make the interaction very simple.
The ADO.NET data services will be shipped with VS2008 SP1.
The thing to notice is that in order to build a data service over a source it needs
to be IQueryable. All the interaction between a client and a data service is
made in HTTP protocol to the service host. The request is sent to the service
and the service respond with the requested data.
In order to develop ADO.NET data services you need to have VS2008
installed on your computer. Also, you need to have the VS2008 SP1 beta
installed on your computer. If you don’t have SP1 beta you can download it
ADO.NET Data Services First Example
Lets build our first data service.
In VS2008 open a new web site or web application project.
First we will build a data source for the data service. I could use the EDM wizard to
build a new EDM of a database and integrate it very fast into the data service
but I chose to build a simple custom LINQ provider instead.
I’m building two classes. The first is a simple data structure to hold a Course:
The only interesting thing about the class is the DataWebKey attribute
that indicate that the key of the class is the CourseID. As you can see I
made a static GetCourses method that returns a fixed and static course list.
The next thing to do is to build a class that will be the data context for the
data service. You have to remember that the data context have to return a
IQueryable response in order to use it in data service. The
CoursesDataContext class code:
There is an extension method of IEnumerable called AsQueryable that help
us to return a IQueryable data structure from the list of courses.
The last thing is to build the data service.
From Add New Item menu choose ADO.NET data service and add a new data
service to the project:
After adding the service copy and paste the following service code:
A note – the line of configuration in the InitializeService method gives the user of
the data service all right (edit, delete) and can make a security treat. I used it only
for the demonstration.
After that all you have to do is view the service in the browser and the result
should look like:
You can navigate to the courses by writing a URL like:
The post gave the background for a post series about ADO.NET data services.
Also, I gave a small data service introduction example.
In the next posts I’ll try to answer questions about how to use data services.