Minimize your app config file
Summary: How to keep application configuration files (app.config, web.config) nice and clean.
We use configuration files to store application settings that can be modified, so we do not need to recompile the application. Some applications have lots of settings, but many of these settings are not likely to change or may only change when the application is rebuilt. In such case, here is a nice technique that will allow you to reduce the size of the config file.Here is the basic idea:
- Create a static configuration class (let's call it Config).
- In the Config class, implement static methods to get a configuration property value that either gets it from the application's config file (if the setting is defined in the appSettings section) or uses the passed default (you'd need to implement these methods for different data types).
- In the Config class, define the static properties that get initialized by calling the methods mentioned above with the hard-coded defaults. To make it easier to remember, the config file's appSettings keys must be named after the Config class properties.
Here is the code:
The configuration class is responsible for initialization of the application settings:
using System; using System.Configuration; namespace MyApp.Configuration { public static class Config { public static string OPERATION_LIST = GetValue("OPERATION_LIST", "Create|Read|Update|Delete|Assign|Revoke|Enable|Disable"); public static string OBJECT_LIST = GetValue("OBJECT_LIST", "User|Group|Role"); private static string GetValue ( string keyName, string defaultValue = null ) { string configValue = ConfigurationManager.AppSettings.Get(keyName); if (String.IsNullOrEmpty(configValue)) return defaultValue; return configValue; } private static int GetValue ( string keyName, int defaultValue ) { string configValue = ConfigurationManager.AppSettings.Get(keyName); if (String.IsNullOrEmpty(configValue)) return defaultValue; return Int32.Parse(configValue); } private static bool GetValue ( string keyName, bool defaultValue ) { string configValue = ConfigurationManager.AppSettings.Get(keyName); if (String.IsNullOrEmpty(configValue)) return defaultValue; return bool.Parse(configValue); } private static object GetValue ( string keyName, Enum defaultValue, Type type ) { string configValue = ConfigurationManager.AppSettings.Get(keyName); if (String.IsNullOrEmpty(configValue)) return defaultValue; return Enum.Parse(type, configValue); } } }Using an application setting is now as easy as referencing the corresponding configuration class property:
string[] operations = Config.OPERATION_LIST.Split('|') .Select(op => op.Trim()) .ToArray(); string[] objects= Config.OBJECT_LIST.Split('|') .Select(op => op.Trim()) .ToArray();Notice that we do not need to define these values in the application config file unless we have to modify them before we release an application update, in which case, simply add them to the appSettings section:
I assume this must be obvious but just in case: NEVER HARD CODE SENSITIVE INFORMATION (PASSWORDS, ENCRYPTION KEYS, ETC) IN THE APPLICATION SOURCE CODE. Okay, I'm done for today.
UPDATE: And here is an even better option: BasicConfiguration.