Introduction to ADO.NET Data service

No.of Views1034
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : ADO.NET Entity Framework , General
Introduction to ADO.NET Data service
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article will explain theoretical concept of ADO.NET Data Service.
Introduction:
• ADO.NET Data service framework provides an API that allows data to be created and consumed over HTTP using RESTful service.
• ADO.NET Data service supports all database operations using URI.
• ADO.NET Data service can expose an entity model via an URI.
• ADO.NET Data service is RESTful service to support CRUD operations on database.
• ADO.Net Data service could be consumed by any type of client like Windows, SilverLight, Web , AJAX and console.
So, ADO.NET Data service could be defined as,

It exposed all database operations as RESTful service on an entity model, if entity model supports both IUpdateable and IQueryable interface.

Image Loading

How HTTP Verbs being used ?

ADO.NET Data service uses HTTP to interact with data item using the HTTP Methods.

Image Loading

There are three options to make HTTP call.
1. By using Fiddler2
2. By using web browser’s address box
3. By client library. Client library exposed by the System.Data.Service.Client namespace. Client library encapsulates the HTTP Request such that developers have no need to manually craft HTTP Request.

Supported Message format

Image Loading

JSON message format is mainly to work with AJAX clients.
XML is highly readable message format.
ATOM is default message format. It is highly descriptive in nature.


Explanation of database , going to use forCRUD operation 

Image Loading

IPL database has been created. This contains three tables. Details of tables with records are illustrated below. 
Player_Information

Image Loading
Image Loading

Team_Information

Image Loading
Image Loading

Player_Team 

Image Loading

 

Image Loading


"Note: To accommodate URI query explained below , it is assumed that one ADO.NET DataService is added to the web project . To add ADO.NET Data Service, Right click on project and add new Item. ADO.Net Data Service works on an entity model. So To create Entity Model Add New Item and select Entity Model. Name of this entity model class will be used as base class for ADO.NET Service." 

How to create entity model and add ADO.NET services in a project. For step by step explanation, see my ADO.NET Services part 2 article on codegain.com


Querying using URI on database
On running service, following URI will get invoked by the browser.
http://localhost:2888/DataService.svc/

1. localhost:2888 is server and port number where service is running
2. DataService is name of ADO.NET data service.

Result would be in browser as below.

Image Loading

Above service is using ATOM message format 

Metadata
• One feature of ADO.NET data service is that, they provide operation to retrieve Metadata about the service and offer of the service.
• This is very useful in determining structure before requesting them.
• ADO.NET Data service provides metadata accessible.
• Metadata expose all the resource and custom services.
• Metadata could be accessed by appending $metadata to service URI.
So, the URI to access metadata is
http://localhost:2888/DataService.svc/$metadata
And metadata will be in browser as, the below is content in browser

Image Loading

 

URI Query options

Any complex query could we written using URI.
1. Below URI will display record from Player_Information table.
http://localhost:2888/DataService.svc/Player_Information

Image Loading

 

2. Below URI will display record from Team_Information table.
http://localhost:2888/DataService.svc/Team_Information 

Image Loading

 

URI options

Image Loading

 

URI logical operators

Image Loading

URI math operators

Examples : • Below query will fetch record of Team_Information with Team_Id equal to T2

http://localhost:2888/DataService.svc/Team_Information?$filter= TeamId eq 'T2' 

Image Loading

• Below query will fetch all records of Team_Information with Team_Id not equal to T2 

http://localhost:2888/DataService.svc/Team_Information?$filter= TeamId ne 'T2' 

Image Loading

• Below query will fetch top 2 records of Team_Information
http://localhost:2888/DataService.svc/Team_Information?$top=2
• Below query will fetch playerID P1 or P2 from Player_Information table.
http://localhost:2888/DataService.svc/Player_Information?$filter=(PlayerId eq 'P1') or (PlayerId eq 'P2')

Image Loading

Very Important Note:

1. ? $ URI Option = is used in all URI query to fetch records.

2. If Browser is unable to show the results of URI query make sure FEED Reading of browser is OFF. TO do so, in IE Browser click on TOOL-> Internet option -> Content. Select Feeds and Web slices setting. And uncheck Turn on reading view checkbox.

Image Loading

 

Image Loading

 

Access Rules

ADO.Net Data service class must inherit from DataService class.
Where, T is name of the entity model class.

public class DataService : DataService

Here IPLEntities is entity model class. DataService is name of ADO.NET Data service.
By default ADO.NET Data service disable all the access to entity model.
So it need to be enabled, such that client could access the entity model. Developer has explicitly give action to model to access.
SetEntitySetAccessRule(,) method is used to give access. This method accept two parameters.
1. Entity name on which rule to be applied.
2. Enumerator of EntitySetRight

Image Loading

 

Image Loading

 

config.SetEntitySetAccessRule("*", EntitySetRights.All);

This will permit all right access on all entity set.

config.SetEntitySetAccessRule("Player_Information", EntitySetRights.AllWrite);

This will permit all write access on Player_Information entity set. 

Creating the proxy class

There are two way to create proxy class for ADO.NET Data service at client side.
1. By adding service reference of ADO.NET Data service URI.
2. By using ADO.Net client library. To use client library there is need to add System.Data.Services.Client.dll assembly to the project. The two main constructs in the client library are the DataServiceContext class and the DataServiceQuery class.

Conclusion

Above article has explained theoretical concept of ADO.NET Data Service. For step to step explanation of how to create ADO.NET Data service and consume it in different type of clients,Happy Coding Thank you.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
    This article will cover the code-only approach, which provides developers the ability to use the Entity Framework using POCO entities and without an EDMX file. This approach lets developers view code as their model.
    Published Date : 20/Jul/2010
    In Model first approach, we will be creating the EDM, conceptual model, first with respect to the business, and keep evolute the EDM till it fits for the business and then generate the database schema based on the EDM. At least it makes easier to limit the huge changes.
    Published Date : 20/Jul/2010
    In this article, I'm going to explain how to handle Output Parameter in Stored Procedure with Entiry Framework.The Entity Framework is getting popular and many developers love it.
    Published Date : 11/Sep/2011
    There are lots of ways to implement repository pattern but with ado.net entity model you can create repository pattern within 10 to 15 minutes.
    Published Date : 04/Jun/2010
    Microsoft .net framework 4 have usefull enhancements in there Data-services model After the WCf For quite long Microsoft .net Providing such useful services fro client and server side features One of Most Beautful enhancements
    Published Date : 16/May/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top