People all around the world want to share data. They want to share their interests with others. Companies want to share data too. For example, EBay wants to distribute data of latest products and interests to the web and enable other companies to use that data in their own websites. There were ways to do that:
- RSS/ATOM feeds.
- XML Web services.
But each of the items above has their own problems and both of them have lots of limitations. In fact, there is no way to query the source’s database. In the EBay example, it’s impossible to select some data with specific parameters except the EBay has been developed some methods in order to that but it’s not enough in some cases, as you know.
But there’s a new way to distribute data and that’s OData!
OData is the short form of “Open Data Protocol” which is a new standard way for distributing data. The result of OData is standard XML so you can access to it using any programming language/tools. Here’s the OData definition from its official website:
The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores. The protocol emerged from experiences implementing AtomPub clients and servers in a variety of products over the past several years. OData is being used to expose and access information from a variety of sources including, but not limited to, relational databases, file systems, content management systems and traditional Web sites.
Companies have started to use OData too; for example you can access Netflix’s latest movies and use them in your application using OData! In order to do so, visit http://odata.netflix.com.
The table below shows the libraries which currently support OData:
You can download any library you want depend on what programming language you’re working with. As you can see in the table above, .NET Framework 3.5 SP1 and 4 is supported. In order to use OData in .NET 3.5 SP1, you have to download and install Data Service Support for .NET Framework 3.5 SP1 and then start working with but if you’re developing your application based on .NET Framework 4, you don’t need anything at all because Data Services is supported built-in.
In order to get more information about OData standards and documentations, you can checkout: http://odata.org.
WCF Data Services
WCF Data Services is the implementation of OData in .NET Framework. Hopefully, Microsoft has done a great job on this and it’s very easy to use. Data Services is not dedicated to databases; in fact it’s all about entities. You can build any entities and access it via Data Services. For example you can create an Entity Framework 4 entity and the access it via a Data Service. It’s the same story for LINQ to SQL, XML, etc.
As an example, I decided to create a Data Service for my EF 4 database model. Here are steps you can create a Data Service:
- As I mentioned, I preferred to use LINQ to Entity in my project so first you need to do is to create a database model by creating an ADO.NET Entity Data Model.
- After creating the database model, it’s time to create the data service. There’s nothing else to do! If you want to create a data service, you can add new WCF Data Service file to the project.
- After a WCF Data Service file to your project, Visual Studio will create a .NET class which is inherited to DataService class. The DataService class in a generic class so you have to assign a data source to it. If you’re using EF, all you need is to type the name of your entity. For example “NorthwindEntities”.
- Then you need to set permissions for the data service. The WCF data service that is created by Visual Studio contains a static InitializeService method that has an input parameter as a DataServiceConfiguration class which includes some methods in order to set permissions for the data service. The most important method for setting permissions is SetEntitySetAccessRule that you can set access rule for your entity. Here’s an example:
- As you can see, SetEntitySetAccessRule accepts two parameters. The first parameter is the name of the entity as string and the second parameter is the entity set rights. In the example above, the entity set rights is set to AllRead which means that users can only read from this entity. If you want to allow users to read and also write data using your service, you can set the entity rights to “All”.
Now your service is ready to use! It’s that easy! You can add your service to another application as same as adding a WCF service and use it. But the difference is that the service is using OData to publish the data. Also you can use LINQ expressions to access whatever you want!
Unfortunately, some of the lambda expressions are not supported by WCF Data Services. For instance, you can’t use Single method like this:
var x = myService.Table.Single(p=>p.Id == 1);
You have to use Single like this:
var x = (from i in myService.Table where i.Id == 1 select i).Single();
I hope Microsoft fix this problem as soon as possible.
I’ll post about WCF Data Service in my future posts but for now if you want to know more about it, you can check out WCF Data Service Client Library in MSDN