Click here to Skip to main content
Click here to Skip to main content

Cinchoo - ConfigurationManager Tips 2

By , 21 Apr 2013
 

Introduction

Cinchoo is the application framework for .NET. One of the main functionalities that it provides to the users is application configuration management. Application configuration is the information that the application reads and/or writes at run-time from the source. Please take a look at 'Cinchoo - Simplified Configuration Manager' jump start article on using configuration framework in your application.

In this section, I'll show you the way to attach the existing configuration file to each Configuration Object. By default, the configuration files will be created automatically by the Cinchoo framework for each configuration object if it does not exist. In some cases, you may want to attach them to each configuration object at run time.

Using the Code

Two ways you can specify the configuration file path to a configuration object are:

  1. Declaratively
  2. Run Time

For a sample configuration object below, I've given the configuration file path as 'C:\Temp\NewApplicationSettings.xml' declaratively in 'ChoSingeTagConfigurationSection' attribute. This path can be either absolute or relative file path.

[ChoSingleTagConfigurationSection("applicationSettings", 
ConfigFilePath = @"C:\Temp\NewApplicationSettings.xml")]
public class ApplicationSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("path", DefaultValue = @"C:\")]
    public string Path;

    [ChoPropertyInfo("OS", DefaultValue = @"Apple,Windows")]
    public string OS;

    [ChoPropertyInfo("singleInstanceApp")]
    public bool SingleInstanceApp = false;

    protected override void OnAfterConfigurationObjectLoaded()
    {
        Console.WriteLine(ToString());
    }

    private static void OverrideMetaDataInfo(ChoBaseConfigurationMetaDataInfo configurationMetaDataInfo)
    {
        ChoStandardConfigurationMetaDataInfo standardConfigurationMetaDataInfo = 
	configurationMetaDataInfo as ChoStandardConfigurationMetaDataInfo;

        standardConfigurationMetaDataInfo.ConfigFilePath = "OverriddenApplicationSettings.xml";
    }
} 

In order to override the path at run-time, you must declare 'OverrideMetaDataInfo' method as above. It must be a static member of the configuration object taking 'ChoBaseConfigurationMetaDataInfo' object as parameter. At present, there are only two derived classes available. Those are 'ChoStandardConfigurationMetaDataInfo' and 'ChoCustomConfigurationMetaDataInfo'. First one is widely used in the framework. Cast the parameter to ChoStandardConfigurationMetaDataInfo and assign the new configuration file path (either relative or absolute path) to 'ConfigFilePath' member. This value takes precedence over declarative option. This way, you can control the file path of each configuration object.

Hope this helps. Thanks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Cinchoo
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130513.1 | Last Updated 22 Apr 2013
Article Copyright 2013 by Cinchoo
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid