Created
March 19, 2013 15:09
-
-
Save ashic/5196918 to your computer and use it in GitHub Desktop.
Simple command wrappers in F#. Would love ideas about the comment.
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
// Learn more about F# at http://fsharp.net | |
// See the 'F# Tutorial' project for more help. | |
type DoSomething = { Id:string ; Name : string } | |
type DoSomethingElse = { Id:string ; Name : string } | |
type UnhandledSomething = { Id:string ; Name : string } | |
type Handlers () = | |
static member Handle (cmd:DoSomething) = | |
printfn "handling something %s" cmd.Name | |
static member Handle (cmd:DoSomethingElse) = | |
printfn "handling something else %s" cmd.Name | |
type filters = | |
static member WrapWithLogging f = | |
printfn "log entry" | |
f | |
static member WrapWithTransaction f = | |
printfn "begin transaction" | |
fun x -> | |
f x | |
printfn "end transaction" | |
type Bus () = | |
member this.Handle (c:DoSomething) = | |
c | |
|> filters.WrapWithLogging Handlers.Handle | |
member this.Handle (c:DoSomethingElse) = | |
fun _ -> Handlers.Handle c // <- don't know enough F# but there should be a way to specify type and not have cludge | |
// Although I don't know why F# can't infer it as func takes in c below | |
// Would love to do Handlers.Handle |> filters.WrapX |> filters.WrapY |> fun func -> func c | |
|> filters.WrapWithLogging | |
|> filters.WrapWithTransaction | |
|> fun func -> func c | |
member this.Handle2 (c:DoSomethingElse) = | |
filters.WrapWithLogging | |
filters.WrapWithTransaction | |
(fun x -> Handlers.Handle c) | |
c | |
member this.Handle _ = | |
printfn "unhandled" | |
[<EntryPoint>] | |
let main argv = | |
let bus = new Bus(); | |
bus.Handle { DoSomethingElse.Id="123"; Name = "John" } | |
bus.Handle { UnhandledSomething.Id="123"; Name = "John" } | |
bus.Handle2 { DoSomethingElse.Id="123"; Name = "John" } | |
0 // return an integer exit code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment