REST and ASP.NET Web API (Milan)

51
REST & ASP.NET Web API @JefClaes

Transcript of REST and ASP.NET Web API (Milan)

Page 1: REST and ASP.NET Web API (Milan)

REST & ASP.NET Web API@JefClaes

Page 2: REST and ASP.NET Web API (Milan)

Thanks to the sponsors

Page 3: REST and ASP.NET Web API (Milan)

about/jefclaes

@jefclaes

http://jefclaes.be

Page 4: REST and ASP.NET Web API (Milan)

REST

Page 5: REST and ASP.NET Web API (Milan)

The acronym

REpresentational State Transfer

Page 6: REST and ASP.NET Web API (Milan)

REST is NOT...

• ‘Webservices using JSON’

• A protocol• A design pattern

Page 7: REST and ASP.NET Web API (Milan)

REST is..

“An architectural style for building distributed hypermedia systems.”

Page 8: REST and ASP.NET Web API (Milan)

In theory...

REST isn’t limited to a single message protocol.

Page 9: REST and ASP.NET Web API (Milan)

But in practice...

It’s all about HTTP.

Page 10: REST and ASP.NET Web API (Milan)

So now you wonder..

Haven’t we been successfully building web services using SOAP and HTTP

for over 10 years ?

Page 11: REST and ASP.NET Web API (Milan)

BASIC CONCEPTS

Page 12: REST and ASP.NET Web API (Milan)

Resources

• An entity, item, or just a thing you want to expose.

• REST is Resource Oriented.

Page 13: REST and ASP.NET Web API (Milan)

Resources

Page 14: REST and ASP.NET Web API (Milan)

Resources

Page 15: REST and ASP.NET Web API (Milan)

Resources

Page 16: REST and ASP.NET Web API (Milan)

Resources

Page 17: REST and ASP.NET Web API (Milan)

Identifiers

Something that identifies a resource.

ID: 1URI: http://batman.com/api/batresource/1

Page 18: REST and ASP.NET Web API (Milan)

Representations

View on a resource’s state at an instant in time.

JSON:

{   "Id":"1",   "Description":"Batarang",   "Quantity":1}

XML:

<BatResource> <Id>1</Id> <Description> Batarang </Description> <Quantity>1</Quantity></BatResource>

Other:

• Images• CSV• Custom• ...

Page 19: REST and ASP.NET Web API (Milan)

Verbs

Actions on a resource.

Create Read Update Delete

POST GET PUT DELETE

Page 20: REST and ASP.NET Web API (Milan)

Hypermedia

HATEOAS: Hypermedia as the engine of application state

Linking your API together

{ "Id":"1", "Description":"Batarang", “Quantity":1, “Links": { "Rel":"Next","Href":"http://localhost:8080/api/batresource/2" }}

Page 21: REST and ASP.NET Web API (Milan)

HypeRRRRRRRmedia

• Relations• Embedded resources• Reference data• Redistribution of effort• Reduction of payload size• Reflow• Restriction of functionality

Page 22: REST and ASP.NET Web API (Milan)

ARCHITECTURAL VALUESArchitecture of the WEB

Page 23: REST and ASP.NET Web API (Milan)

Scalability and performance

Yes, text-based, synchronous, request-response can be performant.• Stateless• Caching

Page 24: REST and ASP.NET Web API (Milan)

Loose coupling

• No transactions• No state• No guarantees• HATEOAS• No specific technology stack

Page 25: REST and ASP.NET Web API (Milan)

Consistency and Uniformity

Everybody knows how to use HTTP–Constraints–Well understood semantics

Page 26: REST and ASP.NET Web API (Milan)

LEFT-OVERS

Page 27: REST and ASP.NET Web API (Milan)

Richardson’s Maturity Model

Level 0: POX

Level 1: Resources

Level 2: HTTP verbs

Level 3: Hypermedia

Page 28: REST and ASP.NET Web API (Milan)

ASP.NET WEBAPI

Page 29: REST and ASP.NET Web API (Milan)

What?

.NET(4.0) HTTP framework for building RESTful services.

Page 30: REST and ASP.NET Web API (Milan)

When?

• HTTP Services (WCF)

• AJAX back-ends

Page 31: REST and ASP.NET Web API (Milan)

The server

Page 32: REST and ASP.NET Web API (Milan)

Options

• WebHost– ASP.NET MVC: Click, click, click

• SelfHost

Page 33: REST and ASP.NET Web API (Milan)

Starting

Page 34: REST and ASP.NET Web API (Milan)

Mapping Web API to REST concepts

Page 35: REST and ASP.NET Web API (Milan)

A resource

Page 36: REST and ASP.NET Web API (Milan)

A resource

Page 37: REST and ASP.NET Web API (Milan)

Identifiers

Page 38: REST and ASP.NET Web API (Milan)

Identifiers

Page 39: REST and ASP.NET Web API (Milan)

Verbs

Page 40: REST and ASP.NET Web API (Milan)

Verbs

Page 41: REST and ASP.NET Web API (Milan)

Representations

Page 42: REST and ASP.NET Web API (Milan)

Representations

application/jsontext/xml

Page 43: REST and ASP.NET Web API (Milan)

Representations

Page 44: REST and ASP.NET Web API (Milan)

Hypermedia

Page 45: REST and ASP.NET Web API (Milan)

Hypermedia

Page 46: REST and ASP.NET Web API (Milan)

The Client

Page 47: REST and ASP.NET Web API (Milan)

Package

Microsoft.AspNet.WebApi.Client

Page 48: REST and ASP.NET Web API (Milan)

Verbs and asynchrony

Page 49: REST and ASP.NET Web API (Milan)

Summary

• REST–Concepts–Architectual values– Left overs

• ASP.NET Web API– Server–Client

Page 50: REST and ASP.NET Web API (Milan)

There is more

Slides and source on http://jefclaes.be

Extensibility?• Next session (Raffaele Rialdi)• http://github.com/JefClaes/aspnet-webapi-samples-tunisia

Page 51: REST and ASP.NET Web API (Milan)

Please rate this sessionScan the code, go online, rate this session