Skip to content

Instantly share code, notes, and snippets.

@grishace
Last active March 5, 2019 20:44
Show Gist options
  • Save grishace/6c8422059aa243690cf66b0bcd53470f to your computer and use it in GitHub Desktop.
Save grishace/6c8422059aa243690cf66b0bcd53470f to your computer and use it in GitHub Desktop.
Reading linq2db configuration from json
module Configuration
open System
open System.IO
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.Configuration.Binder
open Microsoft.Extensions.Configuration.Json
open LinqToDB.Configuration
let [<Literal>] private SqlServer = "SqlServer"
[<CLIMutable>]
type DBConfiguration =
{ ConnectionString: string
Name: string
ProviderName: string
}
static member Default =
{ ConnectionString = String.Empty
Name = String.Empty
ProviderName = String.Empty
}
member this.AsSettings () =
{ new IConnectionStringSettings with
member __.ConnectionString with get() = this.ConnectionString
member __.Name with get() = this.Name
member __.ProviderName with get() = this.ProviderName
member __.IsGlobal with get() = false
}
type DBSettings (file: string, [<ParamArray>] sections: string array) =
let configurations =
let cfgBuilder =
ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(file)
.Build()
sections
|> Array.map (fun s ->
let cfg = DBConfiguration.Default
cfgBuilder.GetSection(s).Bind(cfg)
cfg.AsSettings()
)
|> Seq.ofArray
|> Seq.cache
interface ILinqToDBSettings with
member val DataProviders = Seq.empty
member val DefaultDataProvider = SqlServer
member val DefaultConfiguration = SqlServer
member val ConnectionStrings = configurations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment