Created
June 13, 2012 14:49
-
-
Save codearoo/2924540 to your computer and use it in GitHub Desktop.
Cleaner way to define and use App.config or Web.config settings in .Net. Setting names correspond to the namespace and class name of where they are defined in the code. Makes it easy to know exactly where the setting is defined.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<configuration> | |
<appSettings> | |
<!--This is how your config file would look.--> | |
<add key="SomeNameSpace.Class1.STATIC" value="This is my static setting"/> | |
<add key="SomeNameSpace.Class1.STATIC_VERIFIED" value="This is my static setting that is verified to exist"/> | |
<add key="SomeNameSpace.Class1.NONSTATIC" value="This is my non-static setting"/> | |
<add key="SomeNameSpace.Class1.TESTMETHOD.SOMETHING.SPECIFIC" value="Some specific setting"/> | |
<add key="SomeNameSpace.Class1.TESTMETHOD_INT" value="5"/> | |
<add key="SomeOtherNameSpace.Class2.STATIC_VERIFIED" value=""/> | |
</appSettings> | |
</configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ClassLibrary1 | |
{ | |
public class MyConfigSetting | |
{ | |
/// <summary> | |
/// To define your config setting. | |
/// </summary> | |
/// <param name="settingName"></param> | |
public MyConfigSetting(string settingName) | |
{ | |
// StackFrame of 0 would mean this method, but we want the one who called this method, so skip 1. | |
_fullReflectedName = | |
new System.Diagnostics.StackFrame(1).GetMethod().ReflectedType.FullName | |
+ "." | |
+ settingName; | |
// Be aware that if you debug and step into this constructor method, the Stack will be different and this will not | |
// find the config setting. This only happens if you step into this method. | |
} | |
/// <summary> | |
/// Use this after the constructor to verify that the setting is found when defining it. | |
/// </summary> | |
/// <returns></returns> | |
public MyConfigSetting Verify() | |
{ | |
string test = Value; | |
return this; | |
} | |
/// <summary> | |
/// The setting value. | |
/// </summary> | |
public string Value | |
{ | |
// For better performance, cache the value in a private variable and only load it if it's not set. | |
get | |
{ | |
string setting; | |
// .Net 2.0 version: | |
// Requires adding reference to project: System.Configuration | |
setting = System.Configuration.ConfigurationManager.AppSettings[_fullReflectedName]; | |
// .Net 1.0 version: | |
//setting = System.Configuration.ConfigurationSettings.AppSettings[_fullReflectedName]; | |
if (setting == null) | |
// .Net 2.0 version: | |
// Requires adding reference to project: System.Configuration | |
throw new System.Configuration.ConfigurationErrorsException(_fullReflectedName + " setting not defined."); | |
// .Net 1.0 version: | |
//throw new System.ApplicationException(_fullReflectedName + " setting not defined."); | |
return setting; | |
} | |
} | |
private string _fullReflectedName; | |
/// <summary> | |
/// Returns the actual value of the setting. | |
/// </summary> | |
/// <returns></returns> | |
public override string ToString() | |
{ | |
return Value; | |
} | |
/// <summary> | |
/// Lets you use this class instance as if it were a string config setting. | |
/// </summary> | |
/// <param name="setting"></param> | |
/// <returns></returns> | |
public static implicit operator string(MyConfigSetting setting) | |
{ | |
return setting.Value; | |
} | |
/// <summary> | |
/// Lets you use this class instance as if it were an int config setting. | |
/// Will die here if the value can not be parsed into an int. | |
/// </summary> | |
/// <param name="setting"></param> | |
/// <returns></returns> | |
public static implicit operator int(MyConfigSetting setting) | |
{ | |
int x; | |
try | |
{ | |
x = int.Parse(setting.Value); | |
return x; | |
} | |
catch (System.Exception ex) | |
{ | |
throw new System.Exception(setting._fullReflectedName + " setting had error.", ex); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using ClassLibrary1; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
SomeNameSpace.Class1 c = new SomeNameSpace.Class1(); | |
c.TestMethod(); | |
SomeOtherNameSpace.Class2 c2 = new SomeOtherNameSpace.Class2(); | |
c2.TestMethod(); | |
c2.TestMethod2(); | |
} | |
catch (Exception ex) | |
{ | |
Console.Error.WriteLine(ex.ToString()); | |
} | |
} | |
} | |
} | |
namespace SomeNameSpace | |
{ | |
public class Class1 | |
{ | |
private static MyConfigSetting CLASS1_STATIC = new MyConfigSetting("STATIC"); | |
private static MyConfigSetting CLASS1_STATIC_VERIFIED = new MyConfigSetting("STATIC_VERIFIED").Verify(); | |
private MyConfigSetting CLASS1_NONSTATIC = new MyConfigSetting("NONSTATIC"); | |
public void TestMethod() | |
{ | |
MyConfigSetting CLASS1_TESTMETHOD_INT = new MyConfigSetting("TESTMETHOD_INT"); | |
MyConfigSetting CLASS1_TESTMETHOD_SPECIFIC = new MyConfigSetting("TESTMETHOD.SOMETHING.SPECIFIC"); | |
int x = CLASS1_TESTMETHOD_INT; | |
string y = CLASS1_TESTMETHOD_SPECIFIC; | |
Console.WriteLine( | |
"CLASS1_STATIC = {0}\nCLASS1_NONSTATIC = {1}\nCLASS1_TESTMETHOD_INT = {2}\nCLASS1_TESTMETHOD_SPECIFIC = {3}" | |
, CLASS1_STATIC, CLASS1_NONSTATIC, CLASS1_TESTMETHOD_INT, CLASS1_TESTMETHOD_SPECIFIC | |
); | |
} | |
} | |
} | |
namespace SomeOtherNameSpace | |
{ | |
public class Class2 | |
{ | |
private static MyConfigSetting CLASS2_STATIC = new MyConfigSetting("STATIC"); | |
private static MyConfigSetting CLASS2_STATIC_VERIFIED = new MyConfigSetting("STATIC_VERIFIED").Verify(); | |
public void TestMethod() | |
{ | |
Console.WriteLine("Even if Class2.STATIC is not set, this code still runs fine."); | |
} | |
public void TestMethod2() | |
{ | |
Console.WriteLine("CLASS2_STATIC = {0}", CLASS2_STATIC); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replaced use of overloaded constructor to verify with a .Verrify() method that you can chain after calling the constructor.