I work in an environment where I need to have a development version of my application, connected to a development database, with all file paths pointed to places most likely located on my own computer. When I am ready for user testing and load testing, I deploy to a clone of my production environment, with its own database server, web server, batch server, and file server. Finally, when I am ready to deploy to production, that environment also has its own set of servers.
Like everyone else, I use a configuration file to store all of the connection strings, file paths, and other “stuff”. In the old days, part of my deploy procedure was to make all of my config file changes as the code was moved from place to place. Then one day, a fellow employee told of a magical bit of code from the land of wizards and wonder that he had used to make all of this happen magically.
OK, actually, he started us on the path to a custom configuration tool.
With this tool, we were able to add our own sections to the config file, determine what environment we were on, and load a cache of the custom configuration specific to the environment on which the application is running.
Here is a simple version of a configuration file we use:
Now, a few explanations. At the top, in the configSections section, we are warning the configuration manager of the custom sections we are adding.
One is the HostServer section, where we are telling the Config.cs class below which machine will be using which environment. Notice that on my computer, I am using the Development-Test environment, so that I can test on my localhost against the test database.
Next is the actual environments. We have Test, Development, and Production, along with environment definitions for testing locally against the test and production databases.
Finally, notice we still have some settings in the appSettings section. These will apply across all environments. One simple call to Config.GetValue([keyname]) will get the value of any configuration setting, whether in the custom section or the app settings section.
Now for our wrapper class file, Config.cs:
A couple of notes on this file:
- Cache lasts for 5 minutes. I suppose that could be a config setting. That would be SO META!!
- You can explicitly SET the environment. This is helpful in test programs.
- No need to call ANYTHING except Config.GetValue([keyname]). It loads the environment, loads the config into the cache, and returns the value. Magic!
That is all for now. Props to Kyle for starting us down this road. It is one of the most indispensable members of our Utilities library.