26
Click to edit Master title style • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level 14-06-22 1 ANUG December 2011

Nancy - A Lightweight .NET Web Framework

Embed Size (px)

Citation preview

Page 1: Nancy - A Lightweight .NET Web Framework

Click to edit Master title style

• Click to edit Master text styles– Second level• Third level

– Fourth level» Fifth level

20-12-2011 1

ANUG December 2011

Page 2: Nancy - A Lightweight .NET Web Framework

Who Am I?

Super Duper Happy Path

NancyModule

Nancy.Hosting

Nancy.ViewEngines

Nancy.Bootstrapper

Nancy.Authentication

Nancy.Testing

Why use Nancy?

Agenda

Page 3: Nancy - A Lightweight .NET Web Framework

3

Super Duper Happy Path

Page 4: Nancy - A Lightweight .NET Web Framework

4

LightweightWrite less

Sensible defaults

No non sense

DynamicBest effort – very best

Readable!!!!

Testable!!!!

Super Duper Happy Path

Page 5: Nancy - A Lightweight .NET Web Framework

5

Domain Model

Web

Data access

Accounting

Inventory

Super Duper Happy Path

Page 6: Nancy - A Lightweight .NET Web Framework

6

Fully composable

Dependency Injection

Easy to isolate

Super Duper Happy Path

Page 7: Nancy - A Lightweight .NET Web Framework

7

Runs anywhereWindows

» IIS» Windows Service» .exe

Linux + Mono

» ASP.NET» Kayak» executables

Azure

AppHarbor

Super Duper Happy Path

Page 8: Nancy - A Lightweight .NET Web Framework

Click to edit Master title style

• Click to edit Master text styles– Second level• Third level

– Fourth level» Fifth level

20-12-2011 8

Show me the code!

Page 9: Nancy - A Lightweight .NET Web Framework

Click to edit Master title style

• Click to edit Master text styles– Second level• Third level

– Fourth level» Fifth level

20-12-2011 9

Hello World

Page 10: Nancy - A Lightweight .NET Web Framework

Where your Nancy experience starts

Organizes your routes

NancyModule

public class MainModule : NancyModule { public MainModule() { Get["/"] = _ => "Hello from root"; } } public class SubModule : NancyModule { public SubModule() : base("subpath") { Get["/"] = _ => "Hello from subpath"; } }

Page 11: Nancy - A Lightweight .NET Web Framework

Defines which verbs you accepts

NancyModule

public class MainModule : NancyModule { public MainModule() { Get["/"] = _ => "Hello from root"; Post["/”] = _ => DoPost(Request.Form.my_value) Delete["/{id}”] = p => Delete(p.id); Put["/”] = _ => … Patch["/”] = _ => DoPut(Request.my_data) } }

Page 12: Nancy - A Lightweight .NET Web Framework

Allows to setup before and after

NancyModule

public class MainModule : NancyModule { public MainModule() { Before += context => ... ; After += context => ... ; } }

Page 13: Nancy - A Lightweight .NET Web Framework

Nancy.Hosting

Nancy

Your Application

Nancy.Hosting

Page 14: Nancy - A Lightweight .NET Web Framework

Usage:> Install-Package Nancy.Hosting.*

Hosts:ASP.NET

WCF

Self

OWIN

Nancy.Hosting

Page 15: Nancy - A Lightweight .NET Web Framework

Install: > Install-Package Nancy.Viewengines.*

View engines: Razor

Spark

NDjango

DotLiquid

Out-of-the-box: Super Simple View Engine

Nancy.Viewengines

Page 16: Nancy - A Lightweight .NET Web Framework

16

public class MainModule : NancyModule { public MainModule() { Get["/"] = _ => View[”home.cshtml”]; Get["/sayhello"] = _ => View[”say”, ”hello”]; } }

Nancy.Viewengines

Page 17: Nancy - A Lightweight .NET Web Framework

17

INancyBootstrapper

2 methods

DefaultNancyBootstrapper

18 overrideables

Container configuration

Pipeline setup

Override Nancy defaults

NuGets for 5 IoC/DI containers

Nancy.Bootstrapper

Page 18: Nancy - A Lightweight .NET Web Framework

18

public class Bootstrapper : DefaultNancyBootstrapper{ protected override void ApplicationStartup(TinyIoC.TinyIoCContainer container, IPipelines pipelines) { base.ApplicationStartup(container, pipelines); pipelines.BeforeRequest += context => context .Request.Headers .AcceptLanguage .Contains("Danish") ? null : "Only Danish";

pipelines.AfterRequest += context => AddToCache(context.Request.Path, context.Response.Contents); }}

Nancy.Bootstrapper

Page 19: Nancy - A Lightweight .NET Web Framework

19

Usage:> Install-Package Nancy.Authentication.*

Authentication types:Basic

Forms

Nancy.Authentication

Page 20: Nancy - A Lightweight .NET Web Framework

20

public class SecureModule : NancyModule{ public SecureModule() : base("/secure”)    {     this.RequiresAuthentication();     Get["/"] = x => View["secure.cshtml”];    } }

Nancy.Authentication

Page 21: Nancy - A Lightweight .NET Web Framework

21

public class FormsAuthBootstrapper : DefaultNancyBootstrapper{ protected override void RequestStartup( TinyIoCContainer requestContainer, IPipelines pipelines, NancyContext context)    {     var formsAuthConf =          new FormsAuthenticationConfiguration()              {               RedirectUrl = "~/login”,                  UserMapper = requestContainer.Resolve<IUserMapper>()              };

       FormsAuthentication.Enable(pipelines, formsAuthConf);    }}

Nancy.Authentication

Page 22: Nancy - A Lightweight .NET Web Framework

22

Will be demoed later …

Nancy.Testing

Page 23: Nancy - A Lightweight .NET Web Framework

23

Super Duper Happy Path

NancyModule

Nancy.Hosting

Nancy.ViewEngines

Nancy.Bootstrapper

Nancy.Authentication

Nancy.Testing

…And we’re only at v 0.9

Nancy

Page 24: Nancy - A Lightweight .NET Web Framework

24

Close to http

Very, very readable code

Very explicit routing

Embraces modularity

Embraces IoC/DI

Embraces testing

Embraces JSON

Runs anywhere

Why Nancy

Page 25: Nancy - A Lightweight .NET Web Framework

25

www.nancyfx.org

https://groups.google.com/forum/#!forum/nancy-web-framework

www.twitter.com/nancyfx

http://horsdal.blogspot.com/search/label/NancyFx

Resources

Page 26: Nancy - A Lightweight .NET Web Framework

Click to edit Master title style

• Click to edit Master text styles– Second level• Third level

– Fourth level» Fifth level

20-12-2011 27

Mjølner Informatics A/SFinlandsgade 10DK- 8200 Aarhus N

Tlf: +45 70 27 43 43CVR: 1257 8970

[email protected]

Christian HorsdalTwitter: @chr_horsdalBlog: horsdal.blogspot.comemail: [email protected]