Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

OData

0.00/5 (No votes)
15 Sep 2014 1  
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:

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

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

{ 
"$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.

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

{
"$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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here