Application programming has seen an interesting evolution. We first moved
away from developing applications that were tightly coupled to the OS and
programming language to decoupled ones. So users were no longer tied down to a
single machine to access an application. They could access it from any platform.
This has made the application into a service that can be accessed from anywhere.
However, in both these cases, the data remains tightly coupled with the
application, irrespective of where you access it from. So the next obvious step
is to decouple the data from the application, so that it can be accessed by
other applications as well. This is in essence what ADO.NET is all about. It
allows various applications to access various data sources like SQL Server, XML,
or even those exposed by OLE DB, and modify or manipulate the data in them. This
makes data usage much more flexible. We'll take a hypothetical example to
explain this benefit. Consider a school where the student attendance, once
taken, is fed into a database. In the traditional way of working, one would run
a query on this database and pull out various reports like how many days has a
student been absent, which class has the best attendance, etc. This could then
be shared with parents in a PTM. With ADO.NET, data service, this same data
could be made available to parents over web on demand, or further, to their
mobile devices if required. Sounds interesting? In this article, we'll tell you
how to implement it.
Direct Hit! |
Applies To: .NET developers |
Implementation
To implement ADO.NET Data Service, you need Visual Studio 2008 SP1, a database
and a web browser client. We used the 'School' database that comes with SQL
Server 2008. Start by creating an ASP.NET Web Application project in and name it
ADODS_Sample1. We have used C# for creating this. The first step is to create a
conceptual model known as the Entity Data Model (EDM) of the sample database.
This can be done by clicking on Project>Add New Item and then selecting ADO.NET
Entity Data Model. After this, simply follow the wizard (check out our Nov 2008
issue for in depth explanation of this). This EDM will act as layer between the
database and client from which the data would be picked up. After creation of
'School.edmx' file, you have to create the data service that will expose data as
service to be picked up by any client.
To create a data service, click on Project>Add New Item and this time add
'ADO.NET Data Service' and name it 'School.svc'. We now have to make changes to
the 'School.svc.cs' file. Here is the code snipped to do that:
To create a data service, click on 'Project>Add New Item'. Select 'ADO.NET Data Service'. We have named it 'School.svc'. |
using System.Data.Services;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
namespace ADODS_sample1
{
public class School : DataService< SchoolEntities >
{
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
}
}
}
To display output of the project in browser, click on 'Tools>Internet Options' and then go to 'Advanced' tab, uncheck 'Disable script debugging (Internet Explorer)'. |
To display output of the project, one has to make certain changes in browser
settings. In Internet Explorer, go to 'Options', click on 'Advanced' tab, and
under settings go to 'Browsing'. Uncheck Disable script debugging. Next click on
the 'Content' tab and then click on 'Settings'. Uncheck 'Turn on feed reading
view'. Press 'F5' to start debugging, browser will open with following URL:
http://localhost:2995/School.svc/
Here you will find XML representation of the data that can be used by any
client. Now to drill down further, one can find data related to 'Department' and
'OnlineCourse' tables using following URI's:
http://localhost:2995/School.svc/Dept
http://localhost:2995/School.svc/OnlineCourse
To get final output in XML format one needs to uncheck 'Turn on feed reading view'. This check box is in 'Content' tab (click on 'Settings') of Internet Options |
Similarly, other URIs can be provided to clients so that they can look for
data as per their needs. Moving one step ahead, one can also display data
associated with 'DepartmentID' (valued 1) in other table named 'Course'. The URL
for this is:
http://localhost:2995/School.svc/Department(1)/Course
Using ADO.NET Data Service, we have shown here is how data represented as EDM
is exposed as a web service. The same data can be exposed via RSS feeds as well.
Plus, apart from converting URIs to Entity Framework Object Services method
calls, you
can even use XML documents, web service etc.