using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Reflection; using ZeroLevel.Services.Config; using ZeroLevel.Services.Config.Implementation; using ZeroLevel.Services.Serialization; namespace ZeroLevel { public static class Configuration { /// /// Application folder path /// public static string BaseDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); public static string AppLocation = Assembly.GetEntryAssembly()?.Location; public const string DEFAULT_SECTION_NAME = "_defaultsection"; #region Ctor static Configuration() { _empty = new BaseConfiguration(); _emptySet = new BaseConfigurationSet(); _empty.Freeze(true); _emptySet.FreezeConfiguration(true); DefaultSet = Configuration.CreateSet(); } #endregion Ctor #region Cachee private static readonly IConfiguration _empty; private static readonly IConfigurationSet _emptySet; private static readonly ConcurrentDictionary _cachee = new ConcurrentDictionary(); private static readonly ConcurrentDictionary _setCachee = new ConcurrentDictionary(); public static IConfiguration Empty { get { return _empty; } } public static IConfigurationSet EmptySet { get { return _emptySet; } } public static IConfiguration Default => DefaultSet?.Default; public static IConfigurationSet DefaultSet { get; private set; } public static void Save(string name, IConfiguration configuration) { _cachee.AddOrUpdate(name, configuration, (oldKey, oldValue) => configuration); } public static void Save(IConfiguration configuration) { if (DefaultSet == null) { DefaultSet = Configuration.CreateSet(configuration); } else { DefaultSet.Merge(Configuration.CreateSet(configuration)); } } public static void Save(string name, IConfigurationSet configurationSet) { _setCachee.AddOrUpdate(name, configurationSet, (oldKey, oldValue) => configurationSet); } public static void Save(IConfigurationSet configuration) { if (DefaultSet == null) { DefaultSet = configuration; } else { DefaultSet.Merge(configuration); } } public static IConfiguration Get(string name) { IConfiguration result; if (false == _cachee.TryGetValue(name, out result)) { throw new KeyNotFoundException("Not found configuration '{name}'"); } return result; } public static IConfigurationSet GetSet(string name) { IConfigurationSet result; if (false == _setCachee.TryGetValue(name, out result)) { throw new KeyNotFoundException("Not found configuration set '{name}'"); } return result; } #endregion Cachee #region Factory public static IConfiguration Create() { return new BaseConfiguration(); } public static IConfigurationSet CreateSet() { return new BaseConfigurationSet(); } public static IConfigurationSet CreateSet(IConfiguration defaultConfiguration) { return new BaseConfigurationSet(defaultConfiguration); } #endregion Factory #region Read configuration /// /// Creating a configuration from the AppSettings section of the app.config or web.config file /// /// Configuration public static IConfiguration ReadFromApplicationConfig() { return new ApplicationConfigReader().ReadConfiguration(); } /// /// Creating a configuration from the AppSettings section of the app.config file or web.config, is supplemented by the 'ConnectionStrings' section /// /// Configuration public static IConfigurationSet ReadSetFromApplicationConfig() { return new ApplicationConfigReader().ReadConfigurationSet(); } /// /// Creating a configuration from the AppSettings section of the app.config or web.config file /// /// Configuration public static IConfiguration ReadFromApplicationConfig(string configFilePath) { return new ApplicationConfigReader(configFilePath).ReadConfiguration(); } /// /// Creating a configuration from the AppSettings section of the app.config file or web.config, is supplemented by the 'ConnectionStrings' section /// /// Configuration public static IConfigurationSet ReadSetFromApplicationConfig(string configFilePath) { return new ApplicationConfigReader(configFilePath).ReadConfigurationSet(); } /// /// Create configuration from ini file /// /// Path to the ini file /// Configuration public static IConfiguration ReadFromIniFile(string path) { return new IniFileReader(path).ReadConfiguration(); } /// /// Creating a configuration from an ini file, including sections /// /// Path to the ini file /// Configuration public static IConfigurationSet ReadSetFromIniFile(string path) { return new IniFileReader(path).ReadConfigurationSet(); } /// /// Creating configuration from command line parameters /// /// Command line parameters /// Configuration public static IConfiguration ReadFromCommandLine(string[] args) { return new CommandLineReader(args).ReadConfiguration(); } public static IConfigurationSet ReadBinary(IBinaryReader reader) { return reader.Read(); } #endregion Read configuration #region Write configuration /// /// Write a simple configuration to the ini file /// /// Configuration /// Path to the ini file public static void WriteToIniFile(IConfiguration configuration, string path) { new IniFileWriter(path).WriteConfiguration(configuration); } /// /// Write the complete configuration to the ini-file /// /// Configuration /// Path to the ini file public static void WriteSetToIniFile(IConfigurationSet configuration, string path) { new IniFileWriter(path).WriteConfigurationSet(configuration); } #endregion Write configuration } }