Open Data Protocol applications for WCF Odata, wcfodata
OData Introduction
Speaking of the WCF Data Service, we have to talk about OData. For a standard Web service, it often provides some functions, such as ordering and returning, and then users use these functions through the HTTP protocol. This is the basic idea of service-oriented. However, services in front of you have some disadvantages. In many cases, you cannot accurately predict what users need. Therefore, you must constantly add new interfaces and modify the returned objects.
Another method is the so-called Resource-Oriented Architecture (ROA), which exposes Web service resources and allows users to query resources in real time, ability to present and integrate data. Similar to querying data in a database using SQL. The only difference is that ROA creates a query through URL.
OData is a protocol that specifies the characteristics of Web services that expose data. The following section defines OData.
Open Data Protocol (OData) is a Web Protocol used to query and update Data. It provides a way to expose Data in applications. OData is used and built on many Web technologies, such as HTTP, Atom Publishing Protocol (AtomPub), and JSON. It provides the ability to access information from various applications, services, and repositories. OData is used to expose and access information from various data sources, including but not limited to relational databases, file systems, content management systems, and traditional Web sites.
OData protocol Overview
As mentioned above, the OData service uses Web Services to disclose the resources provided. Then you can access these resources through URLs. The OData Protocol specifies how to query data through HTTP. The basic principle is that you can enter URL with parameters to query resources.
The following are some OData public services that you can use. For more OData services, visit the OData official website.
- Http://services.odata.org/WebSite/OData.svc/
- Http://services.odata.org/OData/OData.svc/
- Http://services.odata.org/Northwind/Northwind.svc/
These public OData services will be used when you describe how to use the OData protocol for queries. Below with http://services.odata.org/Northwind/Northwind.svc/
For example, enter the URL in the browser and you will see
You can see that the Service provides resources such as Products, Advertisements, Categories, and Suppliers. You can access these resources by entering the following URLs. For example:
- Http://services.odata.org/Northwind/Northwind.svc/Products
- Http://services.odata.org/Northwind/Northwind.svc/Categories
- Http://services.odata.org/Northwind/Northwind.svc/Suppliers
These queries return all data of the resource in XML-Atom format. For example, Products data.
Format output data
The default format is XML-Atom. Of course, you can also use other formats. Currently, JSON format is supported. You only need to add the $ format = json parameter to the URL to obtain data in json format.
- Http://services.odata.org/Northwind/Northwind.svc/Products? $ Format = json
- Http://services.odata.org/Northwind/Northwind.svc/Categories? $ Format = json
- Http://services.odata.org/Northwind/Northwind.svc/Suppliers? $ Format = json
Select fields
By default, all fields are returned. However, you may only need to obtain some other fields. For example, only the ID and Name fields are returned in the following query.
- Http://services.odata.org/Northwind/Northwind.svc/Products? $ Select = ProductID, ProductName & $ format = json
Expand
In many cases, we also need to obtain the associated navigation attributes. Open the following URL: http://services.odata.org/northwind/northwind.svc/?metadata
From the figure below, we can see that Product has three navigation attributes: Category, Order_Details, and Supplier.
The expand parameter can be used to retrieve the data of related navigation attributes. Enter http://services.odata.org/Northwind/Northwind.svc/Products? $ Format = json & $ expand = Supplier
Of course, you can expand multiple navigation attributes at a time, and separate multiple navigation attributes with commas (,). For example:
Ttp: // services.odata.org/Northwind/Northwind.svc/Products? $ Format = json & $ expand = Supplier, Category
Paging
The $ top and $ skip parameters can be displayed by page, for example:
Http://services.odata.org/Northwind/Northwind.svc/Products? $ Format = json & $ skip = 10 & $ take = 10
Filter
You can use the $ filter parameter to filter data, for example:
Http://services.odata.org/Northwind/Northwind.svc/Products? $ Format = json & ProductId gt 4
Sort
You can use the $ orderby parameter to sort data, for example:
Http://services.odata.org/Northwind/Northwind.svc/Products? $ Format = json & $ orderby = Product
Ascending
Http://services.odata.org/Northwind/Northwind.svc/Products? $ Format = json & $ orderby = Product asc
Descending Order
Http://services.odata.org/Northwind/Northwind.svc/Products? $ Format = json & $ orderby = Product desc