Most .NET web applications today use ASP.NET WebForms or MVC. However, several F# libraries offer new ways to build web APIs and applications help reduce code and offer better abstractions, especially for single-page applications (SPA). How can F# improve the situation? Frank is a minimal, domain-specific library designed to leverage the power of HTTP using functions directly.
2. Ryan Riley
O Logos Bible Software
O Community for F#
O F# MVP
O OSS: FSharpx, Fracture, Frank
O @panesofglass
3. HTTP Parts
O Request/Response Lines
O Methods, URIs, Status Codes
O Headers
O General, Request, Response, Content
O Resources
O Representations
O Hypermedia
4. Frank
O F# DSL using System.Net.Http
O Headers composition
O Follows the natural composition of HTTP
O Frank Resources == HTTP Resources
O Define your own conventions!
16. Resources
O ASP.NET Web Forms in F#
O ASP.NET MVC in F#
O Figment DSL for MVC
O Frank DSL for Web API
O WebSharper
O Pit FW
17. Thank you!
O Please rate this talk:
http://speakerrate.com/talks/9334-frank-the-web-api-dsl
Notas do Editor
HTTP defines several components that allow distributed, loosely-coupled agents to communicate. The first lines of requests and responses are standardized to ensure that loosely-coupled components can understand one another, yet they allow for extension either by standards bodies or for internal, proprietary use.
For those with a stronger bent towards building web sites, Mauricio Scheffer’s Figment may be a closer match to your taste.
Routing is nothing more than filtering down a set of handlers to select the appropriate one and return a response.A Response is nothing more than a map function over a request, though it is true that some HTTP methods are destructive (e.g. POST).Pulling handlers into resources or applications is nothing more than a reduce function. Frank uses merge; WebSharper uses Sum.
Compare the Frank application signature with the simplest app signature we used above. The only addition is that we are stating that an application is non-blocking. This is the same idea as is used in node.js. It’s also sort-of required since Frank sits on System.Net.Http via MessageHandlers. The signature for the MessageHandler.SendAsync is the same but for Task rather than Async. This signature extends all the way through all layers of composition.
This may be new to some, but this resource is not the same as the collection of items at /items.
A resource is the only current location that does not conform solely to the Frank application signature. A resource holds both a Uri Template (its name) as well as a HttpRequestMessage -> Async<HttpResponseMessage> option. Why the option? The resource will respond with a 405 Method Not Allowed if it does not respond to the method thrown its way. Thus the option allows the programmer to define the methods for the resource without having to manually hook up the 405 response.
The aggregate of many resources is generally what we refer to as an HTTP, or Web, Application. A single resource is enough, as is a single method.
Similar to resource, merge is a function of HttpRequestMessage -> Async<HttpResponseMessage> option. When a resource uri is not found, a 404 Not Found response is returned. The result, however, is a Frank application.
Resources may be directly returned in a response. In the beginning, when most HTTP applications served static files, this was common. However, Resources are not equivalent to the representation returned to the client, even if they appear identical. Unlike editing files locally, if you were to edit a file returned from an HTTP application, you would not be editing the original file. You would need to re-submit the file to the server, if allowed, and then the server would need to take the content you supplied and update the original Resource.It is thus reasonable to understand that a Resource can be represented in more than one way. A JSON representation is just as valid as XML, HTML, or even an image.Content negotiation, or “conneg,” is a cross-cutting concern that allows HTTP applications to select an appropriate representation to send in the response message. The actual execution of this selection could occur either at the top-level or at the lowest-level of a composed HTTP application.
Frank currently uses Mauricio Scheffer’sFsConneg library, now in FSharpx.Http. System.Net.Http has a similar mechanism, but it’s currently still a little heavy as they work out the kinks in preparation for the RC. FsConneg provides a mechanism for submitting the Accept headers and a list of supported formats and returns the best match. negotiateMediaType wraps that with the selection of the appropriate formatter, of type System.Net.Http.Formatting.MediaTypeFormatter, and returns the formatted content.
Using F# for web projects isn’t just about code size. Yes, you’ll save some lines of code, but more importantly, you’ll work closer to the guts of HTTP. You can leverage the power of composition to build applications that will be more maintainable and much clearer to understand.