Once there was a task to save the application settings in a file. There are various classes in the application that require configuration. At the same time, the application developed, new settings were added and it was inconvenient to adjust loading / unloading of settings elsewhere after changing classes. I tried to structure this process a bit.
In general, the system is arranged as follows: each class is able to save its parameters in XML format in a separate node and in the same way can load them from a separate node.
public interface IConfigurable { void SaveConfig(XmlDocument xmlDoc, XmlNode localRoot); void LoadConfig(XmlNode node); String ModuleName { get; } }
The configuration process is managed by a separate XmlConfig class. I register the classes I want to configure in it:
xmlConf = new XmlConfig(); xmlConf.FileName = System.IO.Directory.GetCurrentDirectory() + "\\config.xml"; xmlConf.RootName = "myconfig"; xmlConf.AddMember(SomeClass1); xmlConf.AddMember(SomeClass2); xmlConf.AddMember(SomeClass3);
and then I call the XmlConfig.ConfigureAll () method, which looks for the ModuleName name in the XML file, the node responsible for the specific class and passes that node to it. The class configures itself and returns control to the configurator, which proceeds to the next class.
public void ConfigureAll() { foreach (IConfigurable iconf in objectsToConfigure) { XmlNode node = confDocument[rootName][iconf.ModuleName]; iconf.LoadConfig(node); } }
But I have never seen such implementations anywhere, and I have found little information on the configuration of various application modules. Maybe I invented the bike and there are more elegant ways to load / save many different parameters?
How do you solve such problems? What to read on this topic?