Click here to Skip to main content
15,905,229 members
Articles / Web Development / ASP.NET
Tip/Trick

OData

Rate me:
Please Sign up or sign in to vote.
4.71/5 (10 votes)
15 Sep 2014CPOL2 min read 24.5K   15   4
OData

Introduction

Web is evolving very rapidly and so we have to as well. And demands of generic things are increasing as well.

We all know nowadays no application stands alone, because there are many 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 they need few columns or sometimes, they need data filtered on some basis and so on.

So to fulfill these requirements, we keep 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 have the desired result, Sounds interesting. Isn’t it?

Here is the official site link of Odata.

Let’s look at some piece of code:

C#
[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 api/Player/GetAllPlayer.

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

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

And URl will be like:

api/Player/GetAllPlayer/12 

And so on:

A sample json data from above:

JavaScript
{ 
"$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 be able to resolve the data needed.

Sounds great.

Let’s see how.

C#
[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 the same result from this method; now let’s look at new URLs.

  • 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:

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

You will find that result is exactly the same, with a single method.

The journey doesn’t end here. Stay with me a little longer.

This is just filter with UserID, you can have the same to any property which is in the result set.

Isn’t it great?

  • api/Player/GetAllPlayer?$filter=FirstName%20eq%20%27Tt%27
  • 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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Infocouture Solutions Pvt Ltd
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionGood Article - Update to OData Version 4 Pin
John Willson16-Sep-14 9:47
professionalJohn Willson16-Sep-14 9:47 
GeneralRe: Good Article - Update to OData Version 4 Pin
me.ajaykumar16-Sep-14 16:59
me.ajaykumar16-Sep-14 16:59 
QuestionShort and sweet, but Pin
Meester Over11-Sep-14 13:41
professionalMeester Over11-Sep-14 13:41 
AnswerRe: Short and sweet, but Pin
me.ajaykumar16-Sep-14 16:59
me.ajaykumar16-Sep-14 16:59 
Thanks,

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.