ADO.NET Data Services Introduction

August 10, 2008

ADO.NET Data Services Introduction

IntroductionADO.NET Data Services
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
JSON (JavaScript Object Notation) and the representation is built by the requests
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.

ADO.NET Data Services Framework Architecture
The next figure shows the ADO.NET data services framework architecture:
ADO.NET Data Services Framework Architecture

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.

Environment Prerequisites  
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
here.

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:

    public class Course

    {

        #region Properties

 

        /// <summary>

        /// The course ID

        /// </summary>

        [DataWebKey]

        public int CourseID { get; set; }

 

        /// <summary>

        /// The course title

        /// </summary>

        public string Title { get; set; }

 

        /// <summary>

        /// The duration in days of the course

        /// </summary>

        public int Days { get; set; }

 

        /// <summary>

        /// The course academic credit

        /// </summary>

        public int Creadit { get; set; }

 

        #endregion

 

        #region Methods

 

        /// <summary>

        /// Return a fixed list of courses

        /// </summary>

        /// <returns>A list of courses</returns>

        public static List<Course> GetCourses()

        {

            return new List<Course>() {

                new Course() { CourseID = 1, Creadit = 3, Days = 3 , Title = "Algorithms"},

                new Course() { CourseID = 2, Creadit = 5, Days = 5 , Title = "Data Structures"},

                new Course() { CourseID = 3, Creadit = 5, Days = 5 , Title = "Databases"}

            };

        }

 

        #endregion

    }

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:

    public class CoursesDataContext

    {

        #region Properties

 

        /// <summary>

        /// Return the courses in a IQueryable format

        /// </summary>

        public IQueryable<Course> Courses

        {

            get

            {

                return Course.GetCourses().AsQueryable();

            }

        }

 

        #endregion

    }

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:
Add New ADO.NET Data Service  
After adding the service copy and paste the following service code:

    public class CoursesService : WebDataService<CoursesDataContext>

    {

        // This method is called once during service initialization to allow

        // service-specific policies to be set

        public static void InitializeService(IWebDataServiceConfiguration config)

        {

            config.SetResourceContainerAccessRule("*", ResourceContainerRights.All);

        }

    }

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:
View Service in a Browser  
You can navigate to the courses by writing a URL like:
http://localhost:4205/CoursesService.svc/Courses.

Summary
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.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>