Building a OData Service


Open Data Protocol is popularly known as OData. The definition from the OData.Org is as mentioned below

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 HTTPAtom Publishing Protocol (AtomPub) and JSONto provide access to information from a variety of applications, services, and stores.”

We will see how to build a simple OData Service which can be consumed my applications. To build OData Service, we need to ensure that the WCF Data Service is installed. If not, install the software as mentioned here  http://msdn.microsoft.com/en-us/data/ee712906 .

In this article we will use the “Product” and “Category” table in the NorthWind Database. In case you don’t have the NorthWind database, you can download the same from http://www.microsoft.com/download/en/details.aspx?id=23654

For this article, I will be using the Visual Studio 2010. To begin with lets create a new ASP.NET or ASP.NET MVC project.  After which we shall Add a “WCF Data Service” as shown below





We will name it as “MyODataService”. After adding, we will see the sample code in the file as shown below



We will add an ADO.NET Entity model and call as “NorthWindEntities” for the two tables as mentioned earlier as seen below
As seen above we will add our entity to as the data source as shown below






Let’s now browse the service and the output is as shown below. This is more like a simple atom feed.




Now let’s change the config to set one of the entities, let’s use the products from the entity model. For this let’s modify the code as shown below




Let’s browse the service and the output is as shown below. Here we now see a collection for the products entity.






If we browse by the products, the output will be as shown below. The feed for all data from the products table is seen.





This is the simple OData Service that we can create. We can query against the data.

Lets get all the products which has UnitPrice  less than 20, the result is as seen below. 





For more help on the various query options, please take a look at http://msdn.microsoft.com/en-us/library/ff478141.aspx

 Hope this was useful. Please do drop your comments.

Comments

Popular posts from this blog

Jesus - God Thinks of you.

ASP.NET 4.0 Feature - URL Routing

Tips on JQuery Intellisense in VS 2008