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:
- Declaratively
- 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.