Monday, April 5, 2010

A tour around Open Data Protocol (OData)

It should come as no surprise that the amount of data collected in many places are hard to get to. Given the ubiquity of web technologies, the need for information from the silo’ed databases, file systems and static web sites are increasing more than ever. Google has already established its own GData and offering solutions. Representational State Transfer (REST) and the simplicity of web protocols are also driving these initiatives.

OpenDataProtocol

Microsoft recently rebranded its ADO.NET Data Services (formerly known as “Astoria”) to OData. I have been dabbling with the OData recently and it is quite impressive what is possible just from out-of-the-box. Then again I haven’t built a full-fledged application yet, but first looks are pretty decent.

OData comprises a bunch of well-established protocols helping the creation of HTTP-based data services. It enables access and exposure of variety of sources (database, file systems and web sites). For in depth look, please check out Odata.org

As of today, using Visual Studio 2010 RC, we can build applications and there are few blog posts out there. On VS 2010, start by creating a ASP.NET Web Application from File –> New –> Project, and choose ASP.NET Web Application (see below).

CreateNewSampleWebOData

Next add a new ADO.NET Entity Model for the project. Right-click project and say “Add –> New” and choose ADO.NET Entity Model.

ADONETEntityModel

Give it a proper name. In this example, I am going to use the Northwind database since it is pretty common for SQL Server and at one point, shipped with SQL Server. Now you can download it from here. Once you run the installer you need to attach the database files to your pertinent SQL Server instance.

The wizard will walk you through the Generation of EDMX file. First "Generate from database” option, then point to your SQL Server instance and pick Northwind database.

As shown below, I used “Categories” and “Products” tables for this.

GenerateDatabaseObjects

Now pay attention to your Model namespace, because this will impact your Web.config file. Subsequent attempts of doing the same steps will clutter your file, and you may end up with **Model1, **Model2 names in your configuration file. Click “Finish”. Now you have created a simple ADO.NET Entity model. It is time to expose it via WCF Data service.

In the same web project, add new WCF Web Service. Right-click on the project, and “Add –> New Item…” and choose “WCF Data Service” under “Web” tab. I am starting to like the VS2010’s organization of items, but sometimes things still don’t make sense, or “haven’t adjusted to it yet”.

CreateNewWCFDataService

Once you have created the service, go ahead and open it up source editor. Notice the first line. Change it to say the name of your entities.

public class WcfDataService1 : DataService<NorthwindEntities>





Next, add the below two lines to indicate you are setting these access rules on these entities.



config.SetEntitySetAccessRule("Categories", EntitySetRights.All);
config.SetEntitySetAccessRule("Products", EntitySetRights.All);


Those two lines in place, you can check your work up-to now by simply setting the service as “Start page” and view the results in browser. (shown below for IE-8).



baseEntity



Finally to consume and query this service, we can create a simple console application and add it to this project. I wrote this simple function to display products on the query window (borrowed a few ideas from Scott Hanselman’s a much better and elegant “Creating an OData API for StackOverflow including XML and JSON in 30 minutes”. For in depth coverage, please check out his blog.



First we need to add a service reference, right click on the Console project and choose “Add Service Reference”. You have to copy and paste the web service URL you created in the previous step. Paste the address and hit “Discover”. Again pay attention to your name spaces.



TechMasterService



Again if you think this is too simplistic, you are not alone. At some point we have to worry about security/credentials and maybe performance, but not right now. But impressive what you can accomplish with a few mouse-clicks out of the box.



For VS-2010 RC, you can also download and install “Open Data Protocol Visualizer” from “Tools –> Extension Manager” menu item. Once that is place, it is pretty slick to see it in action.



Right click on the service name from the Solution Explorer and choose “View in Diagram” option. This will open up a nice canvas for object visualization. Notice the new tab called “Open Data Model Browser” . Very nice I thought. You can drop your entities and look at your columns, etc.



OpenDataModelBrowser



Finally to query referenced entity model, I added below to the start-up of this console application.




   1:  class Program

   2:     {

   3:         static void Main(string[] args)

   4:         {

   5:             CallingODataService();

   6:         }

   7:   

   8:         public static void CallingODataService()

   9:         {

  10:             var tm = new TechMasterEntities(

  11:                 new Uri(http://localhost:xxxx/TechMasterService.svc));

  12:   

  13:             var products = from p in tm.Products

  14:                            select p;

  15:   

  16:             foreach (Product p in products)

  17:             {

  18:                 Console.WriteLine(p.ProductName);

  19:             }

  20:   

  21:             Console.ReadLine();

  22:         }

  23:     }



Don’t forget to change your port number to your own as well as your service name. The partial results from the console are shown below.

partialResults
That is pretty much it for now. Looking forward to finding more about oData protocols and exposing silo’ed data using HTTP protocol in secure and simple ways. Happy Coding!

1 comment:

  1. There are various online sources to provide you informative details on this topic, but this is one is very helpful.

    OData Development

    ReplyDelete