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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 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); } } } |
1 2 3 4 5 6 | string [] operations = Config.OPERATION_LIST.Split( '|' ) .Select(op => op.Trim()) .ToArray(); string [] objects= Config.OBJECT_LIST.Split( '|' ) .Select(op => op.Trim()) .ToArray(); |
1 2 3 4 5 6 | <!--xml version="1.0" encoding="utf-8"?--> < configuration > < appsettings > < add key = "OPERATION_LIST" value = "Create|Read|Update|Delete|Assign|Revoke|Enable|Disable|Expire|Unexpire" ></ add > </ appsettings > </ configuration > |
Okay, I'm done for today.
UPDATE: And here is an even better option: BasicConfiguration.
No comments:
Post a Comment