// TODO Cleanup public void AddFactory <T>(IConfigurationFileFactory <T> factory) where T : IConfiguration { if (factories.TryGetValue(typeof(T), out _)) { throw new InvalidOperationException($"Es gibt bereits eine Factory für den Typ {typeof(T)}"); } factories.Add(typeof(T), factory); }
public T LoadConfiguration <T>() where T : IConfiguration { Type key = typeof(T); if (configurations.ContainsKey(key)) { return((T)configurations[key]); } var filename = key.GetCustomAttribute <ConfigurationFileAttribute>().Filename; string pathToFile = $"{ConfigurationBasePath}/{filename}"; if (!File.Exists(pathToFile)) { IConfigurationFileFactory <T> factory = registry.GetFactory <T>(); T defaultConfiguration = factory.CreateDefaultConfiguration(); WriteConfiguration(defaultConfiguration); configurations.TryAdd(key, defaultConfiguration); if (defaultConfiguration is IChangeableConfiguration changeableConfiguration) { changeableConfiguration.PropertyChanged += ConfigurationChanged; } return(defaultConfiguration); } using (TextReader reader = new StreamReader($"{ConfigurationBasePath}/{filename}")) { string serialzedConfiguration = reader.ReadToEnd(); var configuration = JsonConvert.DeserializeObject <T>(serialzedConfiguration); configurations.TryAdd(key, configuration); if (configuration is IChangeableConfiguration changeableConfiguration) { changeableConfiguration.PropertyChanged += ConfigurationChanged; } return(configuration); } }