Skip to content

Instantly share code, notes, and snippets.

@akhansari
Last active February 22, 2023 16:51
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save akhansari/d4c5a219d22199f151016639e5aa8d8a to your computer and use it in GitHub Desktop.
Save akhansari/d4c5a219d22199f151016639e5aa8d8a to your computer and use it in GitHub Desktop.
F# : Onion architecture in a nutshell
// 1. pure, don't think about IO at all
module Domain =
let add x y = x + y
// 2. think about IO but not its implementation
module App =
let add (getX: unit -> Async<int32>) y =
async {
let! x = getX ()
return Domain.add x y
}
// 3. IO implementation
module Infra =
let getX () = async { return 7 }
// 4. inject dependencies
module Startup =
let add = App.add Infra.getX
// demo
Startup.add 3
|> Async.RunSynchronously
|> printfn "%A" // 10
// 1. pure, don't think about IO at all
module Domain =
let add x y = x + y
// 2. think about IO but not its implementation
module App =
let add (getX: unit -> Async<int32>) y =
async {
let! x = getX ()
return Domain.add x y
}
// 3. IO implementation
module Infra =
open System.Data.SqlClient
let newConnection () = new SqlConnection ()
let getX conn = async { return 7 }
// 4. inject dependencies
module Startup =
let add y =
async {
use conn = Infra.newConnection ()
return! App.add (fun () -> Infra.getX conn) y
}
// demo
Startup.add 3
|> Async.RunSynchronously
|> printfn "%A" // 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment