Nancy - A Lightweight .NET Web Framework

Post on 24-May-2015

3,683 views 2 download

Tags:

transcript

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

Who Am I?

Super Duper Happy Path

NancyModule

Nancy.Hosting

Nancy.ViewEngines

Nancy.Bootstrapper

Nancy.Authentication

Nancy.Testing

Why use Nancy?

Agenda

3

Super Duper Happy Path

4

LightweightWrite less

Sensible defaults

No non sense

DynamicBest effort – very best

Readable!!!!

Testable!!!!

Super Duper Happy Path

5

Domain Model

Web

Data access

Accounting

Inventory

Super Duper Happy Path

6

Fully composable

Dependency Injection

Easy to isolate

Super Duper Happy Path

7

Runs anywhereWindows

» IIS» Windows Service» .exe

Linux + Mono

» ASP.NET» Kayak» executables

Azure

AppHarbor

Super Duper Happy Path

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!

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

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"; } }

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

Allows to setup before and after

NancyModule

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

Nancy.Hosting

Nancy

Your Application

Nancy.Hosting

Usage:> Install-Package Nancy.Hosting.*

Hosts:ASP.NET

WCF

Self

OWIN

Nancy.Hosting

Install: > Install-Package Nancy.Viewengines.*

View engines: Razor

Spark

NDjango

DotLiquid

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

Nancy.Viewengines

16

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

Nancy.Viewengines

17

INancyBootstrapper

2 methods

DefaultNancyBootstrapper

18 overrideables

Container configuration

Pipeline setup

Override Nancy defaults

NuGets for 5 IoC/DI containers

Nancy.Bootstrapper

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

19

Usage:> Install-Package Nancy.Authentication.*

Authentication types:Basic

Forms

Nancy.Authentication

20

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

Nancy.Authentication

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

22

Will be demoed later …

Nancy.Testing

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

24

Close to http

Very, very readable code

Very explicit routing

Embraces modularity

Embraces IoC/DI

Embraces testing

Embraces JSON

Runs anywhere

Why Nancy

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

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

info@mjolner.dkwww.mjolner.dk

Christian HorsdalTwitter: @chr_horsdalBlog: horsdal.blogspot.comemail: chg@mjolner.dk