Introduction to OData

Web is evolving very rapidly and so we have to. And demands of generic things are increasing as well.
We all know now days no application stands alone, because there are a lot of types of devices available
So we have to create services which can be called by any kind of device and business logic can be placed at a single place.
And when it comes to fetch data from a web service method, client devices don’t always need whole data, sometimes needs few columns or sometime needs data filtered on some basis and so on.

So to fulfill these requirements we keep on creating different methods.
And if any other criteria needs by client device, service side code needs to be changed as well.
Hmm, this is not a good approach.
What if, we can create one method for a data, and user can pass search criteria in the url and can have the desired result, Sounds interesting. Isn’t it.
Here is the official site link of Odata.
http://www.odata.org/documentation/odata-version-2-0/uri-conventions/
Let’s look at some piece of code,

[HttpGet]
public IQueryable<PlayerProfile> GetAllPlayer()
{
return unitOfWork.PlayerRepository.GetAll().AsQueryable();
}

This is a piece from API controller giving back all data of players. And url to this will be something like
http://localhost:52345/api/Player/GetAllPlayer

And if we needs some search to implement in this then we need to give parameterized methods like,

[HttpGet]
public IQueryable<PlayerProfile> GetAllPlayer(UserId id)
{
return unitOfWork.PlayerRepository.GetAll().where(x=>x.UserId == id).AsQueryable();
}

And URl will be like
http://localhost:52345/api/Player/GetAllPlayer/12
And so on,
A sample json data from above

{
"$id": "1",
"UserID": 12,
"FirstName": "Tt",
"LastName": "Tt",
"JerseyNumber": "5",
"PlayingPosition": "Centre",
"Shoots": "1",
"HeightId": 10,
}

Here comes OData in Light.
What if there is no need to create any additional Methods.
Only changing url will able to resolve the data needed.
Sounds great.
Let’s see how.

[HttpGet]
[Queryable]
public IQueryable<PlayerProfile> GetAllPlayer()
{
return unitOfWork.PlayerRepository.GetAll().AsQueryable();
}

Here above is the same snippet with one additional attribute [Queryable] and this attribute is from namespace System.Web.Http.QueryableAttribute’
And you are done.
I want same result from this method; now let’s look at new urls.
http://localhost:52345/api/Player/GetAllPlayer?$filter=UserID%20eq%2012
Hmm, url looks a bit messy, but don’t worry. It’s a standardized format nothing specific.
Now look at the result below

{
"$id": "1",
"UserID": 1253,
"FirstName": "Tt",
"LastName": "Tt",
"JerseyNumber": "5",
"PlayingPosition": "Centre",
"Shoots": "1",
"HeightId": 10,
}

You will find that result is exactly same, with single method.
Journey doesn’t ends here. Stay with me a few longer.
This is just filter with UserID, you can have the same to any of property which is in the result set.
Isn’t it great?
http://localhost:52345/api/Player/GetAllPlayer?$filter=FirstName%20eq%20%27Tt%27
http://localhost:52345/api/Player/GetAllPlayer?$filter=LastName%20eq%20%27Tt%27
Like this one.
And this is not all, there is a lot
Just pointing many of the features of OData

  1. Filtering data(searching)
  2. Sorting data(ascending descending based on any column)
  3. Selecting data (top few records) instead of all
  4. Skipping records and then selecting
  5. Inserting updating and deleting data
  6. In which format you need data like JSON,XML,ATOM
  7. And many others

Now, V4.0 is available with many a new exciting features

Happy coding

Written By: Ajay Kumar, ASP.Net Developer, Mindfire Solutions

Advertisements

Posted on July 16, 2014, in ASP.Net, C# and tagged , , , , , , . Bookmark the permalink. 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: