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.

The Functional Web

1,437 views

Published on

The HTTP protocol adheres to the functional programming paradigm. This talk looks at HTTP on .NET and illustrates how F# allows for a more direct correlation to the patterns of composition inherent in the design of HTTP.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

The Functional Web

  1. 1. The Functional Web
  2. 2. Ryan RileyO Logos Bible SoftwareO Community for F#O F# MVPO OSS: FSharpx, Fracture, FrankO @panesofglass
  3. 3. OutlineO The State of HTTP on .NETO Composition Patterns in HTTPO Identifying ProblemsO Examining a Solution
  4. 4. The State of HTTP on .NET
  5. 5. Current OptionsO http.sys O HttpListener O IIS (IHttpModule & IHttpHandler) O Web Forms O MVC O WCF HTTP & Data Services
  6. 6. Future OptionsO System.Net.Http (BETA) O .NET 4.5 O Available on NuGetO ASP.NET Web API (BETA) O Builds on System.Net.Http O Integrated with MVC 4
  7. 7. OSS Options Frameworks ServersO FubuMVC O Firefly (OWIN)O MonoRail O Fracture (F#)O NancyFx O Kayak (OWIN)O OpenRasta O Manos de MonoO OWIN/GateO ServiceStack.NET O Mono XSPO WebSharperO … and many more!
  8. 8. Composition Patterns in HTTP
  9. 9. Component PartsO Request/Response Lines O Methods, URIs, Status CodesO Headers O General, Request, Response, ContentO ResourcesO RepresentationsO Hypermedia
  10. 10. Simplest HTTP Application HttpRequestMessage -> HttpResponseMessage
  11. 11. ResourcesGET /item/1+ POST /item/1+ PUT /item/1+ OPTIONS /item/1=Resource at / with GET, POST, PUT, &OPTIONS
  12. 12. HTTP Applications/+ /items+ /item/{itemId}+ /help=Typical HTTP “application”
  13. 13. Content NegotiationClient Accepts Server Supportsapplication/xml;q=0.9 application/jsonapplication/json;q=0.8 text/plaintext/plain;q=0.5 text/html application/json
  14. 14. Identifying Problems
  15. 15. HttpListenerO Low-levelO Defines the signature for a single application onlyO HttpListenerContext provides a singleton; no chance to intercept and chain behaviorO Routing requests requires custom handling; typically by using imperative if-then-elseO No means of chaining or composing multiple listeners
  16. 16. HttpListenerHttpListener.Start ("http://localhost:8082/", (fun (request, response) -> async { match request.Url.LocalPath with| "/post" ->// Send message to the chat room room.SendMessage(request.InputString) response.Reply("OK") | "/chat" -> // Get messages from the chat room (asynchronously!) let! text = room.AsyncGetContent() response.Reply(text) | s -> // Handle an ordinary file request let file = root + (if s = "/" then "chat.html" else s.ToLower()) if File.Exists(file) then let typ = contentTypes.[Path.GetExtension(file)] response.Reply(typ, File.ReadAllBytes(file)) else response.Reply(sprintf "File not found: %s" file) }),cts.Token)
  17. 17. IHttpModule & IHttpHandlerO Low-levelO HttpContext has the same problems as HttpListenerContextO IHttpHandler could represent a Resource, but method mapping is imperativeO IHttpModule provides a middleware layer for routing or hosting multiple IHttpHandlersO System.Web.Routing surfaces the routing component
  18. 18. IHttpModule & IHttpHandlerinterface IHttpModule{ void Dispose(); void Init(HttpApplication context);}interface IHttpHandler{ bool IsReusable { get; } void ProcessRequest(HttpContext context);}
  19. 19. ASP.NET Web FormsO HTML composition via MasterPages, Pages, and ControlsO Suffers many problems associated with IHttpModule and IHttpHandlerO Evented model allows for interceptionO No built-in support for other representationsO Pages loosely correlate to ResourcesO Application composition and routing by file path
  20. 20. ASP.NET MVCO Suffers many problems associated with IHttpModule and IHttpHandlerO Primarily HTML generation but allows for better HTTP controlO Built-in support for handling Ajax requestsO ActionFilters allow for behavior composition at different points: Authorize, ActionExecuting, ActionExecuted, Resul tExecuting, ResultExecuted
  21. 21. ASP.NET MVC, cont.O Content Negotiation is still very manualO ViewEngines and Serializers can be made to render various representationsO ViewEngines offer representation compositionO Controllers ~ Resources, but not quiteO Areas and Routing allow for Nested Resources, to a degree
  22. 22. Problem SummaryO Have: O Limited interception for cross-cutting concerns O Representation compositionO Do not have: O No easy resource composition O No built-in content negotiation
  23. 23. Composition for ConnegO Rendering O Imperative if-then-else O SerializersO Location O Within the specific method handler (common for imperative) O Top-level (common for serializers)O Why not both?
  24. 24. Nested ResourcesO Imperative if-then-elseO RoutingO MVC Areas offer another levelO What about more than one level? O Rails – problem with deep URIs O System.Web.Routing does everything at the top-level, so tracking deeper resource trees is difficult.
  25. 25. Examining a Solution
  26. 26. Function CompositionO Functional programming offers solutions to these problems O Filter O Map O Reduce
  27. 27. FrankO F# DSL using System.Net.HttpO Headers compositionO Follows the natural composition of HTTPO Frank Resources == HTTP ResourcesO Define your own conventions!
  28. 28. Simplest HTTP Frank Application HttpRequestMessage -> HttpResponseMessageHttpRequestMessage -> Async<HttpResponseMessage>
  29. 29. Define a Method Handler// handlerlet echo request = async { let! body = request.Content.AsyncReadAsString() return HttpResponseMessage.ReplyTo(request, body)}// method handlerget echo
  30. 30. Define a Resourcelet helloworld request = async { … }let echo request = async { … }let resource = route “/” (get helloworld <|> post echo)
  31. 31. Define an Applicationlet todoListResource = route “/” (get todoList <|> …)let todoItemResource = route “/item/{1}” (put …)let app = merge [ todoListResource; todoItemResource ]
  32. 32. Leverage Connegval negotiateMediaType = formatters -> HttpRequestMessage -> string -> Async<HttpResponseMessage>let echo = negotiateMediaType formatters <| fun request -> request.Content.AsyncReadAsString())
  33. 33. WebSharperO One language for both client and serverO Can leverage to cleanly separate partsO Use WebSharper to generate code-on- demand to send to the clientO Plays well with existing tools
  34. 34. Summary
  35. 35. ResourcesO ASP.NET Web Forms in F#O ASP.NET MVC in F#O Figment DSL for MVCO Frank DSL for Web APIO WebSharperO Pit FW
  36. 36. Thank you!O Please rate this talk: http://speakerrate.com/talks/9293-the- functional-web

×