XAML Configuration Section 1.0

Configuration sections are used to load information from app.config and web.config files. .NET focuses on two primary ways to maintain application configuration settings: appSettings and custom configuration sections. Using appSettings is great, if you only have simple types and are comfortable with type-casting -- a string here, an integer there -- but it truly falls short when you require complex types or want more than simple strings for configuration. The XamlConfigurationSection allows you to create any class structure you want to store configuration details and represent that in your config file as XAML.

As an example, if I want to maintain my application's name, version, and a list of authors -- which I cannot do easily with appSettings -- I might create the following configuration classes.

[ContentProperty("Authors")]
public class MyAppConfig
{
    public string Name { get; set; }
    public string Version { get; set; }
    public Author[] Authors { get; set; }
}
public class Author
{
    public string Name { get; set; }
    public Uri WebSite { get; set; }
    public Uri Rss { get; set; }
}

Then, in my config file, I would have the following.

...
<configSections>
    <section name="myConfig" type="Synergy.Configuration.XamlConfigurationSection, Synergy.Configuration, Version=0.0.0.6, Culture=neutral, PublicKeyToken=2a6131a34dae32ab"/>
</configSections>
<myConfig>
    <MyAppConfig Name="My App" Version="1.0" xmlns="clr-namespace:MyApp.Configuration;assembly=MyApp">
        <Author Name="John Doe" WebSite="http://john.doe" Rss="http://john.doe/rss"/>
        <Author Name="Jane Doe" WebSite="http://jane.doe" Rss="http://jane.doe/rss"/>
    </MyAppConfig>
</myConfig>
...

Lastly, from code, simply access the configuration settings using one of the following methods. Note that the configuration settings are read once, during app startup, so the settings are not parsed every time the class is accessed.

// standard method for retrieving configuration sections
MyAppConfig config = (ConfigurationManager.GetSection("myConfig") as XamlConfigurationSection).GetValue<MyAppConfig>()

// more direct approach
MyAppConfig config = XamlConfigurationSection.Load<MyAppConfig>("myConfig");

This is still a relatively simple example, but shows one of the benefits over appSettings, which only supports simple types. By adding in more advanced XAML concepts, like data binding and converters, the possibilities are endless. Of course, XAML files can be fairly large on their own, so to avoid too much bloat within config files, use the file attribute to specify a separate file to store the XAML. This file should be either an absolute, fully-qualified path or a relative path based on the app.config or web.config file. The following is an example of a myapp.xaml.config file placed in the same directory as an app.config or web.config file. Note that the file attribute will override any internal XAML that is specified.

...
<configSections>
    <section name="myConfig" type="Synergy.Configuration.XamlConfigurationSection, Synergy.Configuration, Version=0.0.0.6, Culture=neutral, PublicKeyToken=2a6131a34dae32ab"/>
</configSections>
<myConfig file="myapp.xaml.config"/>
...

Properties

  • File -- File to retrieve XAML from (to avoid config file bloat).

Methods

  • GetValue<T>() -- Gets a serialized object representation of the XAML specified in the configuration section.
  • Load<T>(string sectionName) -- Static method that gets a serialized object representation of the XAML specified in the configuration section identified by the sectionName parameter.

Details

Assembly: Synergy.Configuration
Namespace: Synergy.Configuration
Class: XamlConfigurationSection

Last edited Dec 30, 2010 at 12:13 AM by flanakin, version 3

Comments

No comments yet.