REST should be used if it is very important for you to minimize the coupling between client and server components in a distributed application. REST web services are resources on the web that can be used to get specific information.
What it means for you as GrandNode store administrator? It’s simple, Web API gives you access to business data stored in GrandNode database. In the case of our plugin, it is built with ASP.NET Web API and the OData provider technologies. Please note that using Web API plugins require at least basic knowledge about the GrandNode database structure.
In e-commerce each user want fast and optimized applications. In e-commerce each user also leave vast amount of data in your stores. Data which can’t be used is useless, it can be used only when applications and people can access to them. Probably you are wondering how you can access to it? With rescue comes Open Data Protocol, commonly called OData, this protocol is used in our Web API plugin. Goal of this article is to illustrate why OData is the best and why you should use it in your GrandNode.
Let’s start with explanation how OData works. OData has four main parts:
- OData data model – it’s server side model, it generaly means that all data is available on server and client only knows currently requested data. For example specified operations like filtering or sorting are done on the server. In simplification client sends a filtering or sorting request to the server and server shows the returned data.
OData uses the Entity Data Model (shorter version EDM).
- OData protocol – it lets a client make requests to and get responses from an OData Service, which will be desribed later. It’s based on common web protocols like HTTP and Atom by adding the ability to execute queries and return subsets of data in XML or JSON formats.
- OData client libraries – simply enable you to quickly and easily create software that accesses data via the OData protocol.
In our plugin we use ASP.NET Web API OData v4 library. According to OData documentation:
„ASP.NET Web API Odata is a framework that makes it easy to build OData services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API OData is an ideal platform for building OData applications on the .NET Framework.”
- OData service – Basic OData services consist just a feed. If you create more refined services they could have several feeds. It’s important to create document that will list all feeds so client can easily disover them.
Where we use Web API OData in our store? We’ve made a plugin to GrandNode – Web API OData4 for GrandNode.
Supporting OData Query Options in ASP.NET Web API 2
OData defines parameters that can be used to modify an OData query. The client sends these parameters in the query string of the request URI. For example, to sort the results, a client uses the $orderby parameter:
http://yourstore.com/odata/Product?$orderby=Name
The OData specification calls these parameters query options. You can enable OData query options for any Web API controller in your project — the controller does not need to be an OData endpoint. This gives you a convenient way to add features such as filtering and sorting to any Web API application.
OData Query Options
Web API Plugin supports the following OData query options:
Option |
Description |
$expand |
Expands related entities inline. |
$filter |
Filters the results, based on a Boolean condition. |
$orderby |
Sorts the results. |
$select |
Selects which properties to include in the response. |
$skip |
Skips the first n results. |
$top |
Returns only the first n the results. |
For information about $expand and $select, see Using $select, $expand, and $value in ASP.NET Web API OData.
Client-Driven Paging
For large entity sets, the client might want to limit the number of results. For example, a client might show 10 entries at a time, with “next” links to get the next page of results. To do this, the client uses the $top and $skip options.
http://localhost/odata/Product?$top=10&$skip=20
The $top option gives the maximum number of entries to return, and the $skip option gives the number of entries to skip. The previous example fetches entries 21 through 30.
Filtering
The $filter option lets a client filter the results by applying a Boolean expression. The filter expressions are quite powerful; they include logical and arithmetic operators, string functions, and date functions.
Return all products with category equal to “Books”. |
http://yourstore.com/odata/Category?$filter=Name+eq+'Books' |
Return all products with price less than 10. |
http://yourstore.com/odata/Product?$filter=Price+lt+10 |
Logical operators: Return all products where price >= 5 and price <= 15. |
http://yourstore.com/odata/Product?$filter=Price+ge+5+and+Price+le+15 |
String functions: Return all products with “HTC” in the name. |
http://yourstore.com/odata/Product?$filter=contains(Name,'HTC') |
Sorting
To sort the results, use the $orderby filter.
Sort by price. |
http://yourstore.com/odata/Product?$orderby=Price |
Sort by price in descending order (highest to lowest). |
http://yourstore.com/odata/Product?$orderby=Price+desc |
Using $select, $expand, and $value in ASP.NET Web API 2 OData
Plugin with Web API 2 adds support for the $expand, $select, and $value options in OData. These options allow a client to control the representation that it gets back from the server.
- $expand causes related entities to be included inline in the response.
- $select selects a subset of properties to include in the response.
- $value gets the raw value of a property.
Using $expand
When you query an OData entity or collection, the default response does not include related entities. For example, here is the default response for the Categories entity set:
As you can see, the response does not include any products, even though the Category entity has a Products navigation link. However, the client can use $expand to get the list of products for each category. The $expand option goes in the query string of the request:
GET http://localhost/odata/ProductCategory?$filter=CategoryId+eq+2&$expand=Category,Product
Now the server will include the products for each category, inline with the categories. Here is the response payload:
Using $select
The $select option specifies a subset of properties to include in the response body. For example, to get only the name and price of each product, use the following query:
GET http://localhost/odata/Product?$select=Price,Name
Here is the response body:
You can combine $select and $expand in the same query. Make sure to include the expanded property in the $select option. For example, the following request gets the product name and supplier.
GET http://localhost/odata/Product?$select=Name,ProductManufacturers&$expand=ProductManufacturers
Here is the response body:
For more information about the $select option, see Select System Query Option ($select) in the official OData documentation.
Actions and Functions in OData v4 Using ASP.NET Web API 2.2
In OData actions and functions are a way to add server-side behaviors that are not defind as CRUD operations on entities. In GrandNode perfect example could be Clear Cache action.
Leave your comment