«

»

ADO.NET Dataservices

ADO.NET Dataservices is a new way to open your data to the web. It’s especially usefull in combination with a Rich Internet Client Application, such as a silverlight application or an AJAX application.

Dataservices is an implementation of WCF but has some restrictions to it.

  • Dataservices makes use of the ATOM or the JSON format to transport the data, no other format is supported.
  • The data is transported over the HTTP or HTTPS protocol and no other protocol is supported.
  • Agarding to authentication you can´t use of the WCF authentication model. You are restricted to the standard authentication model ASP.NET offers you, Windows Authentication and Forms authentication.
  • Dataservices has a REST based interface implementation, witch means a request to the service can only be done using REST.

Getting started with Dataservices.

To get started with Dataservices you first need to define a datasource. This datasource needs to implement IQueryable<T> for the Dataservices to use it. Two commonly used framework that implement this interface are LINQ2SQL and the Entity Framework. For use with Dataservices it’s prefered to use the EntityFramework. The  IQueryable<T>makes it possible for Dataservices to query the data but to be able to update insert and delete data the datasource also has to implement the IUpdateAble<T> interface, wich is implemented by the Entity Framework and not by LINQ2SQL.

Now the datasource is created the Dataservice has to be created. There is a new template for adding the dataservice in Visual Studio 2008 SP1.

New Ado Service

New Ado Service

After this has been added the dataservice needs to be pointed to the datasource, below is a sample of that where a NorthwindDataService  is Created which is pointed to a NorthwindDataContext. In the example the security is also set to read for all entities in the InitializeService method. The security can be set per entity and per method.

public class NorthwindDataService : WebDataService<NorthwindDataContext> {
InitializeService(IWebDataServiceConfiguration config)
{
config.SetResourceContainerAccessRule
("*", ResourceContainerRights.AllRead);
}
}

Dataservices is queried by REST, this can be tested in a browser by calling the service. In the examples below it’s shown how to query the service. For this example the url of the service is ‘http://localhost:5555/Bookmarks.svc’.

The url below request for the model of the data.

‘http://localhost:5555/Bookmarks.svc$metadata’

Metadata

Metadata

To request a list of all the bookmarks put the url below in the browser. Here the ‘/Bookmarksrefers to the entityset Bookmarks.

‘http://localhost:5555/Bookmarks.svc/Bookmarks’

As is shown in the metadata the key of Bookmark is the Id property. Every entity needs a key property. To get just 1 bookmark with id 1 the following url can be used:
‘http://localhost:5555/Bookmarks.svc/Bookmarks(1)’

It’s also possible to get a list of entities based upon a filter. For example get all bookmarks with Tags Devdays. This is done by using keywords, which always begin with a ´$´, in the querystring.

‘http://localhost:5555/Bookmarks.svc/Bookmarks?$filter=Tags eq Devdays’

For Silverlight applications it’s possible to query dataservices using LINQ2Dataservices so you don’t have to create these url’s yourself buth you can just use LINQ to create queries.

Much more information about ADO.NET Dataservices can be found on MSDN.

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>