summaryrefslogtreecommitdiff
path: root/examples/DependencyInjection/Program.fs
blob: 84b51c6548d5936137270e98c2f4b7180e0d39dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
open System

type Logger = {
    Log : string -> unit
}

type HelloService = {
    SayHelloTo : string -> unit
}

// This is the constructor of a logger that adds a timestamp in the specified
// format to each log message it writes.
let dateTimeLogger (dateFormat : string) =
    { Log =
        fun msg ->
            let timestamp = DateTime.Now.ToString dateFormat
            printfn "[%s]: %s" timestamp msg }

let logger = dateTimeLogger "yyyy-MM-dd hh:mm:ss"

// This implementation of Logger ignores all messages
let noopLogger =
    { Log = ignore }

let doSomething logger =
    logger.Log "Starting to do something"
    let x = 1 + 1
    sprintf "Did something! Result: %d" x
    |> logger.Log

// Implementations can be injected to application code
doSomething logger
doSomething noopLogger

// Implement HelloService. Our implementation depends on a Logger to write
// messages to.
let helloService logger =
    { SayHelloTo =
        fun user ->
            sprintf "Saying hi to %s" user
            |> logger.Log
            printfn "Hi %s!" user
            logger.Log "Operation complete" }

// Create an instance of helloService.
//
// We don't want to do anything with the messages so we discard them by using
// injecting noopLogger as the implementation of Logger.
let instance = helloService noopLogger

instance.SayHelloTo "John"