This time I wanted to present a scheme, what I have been using for hosting global configuration parameters in some of my applications. Presented scheme solves three following issues :
- Configuration set used by program executable can easily be changed to another set
- There is no need to touch App.Config file
- All configuration properties are global, static and safe (read-only)
When running program executable for the both configuration files (path to folder in which the actual configuration XML file exists, will be given as a parameter for executable), we get the following console printouts :
When the program starts, path to configuration folder is captured (command line argument). Calling any class property of static Configurations class will automatically trigger static class constructor. Static class constructor will then use XML serializer (ObjectXMLHandler<T> class) for creating Configuration object instance (nested class inside static Configurations class). Finally, properties of newly created Configuration object will be assigned to properties of static Configurations class.
For testing this scheme, just replicate XML configuration files, create a new console application (GlobalConfigurations) and copyPaste the following code into CS-file.
Thanks a lot for using your precious time and reading my blog.