Slide 1

Slide 1 text

Add OData Support to Your Asp.Net Core Web Api Email : [email protected] Blog : medium.com/@suadev Code : github.com/suadev Tweet : kose__suat Suat Köse Nov 2th, 2019 İstanbul - Dotnetkonf

Slide 2

Slide 2 text

AGENDA ● What is OData and Why Do We Need It? ● OData Query String Parameters & Filter Expressions ● Consuming OData Services ● OData & Asp.Net Core Web Api ● Demo (public repo at github.com/suadev)

Slide 3

Slide 3 text

OData is a protocol that allows creation and consumption Rest Apis in a standard way. By using OData, you can convert your api to a queryable api. What is OData ? (Open Data Protocol) 2007 Initiated by Microsoft. Approved as a standard by OASIS. ASP.NET Core OData released in June. 2014 2018

Slide 4

Slide 4 text

- Allows to create easy to consume RESTful apis. - Provides access with minimal effort to database for CRUD operations. - Filtering, Sorting, Paging and Projection are effortless! - Allows you to focus more on business logic when developing APIs. - Useful Metadata document. (http://localhost/odata/$metadata) Why OData?

Slide 5

Slide 5 text

Why OData? Projection Name City Birth Date Status Record Date William Poirot Tokyo 1972-06-19 Active 2019-06-22 Daniel Nolan Stockholm 2013-12-24 Passive 2018-11-22 http://localhost/api/users http://localhost/api/? Name William Poirot Daniel Nolan Name Status William Poirot Active Daniel Nolan Passive http://localhost/api/?

Slide 6

Slide 6 text

Why OData? Filtering Search box… (name, category and barcode) Price ❏ Under $25 ❏ $25 to $50 ❏ $50 to $100 ❏ $100 to $200 ❏ $200 & Above $ Min $ Max Barcode Product Name ^ Category ^ Price Expire Date ❏ Expired Product List - A complex search api that should respond to all clients ? ... more refinement ...

Slide 7

Slide 7 text

Most Used OData Query String Parameters $select localhost/odata/products?$select=id,productname $count localhost/odata/products?$count=true $expand localhost/odata/products?$expand=category $orderby localhost/odata/products?$orderby=price $skip localhost/odata/products?$skip=5 $top localhost/odata/products?$top=5 $filter localhost/odata/products?$filter=price gt 10000

Slide 8

Slide 8 text

OData Filter Expressions equal $filter=productName eq ‘TV’ less than $filter=price lt 2000 greater than $filter= price gt 1000 and / or $filter=price gt 1000 and price lt 2000 startswith $filter=startswith(productName, ’PC’) length $filter=length(productName) gt 20 indexof $filter=indexof(description, ’LED’) ge 0

Slide 9

Slide 9 text

OData Filter Expressions toupper $filter=toupper(productName) eq '32 INCH TV' trim $filter=trim(category) eq ‘Notebook' day $filter=day(creationDate) eq 12 month $filter=month(creationDate) eq 12 year $filter=year(creationDate) eq 2010 round $filter=round(weight) eq 1 floor $filter=floor(weight) eq 0 ceiling $filter=ceiling(weight) eq 1

Slide 10

Slide 10 text

Consuming OData Services Client Lıbrarıes Plain Query Type-Safe Client Library for .Net

Slide 11

Slide 11 text

Consuming OData Services Metadata localhost/odata/$metadata

Slide 12

Slide 12 text

OData & Asp.Net Core Web Api - Version 3.0 Doesn’t Support 3.0 Yet. .Net Core 3.0 changed routing and OData tied to how Asp.Net does routing. They are working on it. ⚠

Slide 13

Slide 13 text

OData & Asp.Net Core Web Api - Use Cases Your api can consist of ; - Only OData endpoints for all CRUD operations. - OData for Query part and Non-OData for Command part. So, you may want to use OData only for supercharging your api’s Query part. You are also free to add OData support for only some specific services.

Slide 14

Slide 14 text

OData & Asp.Net Core Web Api - Basic Setup 2 3 4 1 Only OData endpoints are exist. OData route is disabled. Returns IQueryable

Slide 15

Slide 15 text

Show me the code !

Slide 16

Slide 16 text

No content