Introduction into ADO .NET Data Services/RIA Services
This article is not that complex and will not give you any re-usable code in any shape way or form, but what it will hopefully do is give you a gentle introduction in how to get started with 2 of Microsofts newer data access offerings:
• WCF Data Services, formerly ADO .NET Data Services, formerly Astoria
• RIA Data Services (the new kid on the block)
formerly ['fɔ:məli] adv. 以前；原来This is not an advanced article for people that may have used these 2 data access areas for some time, rather it is a general overview of what can be done with these 2 different data access technologies and how you might go about doing some simple CRUD operations with them.Pre-Requisites
I wrote the attached demo code using Visual Studio 2010 BETA 2 / Silverlight 4 / RIA Services Preview, so the following are all considered to be pre-requistites
• Visual Studio 2010 RC
• ADO .NET Data Services 1.5 CTP1
• RIA Services Preview
And since both the ADO .NET Data Services code and RIA Services code makes use of SQL data, SQL server is also a pre-requisite.
ADO .NET Data ServicesWhat's It All About
ADO.NET Data Services were previously called "Astoria", and put plainly are used to expose data as resources (JSON/ATOM) using REST.
As such the data is exposed as resources, and can be fetched using standard HTTP urls. It is also possible to delete, update and insert new data using REST. The RESTful resources are exposed using WCF under the covers, though looking at a typical ADO .NET Data Service you may not think so.
Since WCF is used as the actual service implementation, all the core logic that fetches data from the database is run on an application server, so can be behind a firewall, which helps maintain that nice 3 levels of physical secure seperation that we may all be used to.
I think this diagram explains it fairly well, where you can imagine the Database is on one box, the Data Access Layer (the ADO .NET Data Service) is on another physical box, and a Client app (such as Silverlight) may be running on yet another different box (basically the clients PC).
By exposing the relational data using REST
we are able to use standard HTTP requests, and make use of various URLs to access the resources. We are also able to use standard HTTP verbs such as PUT/POST to update the resources, which in the end may translate into updating a row of relational data in the relational database (if you are actually using a relational database to expose the data in the first place, which you may not be).
But before we get into that, we need to understand how to create a ADO .NET Data Service. So lets go through that shall we.
Is to define a Entity Model for the data, this is as easy (well for the demo app it is anyway), as creating a new Entity Model, and following the wizard through to completion, using your own SQL Server database connection string, and pointing to the WCFDataServicesDemoApp database you set up using the setup scripts provided, and discussed above.
Once we have a Entity Model we can then create a new ADO .NET Data Service to expose the relational data as resources. Again this is easy all we need to do is add a new item as shown below.
Once you have done that, you will have a new <YOUR_SERVICE_NAME>.svc item in your solution with the following boilerplate code in it.
public class WebDataService1 :
DataService< /* TODO: put your data source class name here */ >
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
// TODO: set rules to indicate which entity sets and service
// operations are visible, updatable, etc.
Believe it not you are nearly there with creating a baic ADO .NET Service. All you have to do it add the generic argument, which is the type of the EntityModel you created earlier, and decide which of your EnitySets you want to expose and what rights you want them to have. So for the demo app (which is an insanely trivial example) this looks like this:
public class WCFDataService : DataService<DemoAppEntities>
// This method is called only once to
//initialize service-wide policies.
public static void InitializeService(
// Define a change interceptor for the Address entity set.
public void OnChangeAddresses(Address address,
if (operations == UpdateOperations.Add ||
operations == UpdateOperations.Change)
if (String.IsNullOrEmpty(address.City) ||
throw new DataServiceException(400,
"Can not save Address with empty City/Line1/PostCode");
Do not worry too much about the OnChangeAddresses() method, I will get to that later. So with this in place we should be able to test this service out in the browser, so we can do that using a number of urls, lets see some:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>