Created
April 4, 2016 23:26
-
-
Save dmitry-a-morozov/346c1eecfa37a87c88e03872023a5a41 to your computer and use it in GitHub Desktop.
thread safe singleton defined as F# record
This file contains 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
type Configuration = { | |
Database: string | |
RetryCount: int | |
} | |
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] | |
[<AutoOpen>] | |
module Configuration = | |
let private singleton = ref { Database = "(local)"; RetryCount = 3 } | |
let private guard = obj() | |
type Configuration with | |
static member Current | |
with get() = lock guard <| fun() -> !singleton | |
and set value = lock guard <| fun() -> singleton := value | |
printfn "Default start-up config: %A" Configuration.Current | |
Configuration.Current <- { Configuration.Current with Database = ".\SQLExpress" } | |
printfn "Updated config: %A" Configuration.Current |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment