I have recently been glued to my screen watching some of the awesome presentations that were shown at the recent PDC 2010. There were some really amazing presentations and one of them really caught my eye. It was a talk about the power of ODATA and the support that Microsoft are building into Visual Studio for it. The presentation was entitled "Inside Some of The Top OData Services" by Pablo Castro. You should still be able to stream the presentation on the PDC player.
One of the things that he showcased was the ability to use Entity Framework and WCF Data Services in order to create an OData API. As I watched him quickly put together an example, I thought to myself that there was no way it could be that easy - Really!? So, I decided to put together a small sample using my Blog's Database. These are the steps that I took.
Firstly, add a new Entity Data Model. Right click on your Project - select Add New Item. I called mine BlogData.
Next, choose Generate from database.
On the next screen, choose your database connection and name your Entity Connection Setting. I called mine "DeanTestEntities", but you could call yours anything.
You should then have a screen displaying a Model Browser of your database entities.
I need to link my two tables together so that I can associate my comments with the relevant blog entry. Right click on your table and choose Add | Association. I added an association between my BlogPost model and the Comment table. They need to be joined on BlogId.
After that, all I needed to do was to add a WCF Data Service and wire it up. Right click on your project and choose Add | New Item | WCF Data Service. We are almost there, all we need to do is wire it up. Once you have created your WCF Data Service you should be presented with some autogenerated code. You will need to replace the following line with the name of the Entity Connection String we used when we created the Entity Data. Mine was called DeanTestEntities.
With the following. I also uncommented the line that contained "SetEntitySetAccessRule". This allows all user to access the API. I hope to talk more about Entity Access rules in a future post.
That's it. We now have a working API that we can start using. Fire up your application. This is what I see when I open the application in my browser.
If I use the href in the data, and update my URL to be http://locahost/blog.svc/BlogPosts. This new URL then navigates to my Blogpost entity and displays like so. Awesome!
I can now use the associations and links in the data to get anything I need. If I try changing the URL to -http://locahost/blog.svc/BlogPosts(2) - I get the blog entry with the Id of 2.
Or if I try - http://locahost/blog.svc/BlogPosts(2)?expand=Comments - I get the blog entry and all comments associated to it. If you remember we set up the associations in the Model Browser.
I can order by - http://localhost/blog.svc/BlogPosts?orderby=Title
Or even get only the value without any xml brackets - http://localhost/blog.svc/BlogPosts(2)/Title/$value
Building this small OData sample around my existing application literally only took about 20-30 minutes. I was really surprised at how easy it was. There are a few more things that I would like to take a look at, but I will try and cover that in future post.
For more information on this awesome feature please take a look at: