Skip to content

An OData Journey in ASP.NET Web API Part 1 – An introduction to OData

December 5, 2012

Disclaimer: OData is much more complex and encompasses much more than I will be covereing here. I will be focussing on it’s use in ASP.NET Web API, and how it can be used in RESTful service calls over http. For full information you can visit http://www.odata.org/

What is OData?

For the purpose of this introduction, and in the context of .NET, I like to think of OData as ‘Linq-to-Querystring’. Essentially it is a query expression protocol that is designed to faciliate CRUD  (CReate, Update Delete) operations and specifically to be used as part of a URI. The specified expressions can be mapped onto server side elements such as Entity Framework queries in order to return the data that the client requests.

OData query options go on the querystring, conventionally start with a ‘$’ symbol and are seperated by ampersands. A simple URI containing an OData expression may look something like this (you can click the link to see the results, and change the parameters as you wish to see the effect):

http://services.odata.org/OData/OData.svc/Products/?$skip=2&$top=1

spc

This URI utilises two OData query options, skip and top. These tell the service that when returning our data it should skip the first two product resources, and then return the top 1 after that. We can use these to implement simple paging of the data. But the fun doesn’t stop there… we have a whole host of other query options at our disposal such as:

  • $inlinecount  – Specifies that the response include a count of the total number of records
  • $orderby=Rating – Specifies that the data in the response should be ordered by the Rating field.
  • $filter=Price le 200 and Price gt 3.5 – Specifies that results should be filtered to return only those where price is less than 200 and greater than 3.5
spc

The filter query option is very powerful… supporting syntax for all the operators you would usually expect including brackets, logical expressions and even a whole host of data related functions. What more could one ask for!? Here are just a few examples:

  • $filter=not endswith(Description,’milk’)
  • $filter=month(BirthDate) eq 12
  • $filter=tolower(CompanyName) eq ‘alfreds futterkiste’

There are many more options than this, but for the purpose of this introduction they have been omitted. The full list of URI conventions and filter options can be found here:  http://www.odata.org/documentation/uri-conventions.

So all in all, it allows us to do pretty well anything we might do in a simple LINQ to Entities or SQL query. Bargain!

spc

Providing OData Services in ASP.NET Web API

I am not going to go into detail about what you can do here, as there is already a great number of posts on the matter, particularly this post by Alex D James: http://blogs.msdn.com/b/alexj/archive/2012/08/15/odata-support-in-asp-net-web-api.aspx.

In short though, OData support can added to your API via a nuget package: Microsoft.AspNet.WebApi.OData and can be enabled then simply by adding the [Queryable] attribute to your controller action like so:

[Queryable]
public IQueryable<Supplier> GetSuppliers()
{
    return _db.Suppliers;
}

For most people this will work extremely well, takes almost no time to set up, and when you are finished you can use most of the functionality OData has to offer. Which is quite a fantastic result! There is however one fairly substantial caveat to this at present, and that is that this will only work out of the box if you are using the Entity Framework…. and more importantly only if your model is strongly typed

DIY

So now we come to the real purpose for this post, and what we are leading up to in parts 2. Suppose that we are not using entity framework or even SQL for our datastore… suppose instead we are using a No-SQL solution such as MongoDB or Raven. And further to that what if our data store is not strongly typed?

The former is supported (albeit in complicated form) by the Microsoft offering provided we have some form of meta data describing our model. This is also detailed in Alex James’  post. But if our data is loosely typed then we are up the proverbial creek without a paddle.

Join me for Part 2 where I will explore a way in which we can take control and find a solution to the problem of loosely typed OData by using the Linq to Querystring library.

spc

Pete

spc

(Postface)

It’s always been an aim of mine to pitch this blog at all levels of ability… some of you may scoff, but I mean we’ve all been there right? Trying to get your head around something by reading a post that assumes far too much, giving you the complex solution but not the basic knowledge needed to actually make it work?

However technical we may be or however ‘famous’ we are on twitter, we all started out this way once and I think it’s important not to forget how important it is to help beginners. What I actually want to blog about in this instance is complex. It’s also quite a lot of fun and even a bit old-school… but I’m choosing to build up slowly. This post may well be too simple for some tastes, but I urge you to stick around as that won’t always be the case.

spc

References:

http://www.odata.org/documentation/uri-conventions
http://blogs.msdn.com/b/alexj/archive/2012/08/15/odata-support-in-asp-net-web-api.aspx
http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData
http://en.wikipedia.org/wiki/Open_Data_Protocol
http://www.antlr.org/

Advertisements

From → OData, Web API

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: