Lightweight Approach to Building Web APIs with .NET

27
The Lightweight Approach to Building Web Based APIs with .NET Christian Horsdal Independent Consultant @chr_horsdal http://horsdal.blogspot.com

description

Traditional .NET development frameworks tend to be big, and cover all possible eventualities, and for many projects this is A Good Thing. But for just as many, if not more, projects, a full one size fits all service solution like WCF is just overkill, and adds unnecessary complexity. This is A Bad Thing. In this talk, I'll go over a lightweight, low-ceremony approach to creating a public APIs for the web in .NET. I will show how to use REST and the Nancy framework to create service applications with less code, less cruft and fewer maintenance headaches. We'll follow the super-duper happy path of Nancy, .NET's answer to Ruby's ultra-lightweight Sinatra framework, to create RESTfull API's that takes full advantage of HTTP and it's ubiquity to make building client applications equally enjoyable.

Transcript of Lightweight Approach to Building Web APIs with .NET

Page 1: Lightweight Approach to Building Web APIs with .NET

The Lightweight Approach to Building

Web Based APIs with .NET

Christian Horsdal

Independent Consultant

@chr_horsdal

http://horsdal.blogspot.com

Page 2: Lightweight Approach to Building Web APIs with .NET

Agenda

Who Am I?

What is “lightweight”

RestBucks

REST

Nancy

Page 3: Lightweight Approach to Building Web APIs with .NET

Who Am I?

Independent consultant

Husband and Father

Some who enjoys

Clean code

TDD’ing

When my soccer team wins

Simplicity

Whisky

3

Page 4: Lightweight Approach to Building Web APIs with .NET

4

What is lightweight?

Page 5: Lightweight Approach to Building Web APIs with .NET

What is lightweight?

Low ceremony

Low cruft

Conventions

5

Page 6: Lightweight Approach to Building Web APIs with .NET

What is lightweight?

Open

Agile

Inexpensive

6

Page 7: Lightweight Approach to Building Web APIs with .NET

7

RestBucks

Page 8: Lightweight Approach to Building Web APIs with .NET

8

RestBucks

Page 9: Lightweight Approach to Building Web APIs with .NET

REST

9

Page 10: Lightweight Approach to Building Web APIs with .NET

Richardsons Maturity Model10

Level 3: Hypermedia

Level 2: HTTP Verbs

Level 1: Resources

Level 0: POX-RPC

Page 11: Lightweight Approach to Building Web APIs with .NET

REST - Resources

The basic building blocks of a web API

Anything with a URI

http://restbucks.com/menu/

http://restbucks.com/orders/

http://restbucks.com/order/42/

http://restbucks.com/order/42/payment/

11

Page 12: Lightweight Approach to Building Web APIs with .NET

REST - Representations 12

GET http://restbucks.com/order/19202048/ HTTP/1.1

Accept: application/vnd.restbucks+xml

<?xml version="1.0"?><order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" <links><link uri="http://restbucks.com/order/19202048" rel="http://restbucks.com/docs/order<link uri="http://restbucks.com/order/19202048" rel="http://restbucks.com/docs/order<link uri="http://restbucks.com/order/19202048" rel="http://restbucks.com/docs/order<link uri="http://restbucks.com/order/19202048/payment" rel="http://restbucks.com/docs/order

</links><location>inShop</location><cost>7.60000</cost><items><item><name>Latte</name><quantity>1</quantity><milk>skim</milk><size>large</size>

Page 13: Lightweight Approach to Building Web APIs with .NET

REST - Representations 13

GET http://restbucks.com/order/19202048/ HTTP/1.1

Accept: application/vnd.restbucks+json

{"Location":inShop,"Cost":7.60000,"Items":[{

"Name":"Latte","Quantity":1,"Preferences":{"milk":"skim","size":"large"

}}

],"Status":1,"Links":[{

Page 14: Lightweight Approach to Building Web APIs with .NET

REST - verbs

GET

POST

PUT

DELETE

HEAD

OPTIONS

PATCH

14

Page 15: Lightweight Approach to Building Web APIs with .NET

15

Page 16: Lightweight Approach to Building Web APIs with .NET

Why Nancy?

“Close” to http

Very, very readable code

Very explicit routing

Automatic content negotiation

Embraces modularity

Embraces IoC/DI

Embraces testing

Runs anywhere

16

Page 17: Lightweight Approach to Building Web APIs with .NET

Nancy Basics

public class MainModule : NancyModule{

public MainModule(){

Get["/"] = _ => "Hello from root";}

}

public class SubModule : NancyModule{

public SubModule() : base("subpath"){

Get["/"] = _ => "Hello from subpath";}

}

Page 18: Lightweight Approach to Building Web APIs with .NET

Nancy Basics

Defines which verbs you accepts

HEAD and OPTIONS and automatic

public class MainModule : NancyModule{

public MainModule(){

Get["/"] = _ => "Hello from root";Post["/"] = _ => DoPost(Request.Form.my_value);Delete["/{id}"] = p => Delete(p.id);Put["/"] = _ => DoPut(Request.Body);Patch["/"] = _ => DoPatch(Request.Body);

}}

Page 19: Lightweight Approach to Building Web APIs with .NET

Restbuck on Nancy

Place an Order

19

Page 20: Lightweight Approach to Building Web APIs with .NET

RestBucks on Nancy

View/Cancel/Update

20

Page 21: Lightweight Approach to Building Web APIs with .NET

RestBucks on Nancy

Pay an Order

21

Page 22: Lightweight Approach to Building Web APIs with .NET

RestBucks on Nancy

XML, JSON or YAML

22

Page 23: Lightweight Approach to Building Web APIs with .NET

RestBucks on Nancy

Conditional Gets

23

Page 24: Lightweight Approach to Building Web APIs with .NET

Nancy.Testing

Test through the whole Nancy pipeline

Take advantage of IoC/DI

…Or not

26

Page 25: Lightweight Approach to Building Web APIs with .NET

Why Nancy?

“Close” to http

Very, very readable code

Very explicit routing

Automatic content negotiation

Embraces modularity

Embraces IoC/DI

Embraces testing

Runs anywhere

27

Page 26: Lightweight Approach to Building Web APIs with .NET

Why REST + Nancy

Lightweight

Low ceremony

Low cruft

Follows conventions

Open

Agile

28