CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

David Hayden [MVP C#]

         .NET Tutorials, Patterns, and Practices

Enterprise Library - Configuration Application Block - Patterns and Practices

Armed with our knowledge of XML Serialization, we can certainly start creating a solution for accessing and persisting global settings in our ASP.NET application.  However, we may want to back up a bit and look at the Configuration Application Block created by the Microsoft Patterns and Practices Group.  Even if we decide not to use the application block, we certainly want to at least understand how it works so we can justify our decision to our boss or client - but even more important, the guys at the next .NET Pub Club or Code Brew meeting!

After reading the documentation, one essentially runs the Enterprise Library Configuration Application to help create the configuration information that needs to be placed in the application's config file.

 

Configuration Application Block

 

Notice that the application block comes with an XML File Storage Provider and an XML Serializer Transformer.  Heck, this sounds like XML Serialization to me, so learning it was time well spent.

After making the configurations and saving the results, we end up getting two files: configtest.config (with the configuration information) and a blank sitesettings.config file.  The configuration information looks like this:

 

Configuration Settings
<configuration>
  <configSections>
    <section name="enterpriselibrary.configurationSettings"
        type="Microsoft.Practices.EnterpriseLibrary.Configuration.
        ConfigurationManagerSectionHandler,
        Microsoft.Practices.EnterpriseLibrary.Configuration, Version=1.0.0.0,
        Culture=neutral,
        PublicKeyToken=null" />
  </configSections>
  <enterpriselibrary.configurationSettings
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        applicationName="configtest"
        xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/configuration">
  <configurationSections>
    <configurationSection name="SiteSettings" encrypt="false">
      <storageProvider xsi:type="XmlFileStorageProviderData"
           name="XML File Storage Provider"
           path="sitesettings.config" />
      <dataTransformer xsi:type="XmlSerializerTransformerData"
           name="Xml Serializer Transformer">
        <includeTypes />
      </dataTransformer>
    </configurationSection>
  </configurationSections>
  <keyAlgorithmStorageProvider xsi:nil="true" />
  <includeTypes />
</enterpriselibrary.configurationSettings>
</configuration>

 

I copy over a couple of the Configuration Application Block DLL's as shown above and run Snippet Compiler to test it out. Here is the code:

 

Using the Configuration Application Block
using System;
using Microsoft.Practices.EnterpriseLibrary.Configuration;

public class SiteSettings
{
    #region Private Members
    
    private string _name;
    private string _url;
    private string _emailAddress;
    
    #endregion
    
    #region Properties

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    
    public string Url
    {
        get { return _url; }
        set { _url = value; }
    }
    
    public string EmailAddress
    {
        get { return _emailAddress; }
        set { _emailAddress = value; }
    }
    
    #endregion
}

public class TestClass
{
    public static void Main()
    {
        SiteSettings siteSettings = new SiteSettings();

        siteSettings.Name = "DavidHayden.com";
        siteSettings.Url = "http://www.davidhayden.com/";
        siteSettings.EmailAddress = "nospam@nospam.com";
        
        ConfigurationManager.WriteConfiguration("SiteSettings", siteSettings);
        
        SiteSettings mySettings = ConfigurationManager.GetConfiguration("SiteSettings") as SiteSettings;
        
        if (mySettings != null)
        {
            Console.WriteLine("Name = {0}", mySettings.Name);
            Console.WriteLine("Url = {0}", mySettings.Url);
            Console.WriteLine("Email Address = {0}", mySettings.EmailAddress);
            Console.ReadLine();
        }
    }
}

 

Here is the now populated sitesettings.config file with the global application settings:

 

Global Application Configuration Settings
<?xml version="1.0" encoding="utf-8"?>
<SiteSettings>
  <xmlSerializerSection type="SiteSettings, configtest,
            Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
    <SiteSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Name>DavidHayden.com</Name>
      <Url>http://www.davidhayden.com/</Url>
      <EmailAddress>nospam@nospam.com</EmailAddress>
    </SiteSettings>
  </xmlSerializerSection>
</SiteSettings>

 

This looks like the same output as the previous XML Serialization Example.  Man, this is sweet!  Not only are the results basically the same, but I have totally abstracted the storage of the global application settings so that I could change it in the future.  Notice that all my references to anything concerning XML are gone.  I like this better.  Besides, anytime you can use the word "Enterprise" in a sentence has got to be worth something :)



Comments

TrackBack said:

# March 9, 2005 9:39 PM

TrackBack said:

# March 18, 2005 9:06 AM
Check out Devlicio.us!

This Blog

Syndication

News

CodeBetter.Com Home