using Microsoft.Extensions.Configuration; using Sarand.Net.Standard.Base.Core; using Saxonite.Net.Standard.Base.App; using Saxonite.Net.Core.Web.Base.App; using System; using System.Collections.Generic; using System.Text; using Sarand.Net.Standard.Base.Utils; namespace Saxonite.Net.Core.Web.Base.Helpers { //======================================================================= public static class SaxAppConfigHelper { //------------------------------------------------------------------- public static Dictionary ExtractAppConfigs(IConfigurationSection appConfigsSection) { // NB The set of configurations will only allow one per context. // Only the last configuration found for each context will be stored (NB usually only one of each apart from 'Defaults'). Dictionary appConfigs = new Dictionary(); // Start the list with a default 'Defaults' configuration - values set to application defaults (defined in Sax). // NB This can be overridden in each level of configuration. // Settings from the 'Defaults' configuration provide values where they are missing from other configurations. // NB Coverage may not be 100% - just where it's proven needed. SaxAppConfig defaultsAppConfig = new SaxAppConfig(Sax.Config.Context_Defaults, null, null); appConfigs.Add(defaultsAppConfig.Context, defaultsAppConfig); // Load session configurations. foreach (ConfigurationSection appConfigSection in appConfigsSection.GetChildren()) { if (StringUtil.IsEmpty(appConfigSection.Key)) continue; // Extract the configuration, supplying current 'Defaults' to supply missing values, and add to the list. SaxAppConfig appConfig = new SaxAppConfig(appConfigSection.Key, appConfigSection, defaultsAppConfig); appConfigs.Remove(appConfigSection.Key); appConfigs.Add(appConfigSection.Key, appConfig); // Replace the 'Defaults' configuration if this one matches the context. if (appConfig.Context.EqualsCI(Sax.Config.Context_Defaults)) defaultsAppConfig = appConfig; } return appConfigs; } //------------------------------------------------------------------- } //======================================================================= public static class AppConfigList { public static SaxAppConfig getConfigForApplication(this List appConfigs) { SaxAppConfig defaultsAppConfig = new SaxAppConfig("", null, null); SaxApplication_Base app = SaxGlobals.App; if (app != null) { foreach (SaxAppConfig appConfig in appConfigs) { if (appConfig.Environments.ContainsCI(app.EnvironmentName)) return appConfig; if (appConfig.Context.EqualsCI(Sax.Config.Context_Defaults)) defaultsAppConfig = appConfig; } } return defaultsAppConfig; } } //======================================================================= }