Automates the process of tying config values back to strong-typed accessors.
Also enforces configuration value naming guidelines of "Fully.Qualified.Namespace.IndividualValue"

The tricky part is that the base class takes the inheritor as a generic argument to enable reflection.

A sample implementation of this class:
namespace Fully.Qualified.Namespace 
{
	public class Config : AutoConfig<Config>
	{
		static Config() { Init(); } // this is critical

		[AutoConfigDefault(4096)]
		public static int IndividualValue { get; private set; }

		public static int ValueWithNoDefault { get; private set; }
		
		public static int UpdateableValueWithNoDefault { get; set; }
		
		[AutoConfigIgnore]
		public static string ManuallyConfiguredProperty { get; set; }
	}
} 


Implementors of this AutoConfig class expect the following in the config file:
<configSections>
	<section name="Fully.Qualified.Namespace" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<Fully.Qualified.Namespace>
	<add key="IndividualValue" value="8096" />
</Fully.Qualified.Namespace> 

// This will also work, and these values override the section if both are provided:
<appSettings>
	<add key="Fully.Qualified.Namespace.IndividualValue" value="65535" /> 
<appSettings> 

Last edited Jul 13, 2011 at 11:33 PM by InfiniteGeek, version 4

Comments

No comments yet.