|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis article describes a simple mechanism to use to XML serialization within configuration files to support strongly typed configuration items in your projects. BackgroundThis article uses the following concepts (and readers are expected to have a basic understanding of the same):
Using the codeNote:
Strongly typed configuration entities are very much advantageous in terms of code clarity and readability, ease of programming etc. Consider a hypothetical situation where we need to save the name and IP of an email server within our configuration file. We need to be able to retrieve these details from the configuration file as strongly typed entities. Meaning, we could do with a Of course this is just an example, and the approach can be easily reused and extended for configurable entities of your choice. First thing is to define the ///
This is really straightforward. To allow for XML serialization, we mark up the properties and class with the necessary XML attributes. .NET 1.1 ApproachNext, we define a generic XML serialization based configuration section handler. What this class does is it implements the ///
At this stage we have the fundamental blocks required; a simple helper class that gives us the strongly typed configuration entities is also written. ///
All the helper class does is it provides some userful routines that casts the configuration objects to the specified type. That's it. These three classes form the core classes in the To use them, first we need to update our configuration file. The following is a simple configuration file that uses <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!-- The following is used to describe our custom section -->
<section name="EmailServerSettings"
type="Citrus.Configuration.XmlSectionHandler`1
[[Citrus.Configuration.EmailServerSettings,
Citrus.Configuration,
Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=null]],
Citrus.Configuration,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" />
</configSections>
<!-- Now that our section has been declared, use it -->
<EmailServerSettings configSource="EmailServerSettings.config" />
</configuration>
The external <!-- A simple XML file containing settings for a email server -->
<EmailServerSettings>
<Name>Coldmail</Name>
<IP>127.0.0.1</IP>
</EmailServerSettings>
Now we are ready to actually use all this in code: // The following will give us the email server settings
EmailServerSettings emailServerSettings1 = ConfigurationHelper.GetSection
In fact you can easily use To extend this for your requirements, you would need to define your own configuration specific classes (like the .NET 2.0 ApproachSince the public class XmlSection<T> : ConfigurationSection where T: class
(Note the class name has changed in the .NET 2.0 version). Subtle modifications have to be made to allow the class to handle XML serialization in this case. These changes include overriding the // The object that represents our email server's settings
EmailServerSettings anEmailServer;
// Get the section that is specified an external configuration file
anEmailServer = XmlSection<EmailServerSettings>.GetSection("EmailSettings");
Console.WriteLine(anEmailServer);
// Update the section, refresh and load it again
Config.Configuration c = Config.ConfigurationManager.OpenExeConfiguration(Config.ConfigurationUserLevel.None);
XmlSection<EmailServerSettings>.GetSection("EmailSettings", c).Name = "Hello";
c.Save();
Config.ConfigurationManager.RefreshSection("EmailSettings");
anEmailServer = XmlSection<EmailServerSettings>.GetSection("EmailSettings");
Console.WriteLine(anEmailServer);
// Get settings that are specified inline
anEmailServer = XmlSection<EmailServerSettings>.GetSection("ProxyEmailServer");
Console.WriteLine(anEmailServer);
The corresponding config file is as: <configuration>
<configSections>
<section name="EmailSettings"
type="Citrus.Configuration.XmlSection`1
[[Citrus.Configuration.EmailServerSettings, Citrus.Configuration]],
Citrus.Configuration"/>
<section name="ProxyEmailServer"
type="Citrus.Configuration.XmlSection`1
[[Citrus.Configuration.EmailServerSettings, Citrus.Configuration]],
Citrus.Configuration"/>
</configSections>
<EmailSettings configSource="EmailSettings.config" />
<ProxyEmailServer>
<Name>Proxy</Name>
<IP>127.0.0.1</IP>
</ProxyEmailServer>
</configuration>
Let me know if all this is helpful in some way to you. Enjoy. History[ .] Initial version
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||