what is ODATA with example ?

OData is the Open Data Protocol. It is an open web protocol started by Microsoft to expose data using existing web technologies. This is based on Representational State Transfer (REST) full architecture. HTTP, AtomPub (similar to RSS, but specifically introduced special XML format for OData), JSON are all supported.

Here, the URL is the primary operator on the data query.


Example of OData Service has been used:

Last (2010) football world cup (S.A), the scoring website was done using an OData Service.


Why should we use an OData Service? 

  1. OData is based on the REST architecture, so we can retrieve data based on an URL.
  2. It also supports HTTP, Atom Pub as well as JSON format.
  3. It has support for any type of data source. Even you can use a custom class as a data source.
  4. No need to create a proxy service object. So, it is lightweight to use.
  5. You can create your own custom methods and expose it.
  6. Since it is lightweight, the interaction between server and client is fast. Thus, performance is good.
  7. It offers full CRUD support by using the different HTTP methods:

GET: Gets one or many entries.

POST: Create a new entry.

PUT: Update an existing entry.

DELETE: Remove an entry.


The following are the OData query options that ASP.NET Core WebAPI supports

$orderby :

Sorts the fetched record in a particular order like ascending or descending.


$select :

Selects the columns or properties in the result set. Specifies which all attributes or properties to include in the fetched result.


$skip :

Used to skip the number of records or results. For example, I want to skip the first 100 records from the database while fetching complete table data, then I can make use of $skip.


$top :

Fetches only top n records. For  example,  I want to fetch the top 10 records from the database, then my particular service should be OData enabled to support the $top query option.

$expand :

Expands the related domain entities of the fetched entities.

$filter :

Filters the result set based on certain conditions, it is like where clause of LINQ. For example,  I want to fetch the records of 50 students who have scored more than 90% marks, and then I can make use of this query option.

$inlinecount :

This query option is mostly used for pagination on the client-side. It tells the count of total entities fetched from the server to the client.


Steps to Use ODATA in asp.net core and web API.


1. Install the NuGet Package

ASP.NET Core OData Nuget package  and ASP.NET Core NewtonsoftJson

2. add model class

3. Register service through Dependency Injection (DI) and also register OData Service.

4. Register Odata EndPoints.

5. Create an empty  API Controller to implement the GET method under EmployeeController and here EnableQuery attribute enables an endpoint to have OData Capabilities.


public void ConfigureServices(IServiceCollection services)  

        {  


            services.AddControllers().AddNewtonsoftJson();  

            services.AddSwaggerGen(c =>  

            {  

                c.SwaggerDoc("v1", new OpenApiInfo { Title = "OData_API", Version = "v1" });  

            });  

            services.AddScoped<EmployeeService>();  

            services.AddOData();  

        }  


  1. [HttpGet(nameof(GetData))]  
  2.         [EnableQuery]  
  3.         public IActionResult GetData() => Ok(_employeeService.GetEmployees());  
  4.     }                                                                      


Let's run the code and test with Postman
 
$select 





Comments

Popular posts from this blog

What is Entity Framework?

Explain the ways to increase the performance of EF.