Last active
March 5, 2019 20:44
-
-
Save grishace/6c8422059aa243690cf66b0bcd53470f to your computer and use it in GitHub Desktop.
Reading linq2db configuration from json
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
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