/// <summary> /// Gets a value from the config store. Returns default value if config value not defined /// </summary> /// <param name="key">The key of the configuration value to get</param> /// <param name="defaultValue">The default value to return, if configuration key is not present</param> /// <param name="dependencies">The dependencies of the configuration value</param> /// <typeparam name="T"></typeparam> /// <returns>The value of the requested config</returns> public T GetConfigValue <T>(string key, T defaultValue = default(T), params IConfigurationDependency[] dependencies) where T : IConvertible { var fileName = GetFileNameFromDependencies(dependencies); var configNotFound = false; var configFile = ConfigFiles.Find(x => string.Equals(x.Name, fileName, StringComparison.CurrentCultureIgnoreCase)); // if config file does not exist or is empty // return default value for the requested type (means config value is not set) if (configFile == null || configFile.Sections == null || configFile.Sections.Count == 0) { configNotFound = true; } if (!configNotFound) { if (dependencies == null || dependencies.Length == 0) { return(GetConfigValue <T>(configFile.Sections.First(), key, defaultValue)); } var dependenciesOrdered = dependencies.Where(x => x != null).OrderBy(x => x.ConfigOrder).ToList(); var curSection = configFile.Sections.Find(x => x.Id == dependenciesOrdered[0].ConfigId); if (dependenciesOrdered.Count == 1) { return(GetConfigValue <T>(curSection, key, defaultValue)); } // Find the config entry section foreach (var dependency in dependenciesOrdered.Skip(1)) { // Ignore null entries if (dependency == null) { continue; } // If section was not found, return default value (config value not set) if (curSection == null) { configNotFound = true; return(defaultValue); } curSection = curSection.SubSections.Find(x => x.Id == dependency.ConfigId); return(GetConfigValue <T>(curSection, key, defaultValue)); } } // If section was not found, return default value (config value not set) return(defaultValue); }
/// <summary> /// Gets a list of all currently set config values for given dependencies /// </summary> /// <param name="dependencies"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public IReadOnlyList <ConfigValue> GetAllConfigValues(params IConfigurationDependency[] dependencies) { var fileName = GetFileNameFromDependencies(dependencies); var configFile = ConfigFiles.Find(x => string.Equals(x.Name, fileName, StringComparison.CurrentCultureIgnoreCase)); // if config file does not exist or is empty // return default value for the requested type (means config value is not set) if (configFile == null || configFile.Sections == null || configFile.Sections.Count == 0) { return(new List <ConfigValue>()); } var curSection = GetConfigSectionOrNull(configFile, dependencies); return(curSection != null ? curSection.Config : new List <ConfigValue>()); }
/// <summary> /// Checks whether a given setting is defined /// </summary> /// <param name="key"></param> /// <param name="dependencies"></param> /// <returns></returns> public bool DoesConfigEntryWithKeyExist(string key, params IConfigurationDependency[] dependencies) { var fileName = GetFileNameFromDependencies(dependencies); // Get file, create it if it doesnt exist yet var configFile = ConfigFiles.Find(x => string.Equals(x.Name, fileName, StringComparison.CurrentCultureIgnoreCase)); if (configFile == null) { return(false); } var section = GetConfigSectionOrNull(configFile, dependencies); if (section == null) { return(false); } return(section.Config.Any(x => x.Key == key)); }
/// <summary> /// Removes a given config entry /// </summary> /// <param name="key">The key of the configuration to remove</param> /// <param name="dependencies"></param> public void DeleteConfigEntry(string key, params IConfigurationDependency[] dependencies) { var fileName = GetFileNameFromDependencies(dependencies); // Get file, create it if it doesnt exist yet var configFile = ConfigFiles.Find(x => string.Equals(x.Name, fileName, StringComparison.CurrentCultureIgnoreCase)); if (configFile == null) { return; } var section = GetConfigSectionOrNull(configFile, dependencies); if (section == null) { return; } section.Config.RemoveAll(x => x.Key == key); SaveConfigFile(configFile); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="dependencies"></param> public void SetConfigValue <T>(string key, T value, params IConfigurationDependency[] dependencies) where T : IConvertible { var fileName = GetFileNameFromDependencies(dependencies); // Get file, create it if it doesnt exist yet var configFile = ConfigFiles.Find(x => string.Equals(x.Name, fileName, StringComparison.CurrentCultureIgnoreCase)); if (configFile == null) { configFile = new ConfigFile { Name = fileName.ToLower(), Sections = new List <ConfigSection>() }; ConfigFiles.Add(configFile); } var dependenciesSorted = dependencies.Where(x => x != null).OrderBy(x => x.ConfigOrder); ConfigSection curParentSection = null; if (dependencies == null || dependencies.Length == 0) { ConfigSection section = null; if (configFile.Sections.Count == 0) { section = new ConfigSection { Id = "global" }; configFile.Sections.Add(section); } else { section = configFile.Sections.First(); } SetConfigValue(key, value, section); SaveConfigFile(configFile); } // Find the section foreach (var dependency in dependenciesSorted) { ConfigSection section = null; if (curParentSection == null) { section = configFile.Sections.Find(x => x.Id == dependency.ConfigId); } else { section = curParentSection.SubSections.Find(x => x.Id == dependency.ConfigId); } // Create section if it doesnt exist yet if (section == null) { section = new ConfigSection { Id = dependency.ConfigId }; // Add to parent if (curParentSection == null) { configFile.Sections.Add(section); } else { curParentSection.SubSections.Add(section); } } curParentSection = section; // Save the config value SetConfigValue(key, value, curParentSection); } SaveConfigFile(configFile); }