Real-World Web Development with .NET 9 – OData
OData is a standard that makes it easy to expose data via the web to make it accessible to any client that can make an HTTP request.
Websites often need to display data, and that data often comes from relational databases. To improve modularity and reuse, instead of a website directly calling the database, it should instead call a web service. That web service can then be called by other clients, like mobile and desktop apps, as well as the website itself or even other web services.
OData is great for scenarios where a standardized, flexible data protocol is needed—especially for internal enterprise environments that integrate with Microsoft products like Excel and Power BI. It simplifies the development of data-driven applications by offering built-in querying, CRUD operations, and deep integration with many tools.
While there are performance considerations that we will consider, OData’s strengths lie in its standardization, metadata-driven model, and ease of integration with complex relational data systems.
Understanding the OData standard
OData (Open Data Protocol) is an ISO/IEC-approved, OASIS standard that defines a set of best practices for building and consuming RESTful APIs. Microsoft created it in 2007 and released versions 1.0, 2.0, and 3.0 under its Microsoft Open Specification Promise. Version 4.0 was then standardized at OASIS and released in 2014. OData is based on HTTP and has multiple endpoints to support multiple versions and entity sets.
Benefits of OData
OData has many benefits when it comes to simplifying data access over HTTP.
Standardized querying
Unlike traditional ASP.NET Core Web APIs where the service defines all the methods and what gets returned, OData uses URL query strings to define its queries. This enables the client to have more control over what is returned and minimizes round trips. Of course, the OData service controls the scope of those queries, but within that scope, the client has complete control.
For example, with a Web API controller-based web service, an endpoint defined by a controller action might always return all columns from the Products table in the HTTP response, but only data from the Products table, not from related tables like Categories. If a client app needs to show a list of product names with their category names, then the client app might have to make two round trips to call two Web API endpoints and then match up the response data at the client side, throwing away the unneeded data. With OData, the client can request just the columns it needs and from multiple related tables in a single round trip.
OData uses familiar HTTP methods (GET, POST, PUT, and DELETE) and supports rich querying features like filtering, sorting, paging, and projections, which make it easy to interact with data from any OData-compliant service.
OData comes with built-in query options that save developers time in implementing filtering, sorting, and pagination logic. These features are easily enabled, and clients can pass query options directly in the URL, like $filter, $select, $top, and so on, reducing the need for custom server-side logic for such tasks.
领英推荐
For example, the following request retrieves products with a price greater than 100, sorted by price in descending order, and limited to 10 results, as shown in the following URL relative path:
/products?$filter=Price gt 100&$orderby=Price desc&$top=10
Cross-platform interoperability and integration with Microsoft ecosystem
OData is designed to be platform-agnostic, meaning it can be used across various environments and technologies. Whether you are using .NET, Java, Python, or any other platform, you can interact with an OData service using standard HTTP methods, making it easy to integrate systems that may otherwise not communicate smoothly.
Being an open standard with wide adoption ensures that OData services and clients can be easily integrated with existing tools, reducing the overhead of building custom data access layers. This also means you can take advantage of a wealth of community support and documentation.
Supports RESTful principles and CRUD operations
OData is built on top of REST principles, which makes it familiar to developers accustomed to REST APIs. It uses HTTP methods like GET for retrieving, POST for creating, and so on, in a predictable manner, and its stateless interactions align with standard web service design patterns.
OData allows for full CRUD (Create, Read, Update, Delete) operations, which makes it more than just a query language. You can use it to not only fetch data but also modify data on the server, providing a comprehensive data access layer over HTTP.
Supports multiple data formats
OData supports multiple data formats, like JSON, XML, and AtomPub, giving it flexibility based on client needs. JSON is particularly valuable for web and mobile applications due to its lightweight nature, while XML and AtomPub can be useful for enterprise-level integrations.
...and much more
.NET 9 and ASP.NET Core 9 added many other new features. Learn about them in my new book, Real-World Web Development with .NET 9, coming in December 2024. Available on Amazon US at the following link: https://www.amazon.com/Real-World-Web-Development-NET-proven-ASP-NET/dp/B0DK1KGH8R/
Author of multiple .NET programming books
2 个月Amazon US: https://www.amazon.com/dp/B0DK1KGH8R/