IgorShare Thoughts and Ideas

Consulting and Training

Prism: File-configuration-driven bootstrapper

Posted by Igor Moochnick on 09/04/2008

If you’d like to extract all your application configuration into a config file, this is what you can do:

1. Create a config file. I suggest that you use a different file name than the App.config

2. Override ConfigureContainer method in your bootstrapper

private UnityConfigurationSection unitySection;

private void GetConfigurationSection()
{
    if  (unitySection == null)
    {
        var configMap = new ExeConfigurationFileMap { ExeConfigFilename = "myApp.config" };
        var configuration = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

        unitySection = (UnityConfigurationSection)configuration.GetSection("unity");
    }
}

protected override void ConfigureContainer()
{
    base.ConfigureContainer();

    GetConfigurationSection();

    try
    {
        unitySection.Containers["global"].Configure(Container);
    }
    catch (Exception e)
    {
        System.Diagnostics.Trace.WriteLine(e.ToString());
    }
}

3. Point the module enumerator to the same config file via ConfigurationStore

protected override IModuleEnumerator GetModuleEnumerator()
{
    string baseDir = AppDomain.CurrentDomain.BaseDirectory;
    var store = new ConfigurationStore(baseDir);
    return new ConfigurationModuleEnumerator(store);
}

 

All this is fun, but, in case you have more than one config file that contains a Modules section – the ConfigurationStore will stop reading them as soon as it’ll find a first one. It may be the file that you needed in a first place, but, as well, it may be some other file.

Here is a simple workaround:

private UnityConfigurationSection unitySection;
private ModulesConfigurationSection modulesSection;

private void GetConfigurationSections()
{
    if ((unitySection == null) && (modulesSection == null))
    {
        var configMap = new ExeConfigurationFileMap { ExeConfigFilename = "myApp.config" };
        var configuration = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

        unitySection = (UnityConfigurationSection)configuration.GetSection("unity");

        modulesSection = (ModulesConfigurationSection)configuration.GetSection("modules");
    }
}

protected override void ConfigureContainer()
{
    base.ConfigureContainer();

    GetConfigurationSections();

    try
    {
        unitySection.Containers["global"].Configure(Container);
    }
    catch (Exception e)
    {
        System.Diagnostics.Trace.WriteLine(e.ToString());
    }
}

protected override IModuleEnumerator GetModuleEnumerator()
{
    GetConfigurationSections();

    return new ConfigurationModuleEnumerator(new MyConfigStore(modulesSection));
}

public class MyConfigStore : ConfigurationStore
{
    public ModulesConfigurationSection Section { get; private set; }

    public MyConfigStore(ModulesConfigurationSection section)
    {
        Section = section;
    }

    public override ModulesConfigurationSection RetrieveModuleConfigurationSection()
    {
        return Section;
    }
}

 

As you can see, by a simple overload of the ConfigurationStore, you can return exactly the modules section that you need. Note that this section is pre-retrieved by the same function that retrieves the “unity” configuration section.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: