Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Four Other .NET Web Frameworks in Less Than an Hour                                                 Christian Horsdal     ...
Who Am I?     Lead Software Architect @ Mjølner Informatics     Husband and Father     Some who enjoys       Clean cod...
Why?     There are alternatives to ASP.NET       Know them!     Style matters     Tradeoffs, tradeoffs, tradeoffs     ...
Why?     Run anywhere     IoC/DI to the bone     Embrace HTTP     OSS and community driven5
What will you learn?            A taste of some alternatives                                  FubuMVC                    ...
Sample7
FubuMVC – At a glance     One Model In One Model Out       Aka OMIOMO       Aka Russian Doll     Convention over Confi...
FubuMVC – ShortUrl Overview    GET “/”                    HomeController.get_Home             HomeView.cshtml             ...
FubuMVC – ShortUrl – Serving the FORMpublic class HomeController{   public HomeController(UrlStore urlStore)   {     this....
FubuMVC – ShortUrl – POST the FORM         public UrlShorteningViewModel post_home(UrlShorteningModel input)         {    ...
FubuMVC – ShortUrl - Redirecting            public FubuContinuation get_Url(ShortenedUrlModel input)            {         ...
OpenRasta– At a glance      Three things:        Resources        Handlers        Codecs      Automatic conneg      ...
OpenRasta – Shorturl Overview      Resource:         “/”         “/{shortenedUrl}”         Home                       ...
OpenRasta– ShortUrl – Serving the FORM public class HomeHandler {   public HomeHandler(IRequest request, UrlStore urlStore...
OpenRasta– ShortUrl – POST the FORM           public Home Post(Home input)           {             input.ShortUrl = input....
OpenRasta– ShortUrl - Redirecting           public OperationResult Get(string shortenedUrl)           {             var lo...
Nancy– At a glance      Lightweight, low ceremony         Just works         But easily swappable      DSLs      Buil...
Nancy – Shorturl Overview      Modules         ShortUrlModule      Routes         Get[“/”]         Post[“/”]        ...
Nancy– ShortUrl – Serving the FORM     public class ShortUrlModule : NancyModule     {       public ShortUrlModule(UrlStor...
Nancy– ShortUrl – POST the FORMpublic class ShortUrlModule : NancyModule{   public ShortUrlModule(UrlStore urlStore)   {  ...
Nancy– ShortUrl - Redirecting     public class ShortUrlModule : NancyModule     {       public ShortUrlModule(UrlStore url...
Frank– At a glance      F#      Bare bones approach to HTTP services      Leverage WebAPI      Compositional23
Frank– ShortUrl – Serving the FORM     let urlShortenerView = @"<!doctype html><meta charset=utf-8>       <title>Hello</ti...
Frank– ShortUrl – POST the FORM     let createShortUrl (request: HttpRequestMessage) = async {       let! value = request....
Frank– ShortUrl - Redirecting   let getShortUrl (request: HttpRequestMessage) = async {     let urlKey = request          ...
Why, again?      There are alternatives to ASP.NET        Know them!      Style matters      Tradeoffs, tradeoffs, tra...
What might you have learned?            A taste of some altenatives                          FubuMVC                     ...
FubuMVC, OpenRasta, Nancy, FrankWHEN, WHAT, WHERE?        29
Evaluation page: http://bit.ly/cd2012b2Please fill in!                 „kthxbai
Upcoming SlideShare
Loading in …5
×

Four .NET Web Frameworks in Less Than an Hour

3,379 views

Published on

In the .NET space the overwhelming majority of web projects are built with ASP.NET in one form or another.

But there are alternatives. Alternatives that offer other approaches, and supports other ways of thinking.Alternatives that are open source. Altenratives that run on Mono.

Maybe one of those alternatives is the better fit for your next project - except if you don't know about them you will never know. In this code heavy talk I'll show the same sample application in the four Open Source .NET web frameworks FubuMVC, OpenRasta, Nancy and Frank.

You will not become an expert at anything by attending this talk - but you may discover just the right web framework for your next project.

Published in: Technology, Design
  • Login to see the comments

Four .NET Web Frameworks in Less Than an Hour

  1. 1. Four Other .NET Web Frameworks in Less Than an Hour Christian Horsdal @chr_horsdal http://horsdal.blogspot.com/
  2. 2. Who Am I?  Lead Software Architect @ Mjølner Informatics  Husband and Father  Some who enjoys  Clean code  TDD‟ing  When ‟GF wins  Simplicity  Whisky3
  3. 3. Why?  There are alternatives to ASP.NET  Know them!  Style matters  Tradeoffs, tradeoffs, tradeoffs  Conventions <-> explicitness  DRY <-> separation of concerns  Abstract <-> concrete4
  4. 4. Why?  Run anywhere  IoC/DI to the bone  Embrace HTTP  OSS and community driven5
  5. 5. What will you learn? A taste of some alternatives  FubuMVC  OpenRasta  Nancy  Frank6
  6. 6. Sample7
  7. 7. FubuMVC – At a glance  One Model In One Model Out  Aka OMIOMO  Aka Russian Doll  Convention over Configuration  Really cool built-in diagnostics  Everything is POCOs8
  8. 8. FubuMVC – ShortUrl Overview GET “/” HomeController.get_Home HomeView.cshtml • Returns • Takes • No params HomeViewModel HomeViewModel POST “/” HomeController.post_Home UrlShortenedView.cshtml • UrlShorteningModel • Takes • Takes UrlShorteningModel UrlShorteningViewModel • Returns UrlShorteningViewModel GET “/42” HomeController.post_Home • Takes ShortenedUrlMode • ShortenedUrlModel • Returns FubuContinuation9
  9. 9. FubuMVC – ShortUrl – Serving the FORMpublic class HomeController{ public HomeController(UrlStore urlStore) { this.urlStore = urlStore; } @model ShortUrlInFubuMvc.HomeViewModel public HomeViewModel get_home() @{ { Layout = null; return new HomeViewModel(); } } <!DOCTYPE html> … <html>} <body> <form method="post" action="home"> public class HomeViewModel { } <label>Url: </label> <input type="text" name="url"/> <input type="submit" value="shorten"/> </form>10 </body>
  10. 10. FubuMVC – ShortUrl – POST the FORM public UrlShorteningViewModel post_home(UrlShorteningModel input) { var shortenedUrl = input.Url.GetHashCode().ToString(); urlStore.SaveUrl(input.Url, shortenedUrl); return new UrlShorteningViewModel { Host = "localhost:51862", HashCode = shortenedUrl }; } public class UrlShorteningModel { public string Url { get; set; } } public class UrlShorteningViewModel { public string Host { get; set; }11 public string HashCode { get; set; }
  11. 11. FubuMVC – ShortUrl - Redirecting public FubuContinuation get_Url(ShortenedUrlModel input) { var longUrl = urlStore.GetUrlFor(input.Url) ?? "/notfound/"; return FubuContinuation.RedirectTo(longUrl); } public class ShortenedUrlModel { public string Url { get; set; } }12
  12. 12. OpenRasta– At a glance  Three things:  Resources  Handlers  Codecs  Automatic conneg  Everything is POCOs13
  13. 13. OpenRasta – Shorturl Overview  Resource:  “/”  “/{shortenedUrl}”  Home GET “/” POST “/” GET “/42”  Handler  HomeHandler  “Codecs” Home resource  WebForms viewengine HomeHandler  Form data14
  14. 14. OpenRasta– ShortUrl – Serving the FORM public class HomeHandler { public HomeHandler(IRequest request, UrlStore urlStore) { public class Configuration : IConfigurationSource this.request = request; { this.urlStore = urlStore; public void Configure() } { using (OpenRastaConfiguration.Manual) public Home Get() { { ResourceSpace.Has return new Home(); .ResourcesOfType<Home>() } .AtUri("/").And.AtUri("/home") … .And.AtUri("/{shortenedUrl}")} .HandledBy<HomeHandler>() .RenderedByAspx("~/Views/Home.aspx");public class Home{ public string LongUrl { get; set; } ResourceSpace.Uses public string ShortUrl { get; set; } .Resolver.AddDependencyInstance<UrlStore> public string Host { get; set; } }}15 }
  15. 15. OpenRasta– ShortUrl – POST the FORM public Home Post(Home input) { input.ShortUrl = input.LongUrl.GetHashCode().ToString(); input.Host = request.Uri.Host + ":" + request.Uri.Port; urlStore.SaveUrl(input.LongUrl, input.ShortUrl); return input; }16
  16. 16. OpenRasta– ShortUrl - Redirecting public OperationResult Get(string shortenedUrl) { var longUrl = urlStore.GetUrlFor(shortenedUrl); if (longUrl != null) return new OperationResult.SeeOther { RedirectLocation = new Uri(longUrl) }; else return new OperationResult.NotFound(); }17
  17. 17. Nancy– At a glance  Lightweight, low ceremony  Just works  But easily swappable  DSLs  Built in diagnostics  Testability is first class18
  18. 18. Nancy – Shorturl Overview  Modules  ShortUrlModule  Routes  Get[“/”]  Post[“/”]  Get[“/{shortenedUrl}”] HTTP Routes Handler Response request function  A lambda for each19
  19. 19. Nancy– ShortUrl – Serving the FORM public class ShortUrlModule : NancyModule { public ShortUrlModule(UrlStore urlStore) { Get["/"] = _ => View["index.html"]; … } … } <!DOCTYPE html> <html> <body> <form method="post" action="home"> <label>Url: </label> <input type="text" name="url"/> <input type="submit" value="shorten"/> </form> </body> </html>20
  20. 20. Nancy– ShortUrl – POST the FORMpublic class ShortUrlModule : NancyModule{ public ShortUrlModule(UrlStore urlStore) { Get["/"] = _ => View["index.html"]; Post["/"] = _ => ShortenUrl(urlStore); … } private Response ShortenUrl(UrlStore urlStore) { string longUrl = Request.Form.url; var shortUrl = longUrl.GetHashCode().ToString(); urlStore.SaveUrl(longUrl, shortUrl); return View["shortened_url", new { Request.Headers.Host, ShortUrl = shortUrl }]; } …}21
  21. 21. Nancy– ShortUrl - Redirecting public class ShortUrlModule : NancyModule { public ShortUrlModule(UrlStore urlStore) { Get["/"] = _ => View["index.html"]; Post["/"] = _ => ShortenUrl(urlStore); Get["/{shorturl}"] = param => { string shortUrl = param.shorturl; return Response.AsRedirect(urlStore.GetUrlFor(shortUrl.ToString())); }; } … }22
  22. 22. Frank– At a glance  F#  Bare bones approach to HTTP services  Leverage WebAPI  Compositional23
  23. 23. Frank– ShortUrl – Serving the FORM let urlShortenerView = @"<!doctype html><meta charset=utf-8> <title>Hello</title> <p>Hello, world! <form action=""/"" method=""post"" enctype=""multipart/form-data""> <input type=""input"" name=""longUrl"" value=""long url""> <input type=""submit"">" let urlShortenerForm request = async { return respond HttpStatusCode.OK (new StringContent(urlShortenerView, System.Text.Encoding.UTF8, "text/html")) <| ``Content-Type`` "text/html" }24
  24. 24. Frank– ShortUrl – POST the FORM let createShortUrl (request: HttpRequestMessage) = async { let! value = request.Content.AsyncReadAsMultipart() let longUrl = value.FirstDispositionNameOrDefault("longUrl") .ReadAsStringAsync().Result; let urlKey = longUrl.GetHashCode().ToString() urlStore.Add(urlKey, longUrl) let shortUrl = baseUri + "/" + longUrl.GetHashCode().ToString() return respond HttpStatusCode.OK (new StringContent(shortUrlCreatedView(shortUrl), System.Text.Encoding.UTF8, "text/html")) <| ``Content-Type`` "text/html" }25
  25. 25. Frank– ShortUrl - Redirecting let getShortUrl (request: HttpRequestMessage) = async { let urlKey = request .RequestUri .Segments.[request.RequestUri.Segments.Length - 1] let longUrl = urlStore.[urlKey] return respond HttpStatusCode.Redirect (new EmptyContent()) <| Location (new Uri(longUrl)) }let rootResource = routeResource "/" [ get urlShortenerForm <|> post createShortUrl ]let redirectReource = routeResource "/{shortUrl}" [ get getShortUrl ]let app = merge [ rootResource; redirectReource ]let config = new HttpSelfHostConfiguration(baseUri)config.Register app26
  26. 26. Why, again?  There are alternatives to ASP.NET  Know them!  Style matters  Tradeoffs, tradeoffs, tradeoffs27
  27. 27. What might you have learned? A taste of some altenatives  FubuMVC  OMIOMU  OpenRasta  Resources, Handlers, Codecs  Nancy  DSL  Frank  F#28
  28. 28. FubuMVC, OpenRasta, Nancy, FrankWHEN, WHAT, WHERE? 29
  29. 29. Evaluation page: http://bit.ly/cd2012b2Please fill in! „kthxbai

×