using System.Collections.Generic; using System.Configuration; using System.Linq; using Microsoft.Extensions.Configuration; using Sony.Filtr.Database; namespace Sony.Filtr.Bootstrap { public class BootstrapperSettings { private IConfigurationRoot _config; public BootstrapperSettings(IConfigurationRoot config) { _config = config; } public Dictionary GetSettings() { var dbSettings = GetDatabaseSettings(); var configOverrides = _config.GetSection("appSettings")?.GetChildren()?.ToDictionary(x => x.Key, x => x.Value); Dictionary joinedSettings = new Dictionary(); if (configOverrides != null) { joinedSettings = configOverrides.Keys.Cast().ToDictionary(configOverrideKey => configOverrideKey, configOverrideKey => configOverrides[configOverrideKey]); } foreach (var dbSetting in dbSettings) { if (!joinedSettings.ContainsKey(dbSetting.Key)) joinedSettings.Add(dbSetting.Key, dbSetting.Value); } return joinedSettings; } private Dictionary GetDatabaseSettings() { Dictionary settings = new Dictionary(); using (var connection = DatabaseHandler.GetOpenReadOnlyConnection()) { var cmd = connection.CreateCommand(); cmd.CommandText = "SELECT strKey, strValue FROM tblBootstrap"; var reader = cmd.ExecuteReader(); while (reader.Read()) { settings.Add(reader.GetString(0), reader.GetString(1)); } } return settings; } } }