Last active
December 26, 2015 14:49
-
-
Save dtchepak/7168143 to your computer and use it in GitHub Desktop.
Trying to port Liam's simplified controller example to F#: http://withouttheloop.com/articles/2013-10-20-are-we-doing-mvc-wrong/
Untested.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module BorrowABook = | |
type BookLoanProblem = | |
NotInCatalogue | |
| OutOnLoan | |
| InsufficientPermission | |
| Failed of string | |
type Book = Book | |
type BookModel = BookModel | |
let borrow (b : BookModel) : Choice<Book, BookLoanProblem> = Choice1Of2 Book |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Web | |
open System.Web.Mvc | |
open FSharpx | |
open BorrowABook | |
[<HandleError>] | |
type LibraryController() = | |
inherit Controller() | |
member this.viewForProb (prob : BookLoanProblem) = | |
match prob with | |
| NotInCatalogue -> setFlash "Not in catalogue"; this.redirect "Catalogue" | |
| OutOnLoan -> setFlash "Out on loan"; this.redirect "Catalogue" | |
| InsufficientPermission -> setFlash "Don't have permission"; this.redirect "Catalogue" | |
| Failed s -> setFlash s; this.viewByName("BorrowFailed") | |
member this.Borrow (model : BookModel) : ActionResult = | |
borrow model | |
|> Choice.choice (this.view "Borrowed") this.viewForProb | |
let setFlash msg = () | |
// Helpers for partial app etc. Put in f#-specific controller base class? | |
member this.view (s : string) (m:obj) : ActionResult = upcast this.View(s, m) | |
member this.viewByName (s : string) : ActionResult = upcast this.View(s) | |
member this.redirect s : ActionResult = upcast this.RedirectToAction(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment