So last month I attended the Richmond .NET User Group meeting where the topic was OData, something that I have of course read about but had never utilized. Besides, attending this meeting would give me a reason to play around with it, hence this post .
Okay we all know that OData (Open Data Protocol) is a Web protocol that can be used to query and modify data. So now that that’s out of the way, this post will cover using OData in a Web application, using LINQ Pad 4 to create URIs for us, and displaying movie information retrieved from the Netflix OData feed.
Creating a WCF Data Service based off of an Entity Data Model
I started off by creating a new Web Application in Visual Studio 2010, adding an Entity Data Model based off of the Northwind database, and then adding a WCF Data Service named Northwind.
By default the code-behind of a WCF Data Service contains a single InitializeService method. This is where you place the rules for accessing your entities:
The next step is to handle the TODOs, which means:
1. Removing the TODO from the implements statement and adding your data source class name
2. Setting rules for your entities in the IntializeService method.
By default all entities are locked down in a WCF Data Service so users do not have access to them. So if I set the Northwind.svc file as my start up page and run the application all I’ll receive is default information via the default URI http://localhost:3574/Northwind.svc/:
You change this by adding access rules for individual or all entities. If the same rule applies to all entities in your model you can use an asterisk (*) to denote this i.e. if I wanted to allow someone read access to all of my entities I would add the following access rule:
Now if I run the application I receive a listing of all of the entities in my model:
If I want to view Categories I could simply use the following URI: http://localhost:3574/Northwind.svc/Categories and receive the following results:
Notice that the unique ID for an entry is a URL and column values are listed in the properties section.
We can control the type of access that users have to our entities using the EntitySetRights enumeration which has the following options:
URI’s and Querying
If I want to retrieve a particular category by ID I would use the following URI: http://localhost:3574/Northwind.svc/Categories(1) which yields the following:
The following query retrieves products for Category 1 and orders the result set by ProductName and Unit price:
Using filters ($filter)
Ex. Retrieve a list of product names and associated prices that have a unit price that is greater than $20:
Creating URIs via LINQ queries using LINQPad 4
1. Add a new Connection
2. Select the WCF Data Services (OData) driver
3. Click Next
4. Enter the URI to your service. Here we will use the Netflix OData Catalog API (Preview)
5. Click OK
6. You should now see the service reference listed as a connection
Now that you have your connection you can simply write LINQ expressions and select the SQL tab option to view the URI.
Example: Retrieve a list of movie titles released before 2000 LINQ query results:
Example of a simple UI that displays Netflix OData feed results
In this example I’m simply calling a method that retrieves top 5 movies released after 2009 in alphabetical order. I am then displaying the cover, title, and synopsis of each movie returned.
The Wrap Up
As you can see OData allows you to query data entities via URIs and you can utilize LINQPad 4 to generate URIs for you by simply writing LINQ queries.