Files
2024-11-19 09:47:00 +00:00

71 lines
3.0 KiB
C#

using Microsoft.Extensions.Configuration;
using Sarand.BusinessMgr.Net.Core.Web.App;
using Sarand.BusinessMgr.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Text;
namespace Sarand.BusinessMgr.Net.Core.Web.Helpers
{
//=======================================================================
public static class SbmAppConfigHelper
{
//-------------------------------------------------------------------
public static Dictionary<String, App.SbmAppConfig> 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<string, App.SbmAppConfig> appConfigs = new Dictionary<string, App.SbmAppConfig>();
// Start the list with a default 'Defaults' configuration - values set to application defaults (defined in Sbm).
// 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.
App.SbmAppConfig defaultsAppConfig = new App.SbmAppConfig(Sbm.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.
App.SbmAppConfig appConfig = new App.SbmAppConfig(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(Sbm.Config.Context_Defaults))
defaultsAppConfig = appConfig;
}
return appConfigs;
}
//-------------------------------------------------------------------
}
//=======================================================================
public static class AppConfigList
{
public static App.SbmAppConfig getConfigForApplication(this List<App.SbmAppConfig> appConfigs)
{
App.SbmAppConfig defaultsAppConfig = new App.SbmAppConfig("", null, null);
SbmApplication app = SbmGlobals_Web.Application;
if (app != null)
{
foreach (App.SbmAppConfig appConfig in appConfigs)
{
if (appConfig.Environments.ContainsCI(app.EnvironmentName))
return appConfig;
if (appConfig.Context.EqualsCI(Sbm.Config.Context_Defaults))
defaultsAppConfig = appConfig;
}
}
return defaultsAppConfig;
}
}
//=======================================================================
}