Configuration settings
I am currently working on a project for LL and a good developer I know (John Liu) wrote a little piece of code which took a configuration section from the config file and transformed it into a class, with very little code. Amazing!!!
I'm referring to the .Net 2.0 configuration class. this little beauty lets you add a custom configuration section to your config and with a few def attributes, lets you wire up the config settings to properties within a class. I started trying to understand how this works, and found it was very difficult to find the information I was looking for on the net. So I decided to write my own point of reference, so that I dont forget it. (Just because it is so cool)
First off, let have a look at the key points to making this work.
The Config file
Notice the config section which needs a name and the assembly name of the class which will be parsing this config file in this case Notfication and Sharepoint.Notifier.Config.NotificationCollection, Sharepoint.Notifier
Now this is the custom bit, which allows you to add any properties you like.
With this, now we can build our custom class to handle the configuration settings. you will need 3 classes.
The Object
1) Settings class - > holds the individual properties
2) Collection class -> Is the object that holds the collection of settings
3) Section class -> initiates the whole thing
1) The settings class is pretty straight forward. Create a property, and add the config attribute which ties it to the value in the config file
Points to take not on are the inheritored class (ConfigurationElement) and this["ID"] is case sensitive
2) Next the collection container needs to configured
this to note are inherits from ConfigurationElementCollection. Attribute is of type NotificationSetting. public NotificationSetting this[int idx] property will help return the idx element of the collection
3) the section class
things to note are inherits from ConfigurationSection. ConfigurationProperty is set to List which is the child container in the Notfication section in our config file. here you can also add any other properties from the appsettings section. This will act like our strongly typed config file.
and now as our final act, we need to initiate the class
things to note here are Getsection with the parameter which passes in the Notification section from our config file.
Easy as pie!!!!
To Initiate class we call
And most importantly you must reference System.Configuration
How easy is that. now you have a custom configuration collection you can access via
with all the properties available from your custom config section
I'm referring to the .Net 2.0 configuration class. this little beauty lets you add a custom configuration section to your config and with a few def attributes, lets you wire up the config settings to properties within a class. I started trying to understand how this works, and found it was very difficult to find the information I was looking for on the net. So I decided to write my own point of reference, so that I dont forget it. (Just because it is so cool)
First off, let have a look at the key points to making this work.
The Config file
Notice the config section which needs a name and the assembly name of the class which will be parsing this config file in this case Notfication and Sharepoint.Notifier.Config.NotificationCollection, Sharepoint.Notifier
<configSections>
<section name="Notfication" type="Sharepoint.Notifier.Config.NotificationCollection, Sharepoint.Notifier" />
</configSections>
Now this is the custom bit, which allows you to add any properties you like.
<Notification>
<List>
<add Id="BirthdayList"
enabled="true"
siteName="Site Name"
/>
</List>
</Notification>
With this, now we can build our custom class to handle the configuration settings. you will need 3 classes.
The Object
1) Settings class - > holds the individual properties
2) Collection class -> Is the object that holds the collection of settings
3) Section class -> initiates the whole thing
1) The settings class is pretty straight forward. Create a property, and add the config attribute which ties it to the value in the config file
namespace Sharepoint.Notifier.Config
{
public class NotificationSetting : ConfigurationElement
{
/// <summary>
/// Id property
/// </summary>
[ConfigurationProperty("ID", IsRequired = true, IsKey = true)]
public string Id
{
get
{
return (string)this["ID"];
}
set
{
this["ID"] = value;
}
}
Points to take not on are the inheritored class (ConfigurationElement) and this["ID"] is case sensitive
2) Next the collection container needs to configured
namespace Sharepoint.Notifier.Config
{
/// <summary>
/// Holds a collection of wssConnection configuration elements
/// </summary>
[ConfigurationCollection(typeof(NotificationSetting),
CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class NotificationCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new NotificationSetting();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as NotificationSetting).Id;
}
public NotificationSetting this[int idx]
{
get {
return this.Cast<NotificationSetting>().ToList()[idx];
}
}
}
}
this to note are inherits from ConfigurationElementCollection. Attribute is of type NotificationSetting. public NotificationSetting this[int idx] property will help return the idx element of the collection
3) the section class
namespace Sharepoint.Notifier.Config
{
public class NotificationSection : ConfigurationSection
{ /// <summary>
/// Gets wssConnection configuration elements
/// </summary>
[ConfigurationProperty("List", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(NotificationCollection))]
public NotificationCollection NotificationList
{
get
{
return (NotificationCollection)base["List"];
}
}
things to note are inherits from ConfigurationSection. ConfigurationProperty is set to List which is the child container in the Notfication section in our config file. here you can also add any other properties from the appsettings section. This will act like our strongly typed config file.
and now as our final act, we need to initiate the class
SettingsSection = ("NotificationSection) System.Configuration.ConfigurationManager.GetSection("Notification");
things to note here are Getsection with the parameter which passes in the Notification section from our config file.
Easy as pie!!!!
Now to recap,
Class structure is as follows
Config file needs config section, and cutom section based in configuration block
NotificationSection
-> NotificationCollection
-> NotificationSetting
To Initiate class we call
SettingsSection = ("NotificationSection) System.Configuration.ConfigurationManager.GetSection("Notification");
And most importantly you must reference System.Configuration
How easy is that. now you have a custom configuration collection you can access via
SettingsSection.NotificationList[0].Id
with all the properties available from your custom config section
Comments
Post a Comment