Introducing ADO.NET Data Services
page 3 of 5
by Sergey Zwezdin
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 30099/ 87

What is ADO.NET Data Services

ADO.NET Data Services is a platform to build services based on REST-approach. It is a complex tool which allows construction of a complicated REST-service thanks to usage of an "out of box" in several minutes.

As it has already been said, REST-services work via HTTP/HTTPS. ADO.NET Data Services work is also based on this protocol. Besides that, a universal syntax URI is supported there. It allows us to address any part of the information from a data model. This is the reason of unavailability to modify format of URI working with web service.

To create a new web service based on ADO.NET Data Services it is necessary to have a ready-made data model. It can be LINQ to SQL, ADO.NET Entity Framework or another data model. As a model of data is ready, you should use tools of Visual Studio to add a new web service in a web application.

Figure 1: Creation ADO.NET Data Services web-service

 

When you create a service it represents as an inheritor of DataService generic-class. In a generic-parameter it is necessary to specify a type which defines a data context. As a result of a service creation, a static method InitializeService, which is required to configure a web service, appears. An object of IDataServiceConfiguration type, which allows us to change service options, is sent as parameters of this method. Thanks to first acquaintance with ADO.NET Data Services it is necessary to grant full access rights to all collections in data models. In real projects you should specify containers explicitly and they will be accessible via a web service. Thus, the code of a service which gives access to a data model will look as follows.

Listing 1 - Definition of simple web-service

public class WebDataService1 : DataService<NorthwindEntities>
{
      public static void InitializeService(IDataServiceConfiguration config)
      {
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
      }
}     

As a result of access to a service it will generate an answer in AtomPub format. This response will contain a list of accessible tables.

Figure 2: Response from web-service

You can access to any tables of this data model now thanks to changing address. For example, access to the address "http://localhost:9580/WebDataService1.svc/Customers" allows us to receive the list of objects from the table "Customers." When you specify values of a primary key in parentheses, you can get access to a single entity. For example, getting access to the address "http://localhost:9580/WebDataService1.svc/Customers('ALFKI ')" web service will return data only for one entity. More over, it is possible to refer to a field of this entity. To do this you need to specify a name of a field after the slash. For example, you can apply to field "CompanyName" using the address "http://localhost:9580/WebDataService1.svc/Customers('ALFKI') / CompanyName."

Operations of data sets manipulation - sorting, filtration and paging - are accessible on a server side. For these purposes use operators "$orderby," "$filter," "$top," and "$skip." Here we have some examples.

Listing 2 - Sample queries to web-service

/Bookmarks?$orderby=Name
/Bookmarks?$filter=Created gt '2007-05-07'
/Bookmarks?$top=10&$skip=30

Building a service side is only half of a work. An important part is client building. To create a client within .NET applications you can use "Add Service Reference" wizard.

Figure 3: "Add service reference" window

A proxy-class will be generated on a client side as a result of adding a link to a service. This class will translate accesses to the web service and work with them like with a simple collection.

Listing 3 - Sample of client code

var client = new NorthwindEntities(new 
      Uri(@"http://localhost:9580/WebDataService1.svc/"));
foreach (Customers customer in client.Customers)
{
}

In this service all collections implement IQueryable interface. For this reason you need to create a LINQ-queries to these collections; they will be transformed into URI which corresponds to ADO.NET Data Services syntax. For example, you can get a sample page using syntax of LINQ.

Listing 4 - Sample of paging

var client = new NorthwindEntities(new Uri(@"http://localhost:9580/WebDataService1.svc/"));
var query = client.Customers.Skip(40).Take(20);
foreach (Customers customer in query)
{
}

At execution of a similar inquiry, it will be transformed in corresponding URI. It is well visible at the debugging of this code.

Figure 4: Transformation of LINQ-query

After execution of a query, the service returns data in AtomPub format that will be parsed by a client and presented as objects.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-08-19 4:34:50 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search