OData is an open source, open protocol technology that provides the ability to develop Queryable APIs.
OData helps you focus on your business logic while building RESTful APIs without having to worry about the various approaches to define request and response headers, status codes, HTTP methods, URL conventions, media types, payload formats, query options, etc. OData also provides guidance for tracking changes, defining functions/actions for reusable procedures, and sending asynchronous/batch requests.
Lets see how can we integrate OData into our API’s.
First install the following packages:
1. Microsoft.AspNetCore.OData -Version 7.5.5
2. Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.2
3. OData.Swagger -Version 1.0.0
4. Then make the highlighted code changes in StartUp.cs
5. Then decorate the API method with [EnableQuery] attribute.
That's it. We have now supercharged our API method with OData capabilities.
Before we explore the capabilities of OData, lets check what all data get returned from the GetPost() method.
So this is the basic output JSON structure, without applying any OData features.
Lets look at what all we can do with OData power.
1. $Select()
If we are interested in getting only the titles of the post entries from the output JSON, we can use the ‘select’ feature of OData. I have shown the output as follows:
see, isn’t it cool?
Without OData, we might have to write another API method to retrieve the titles alone from the server side.
Using $select we can retrieve as many properties we want. We just have to separate the properties using a comma.
2. $OrderBy()
If we want to sort the response objects based up on ascending or descending order of any property’s value, we can use the $orderby feature. I have tried to sort the post entries alphabetically in descending order. And the output is shown below:
3. $Filter()
Now $filter is the place where things getting interesting. We can do alot of things using filter. I will try to demonstrate a few.
If we want to retrieve an exact item, we can use the $filter to do that. Here have retrieved a post item having name equal to ‘xUnit’. Also I have utilized the ‘eq’ property to equate the title to ‘xUnit’ text value. There are other things like ‘ne’ which denotes not equal to etc.
Also suppose I want to get the post items whose viewcount is greater than 200. I can issue a filter condition like this:
?$filter=viewCount gt 200
4. Combining multiple conditions
If we want to combine multiple features of OData, we can do that also.
https://localhost:44314/api/Post/GetPosts?$filter=viewCount lt 250 and categoryName eq ‘Sports’
5. Aggregate functions
For using the aggregate function, we need to apply the ‘$apply’ attribute to the query. And then specify our aggregate condition. Here I have tried to retrieve the count of all post objects which falls under the ‘Programming’ category.
https://localhost:44314/api/Post/GetPosts?$apply=filter(categoryName eq ‘Programming’)/aggregate($count as Count)
Conclusion:
This is just an introduction only. Just try using OData in your web API methods, and see how it goes. I plan to cover the OData security in another post.
Source: Medium
The Tech Platform
Comments