Lightweight Approach to Building Web APIs with .NET

Post on 04-Jul-2015

1,051 views 3 download

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

The Lightweight Approach to Building

Web Based APIs with .NET

Christian Horsdal

Independent Consultant

@chr_horsdal

http://horsdal.blogspot.com

Agenda

Who Am I?

What is “lightweight”

RestBucks

REST

Nancy

Who Am I?

Independent consultant

Husband and Father

Some who enjoys

Clean code

TDD’ing

When my soccer team wins

Simplicity

Whisky

3

4

What is lightweight?

What is lightweight?

Low ceremony

Low cruft

Conventions

5

What is lightweight?

Open

Agile

Inexpensive

6

7

RestBucks

8

RestBucks

REST

9

Richardsons Maturity Model10

Level 3: Hypermedia

Level 2: HTTP Verbs

Level 1: Resources

Level 0: POX-RPC

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

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>

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":[{

REST - verbs

GET

POST

PUT

DELETE

HEAD

OPTIONS

PATCH

14

15

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

Nancy Basics

public class MainModule : NancyModule{

public MainModule(){

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

}

public class SubModule : NancyModule{

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

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

}

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);

}}

Restbuck on Nancy

Place an Order

19

RestBucks on Nancy

View/Cancel/Update

20

RestBucks on Nancy

Pay an Order

21

RestBucks on Nancy

XML, JSON or YAML

22

RestBucks on Nancy

Conditional Gets

23

Nancy.Testing

Test through the whole Nancy pipeline

Take advantage of IoC/DI

…Or not

26

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

Why REST + Nancy

Lightweight

Low ceremony

Low cruft

Follows conventions

Open

Agile

28