Category Archives: ConfigurationSectionHandler

Cinchoo – Configuration Framework, Part 30

Consume configuration values from any database.

Download Sample (Zip)

Cinchoo is an application framework for .NET. One of the main functionality it provides to users is application configuration management. Application configuration is the information that an application reads and/or writes at run-time from the source.

Please visit jump start article [Cinchoo - Simplified Configuration Manager] for more information about Cinchoo configuration manager.

In this section, I’ll detail about new configuration section handler, DbGenericeKeyValueConfigurationSection. It is generic database configuration section handler to read and store configuration data in any database. It eliminates creating new configuration section handler for each targeted database. All you need ADO.NET driver for the corresponding database to work with this handler. This section handler sufficient enough to interact with any database to read/store configuration data. Only downside of this handler is that all the values are converted to string before storing in the database.

Here are steps to work with this section handler

  • Define a table similar to the below schema. The table name can be given anything. This configuration handler needs a table with 2 columns. First column (KEY) holds the configuration parameter names. Must be VARCHAR(50), where length must be minimum of 50. Second column (VALUE) holds the configuration parameter values. It can be size of any. By default, this handler looks for KEY, VALUE as columns. The names of these columns can be given any. In this case, it must be passed as parameter to the configuration object. Will discuss this later about passing these parameters to the configuration object.
CREATE TABLE TEST_CONFIG
(
    [KEY] VARCHAR (50) NOT NULL,
    VALUE VARCHAR (100),
    PRIMARY KEY ([KEY])
)
  • Define configuration object targeted for Sqlite database as below
[ChoDbGenericKeyValueConfigurationSection("dbApplicationSettings",
@"CONNECTION_STRING='Data Source=.\SqliteTest.db;Version=3;Pooling=True;Max Pool Size=100;';
TABLE_NAME=TEST_CONFIG;
PROVIDER_ASSEMBLY_FILE_PATH=C:\Program Files\System.Data.SQLite\2010\bin\System.Data.SQLite.dll;
PROVIDER_NAMESPACE=System.Data.SQLite")]
public class DbApplicationSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("name", DefaultValue = "Mark")]
    public string Name;

    [ChoPropertyInfo("address", _
    DefaultValue = "10 River Road, Edison NJ 08820")]
    public string Address;

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

This configuration object must be decorated with ChoDbGenericKeyValueConfigurationSectionAttribute.

First parameter is the configElementPath, a xpath to the configuration section in the [appName].exe.xml file.

Second parameter is the configObjectAdapterParams, parameter list in key-value pairs format separated by ‘;’ character. (PS. If any value contains ‘;’ character, the whole value must be surrounded by single quote in order to escape it.) Below are the list of applicable parameters needed by this section handler

  1. CONNECTION_STRING (string) - Required. ADO.NET connection string to the database.
  2. TABLE_NAME (string) - Required. Configuration table name.
  3. PROVIDER_ASSEMBLY_FILE_PATH (string) – Optional. Only specify if the ADO.NET assembly is not referenced in the project. File path to the ADO.NET assembly if not in GAC. Otherwise, fully qualified assembly name.
  4. PROVIDER_NAMESPACE (string) – Required. Namespace of the ADO.NET provider (Ex. for Sqlite, it is System.Data.Sqlite)
  5. KEY_COLUMN_NAME (string) – Optional. Default is ‘KEY’. If you have different key column name, please specify it here.
  6. VALUE_COLUMN_NAME (string) – Optional. Default is ‘VALUE’. If you have different value column name, please specify it here.
  7. LAST_UPDATE_DATETIME_KEY_NAME (string) – Optiona. Default is LAST_UPDATE_DATETIME. You can override to different name if you want.
  8. PARAM_PREFIX_CHAR (char) – Optional. Default is ‘@’.
  9. TRUNCATE_VALUE_DATA (bool) – Optional. Default is True.  It will truncate the value if it is over the Value column size.

The above two parameters can be hard-coded in the code and also be modifiable through configuration. Here is how you can modify them through configuration. Open the [AppName].exe.xml file under Config folder

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="dbApplicationSettings" type="Cinchoo.Core.Configuration.ChoDictionarySectionHandler, Cinchoo.Core" />
  </configSections>
  <dbApplicationSettings cinchoo:configObjectAdapterType="Cinchoo.Core.Configuration.ChoDbGenericKeyValueConfigStorage, Cinchoo.Core" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework">
    <cinchoo:configObjectAdapterParams xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework"><![CDATA[CONNECTION_STRING='Data Source=.\SqliteTest.db;Version=3;Pooling=True;Max Pool Size=100;';TABLE_NAME=TEST_CONFIG;PROVIDER_ASSEMBLY_FILE_PATH=C:\Program Files\System.Data.SQLite\2010\bin\System.Data.SQLite.dll;PROVIDER_NAMESPACE=System.Data.SQLite]]></cinchoo:configObjectAdapterParams>
  </dbApplicationSettings>
</configuration>

Now lets try to create and consume the above configuration object. Below code shows how to consume and modify the object members

static void Main(string[] args)
{
    DbApplicationSettings DbApplicationSettings = new DbApplicationSettings();

    ChoConsole.PauseLine();
}

Compile and run this sample to see the output stored in the database.

That’s it folks, it is as easy as to store and consume Configuration values in any databases.


Cinchoo – Using a SQL Server database as a configuration source, Part 2

Cinchoo is an application framework for .NET. One of the main functionality it provides to users is application configuration management. Application configuration is information that an application reads and/or writes at run-time from the source.

Please visit jump start article [Cinchoo - Simplified Configuration Manager] for more information about Cinchoo configuration manager.

This is the second part of [Cinchoo - Using SQLServer database as configuration source] article. So far we learned about using SQL Server database as configuration source in a polling approach. Cinchoo configuration manager polls for the change in the underlying table for every elapsed interval (configurable). This approach is ideal in a situation where SQL Server query notification service is not available or is turned off by administrators.

In this section, I’m going to detail you about using SQL Server database as configuration source for your applications. More commonly all the .NET application configuration are kept in files as XML format. It has the limitation, such as maintaining them for each application separately, potential disk corruption by many applications due to access etc. This may not fit in enterprise world.  In a enterprise application development, there may be requirement to centralize these configuration parameters in database to better serve, manage and control them. Cinchoo framework opens the possibility of extending the configuration source to various medium. A SQL Server database is one of the mediums to store the application configurations.

In this approach, we are going to leverage SQL Server query notification service for change notification. It is available in SQL Server 2005+/ADO.NET 2.0.

For more details on this configuration source, please visit the code project article below

Cinchoo – Using a SQL Server database as a configuration source, Part 2


Cinchoo – Using SQLServer database as configuration source

Cinchoo is an application framework for .NET. One of the main functionality it provides to users is application configuration management. Application configuration is information that an application reads and/or writes at run-time from the source.

Please visit jump start article [Cinchoo - Simplified Configuration Manager] for more information about Cinchoo configuration manager.

In this section, I’m going to detail you about using SQLServer database as configuration source for your applications. More commonly all the .NET application configuration are kept in files as XML format. It has the limitation, such as maintaining them for each application separately, potential disk corruption by many applications due to access etc. This may not fit in enterprise world.  In a enterprise application development, there may be requirement to centralize these configuration parameters in database to better serve, manage and control them. Cinchoo framework opens the possibility of extending the configuration source to various medium. A SQL Server database is one of the mediums to store the application configurations.

For more details on this configuration source, please visit the code project article below

Cinchoo – Using SQLServer database as configuration source


Cinchoo – Configuration framework, part 26

Using ChoFireBirdADODictionaryConfigStorage

UPDATE:

This article is outdated. Please visit the below codeproject article for more updated information on this storage plug-ins

Cinchoo – Using FireBird database as configuration source

Using FireBird database as configuration store using Cinchoo framework made easy with this storage plug-in. Using ChoFireBirdADODictionaryConfigStorage, you can read and store application configuration information easily. It is based on ChoDictionaryAdapterConfigurationSection pattern and uses ADO.NET as the database access medium. You can take this one as an example and implement various plug-ins targeting various other databases. Here is how you can use this storage

  • Download and install FireBird Db from here.
  • Create a table using the below Sql. I designed this table in such a way you can use them for multiple applications. Thats why I set the ‘AppName’ as primary key.
CREATE TABLE application_settings
(
 Path varchar(255) DEFAULT NULL,
 OS varchar(50) DEFAULT NULL,
 SingleInstanceApp int DEFAULT NULL,
 LastUpdateTimeStamp timestamp NOT NULL,
 AppName varchar(255) NOT NULL,
 PRIMARY KEY (AppName)
);
  • Create a VS project and add reference to ChoFireBirdADODictionaryConfigStorage.dll
  • Define the configuration section object decorated with ChoDictionaryAdapterConfigurationSectionAttribute as below.
[ChoDictionaryAdapterConfigurationSection("fireBirdDictionarySectionHandlerTest/applicationSettings",
    typeof(ChoFireBirdADODictionaryConfigObjectAdapter),
    "CONNECTION_STRING='User=SYSDBA;Password=masterkey;Database=C:\Users\raj\AppData\Local\Temp\Sample.fdb;DataSource=localhost';
    TABLE_NAME=APPLICATION_SETTINGS;LAST_UPDATE_DATETIME_COLUMN_NAME=LastUpdateTimeStamp;KEY_COLUMN_NAME=AppName")]
public class ApplicationSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("path", DefaultValue = "C:\")]
    public string Path;

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

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

2. Now instantiate and use it as below

static void Main(string[] args)
{
    ApplicationSettings applicationSettings = new ApplicationSettings();

    ChoConsole.Pause();

    applicationSettings.OS = "Microsoft";

    ChoConsole.Pause();
}

The configuration section will be generated automatically for the first time in [appExeName].xml as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="fireBirdDictionarySectionHandlerTest">
      <section name="applicationSettings" type="Cinchoo.Core.Configuration.ChoDictionarySectionHandler, Cinchoo.Core, Version=1.0.1.1, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
    </sectionGroup>
  </configSections>
  <fireBirdDictionarySectionHandlerTest>
    <applicationSettings cinchoo:configObjectAdapterType="Cinchoo.Core.Configuration.ChoFireBirdADODictionaryConfigObjectAdapter, ChoFireBirdADODictionarySectionHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework">
      <cinchoo:configObjectAdapterParams xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework"><![CDATA[CONNECTION_STRING='User=SYSDBA;Password=masterkey;Database=C:\Users\raj\AppData\Local\Temp\Sample.fdb;DataSource=localhost';
        TABLE_NAME=APPLICATION_SETTINGS;LAST_UPDATE_DATETIME_COLUMN_NAME=LastUpdateTimeStamp;KEY_COLUMN_NAME=AppName]]></cinchoo:configObjectAdapterParams>
    </applicationSettings>
  </fireBirdDictionarySectionHandlerTest>
</configuration>

Cinchoo – Configuration framework, part 25

Using ChoMySqlADODictionaryConfigStorage

UPDATE:

This article is outdated. Please visit the below codeproject article for more updated information on this storage plug-ins

Cinchoo – Using MySql database as configuration source

Using MySql database as configuration store using Cinchoo framework made easy with this storage plug-in. Using ChoMySqlADODictionaryConfigStorage, you can read and store application configuration information easily. It is based on ChoDictionaryAdapterConfigurationSection pattern and uses ADO.NET as the database access medium. You can take this one as an example and implement various plug-ins targeting various other databases. Here is how you can use this storage

  • Download and install MySql from here.
  • Create a table using the below Sql. I designed this table in such a way you can use them for multiple applications. Thats why I set the ‘AppName’ as primary key.

CREATE TABLE `application_settings` (
 `Path` varchar(255) DEFAULT NULL,
 `OS` varchar(50) DEFAULT NULL,
 `SingleInstanceApp` bit(1) DEFAULT NULL,
 `LastUpdateTimeStamp` datetime NOT NULL,
 `AppName` varchar(255) NOT NULL,
 PRIMARY KEY (`AppName`)
)

  • Create a VS project and add reference to ChoMySqlADODictionaryConfigStorage.dll
  • Define the configuration section object decorated with ChoDictionaryAdapterConfigurationSectionAttribute as below.
[ChoDictionaryAdapterConfigurationSection("mySqlDictionarySectionHandlerTest/applicationSettings",
    typeof(ChoMySqlADODictionaryConfigObjectAdapter),
    @"CONNECTION_STRING='server=localhost;User Id=root;password=admin;Persist Security Info=True;database=test';
    TABLE_NAME=APPLICATION_SETTINGS;LAST_UPDATE_DATETIME_COLUMN_NAME=LastUpdateTimeStamp;KEY_COLUMN_NAME=AppName")]
public class ApplicationSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("path", DefaultValue = @"C:\")]
    public string Path;

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

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

2. Now instantiate and use it as below

static void Main(string[] args)
{
    ApplicationSettings applicationSettings = new ApplicationSettings();

    ChoConsole.Pause();

    applicationSettings.OS = "Microsoft";

    ChoConsole.Pause();
}

The configuration section will be generated automatically for the first time in [appExeName].xml as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="mySqlDictionarySectionHandlerTest">
      <section name="applicationSettings" type="Cinchoo.Core.Configuration.ChoDictionarySectionHandler, Cinchoo.Core, Version=1.0.1.1, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
    </sectionGroup>
  </configSections>
  <mySqlDictionarySectionHandlerTest>
    <applicationSettings cinchoo:configObjectAdapterType="Cinchoo.Core.Configuration.ChoMySqlADODictionaryConfigObjectAdapter, ChoMySqlADODictionaryConfigStorage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework">
      <cinchoo:configObjectAdapterParams xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework"><![CDATA[CONNECTION_STRING='server=localhost;User Id=root;password=admin;Persist Security Info=True;database=test';
        TABLE_NAME=APPLICATION_SETTINGS;LAST_UPDATE_DATETIME_COLUMN_NAME=LastUpdateTimeStamp;KEY_COLUMN_NAME=AppName]]></cinchoo:configObjectAdapterParams>
    </applicationSettings>
  </mySqlDictionarySectionHandlerTest>
</configuration>

Cinchoo – Configuration framework, part 19

Using ChoFireBirdConfigStorage

UPDATE:

This article is outdated. Please visit the below codeproject article for more updated information on this storage plug-ins

Cinchoo – Using FireBird database as configuration source

 

In this section, I’ll go over the details of using ChoFireBirdConfigStorage in your application. It provides name/value-pair configuration information from FireBird Database.  ChoFireBirdConfigurationSectionAttribute is the corresponding attribute used to decorate the configuration object class.

1. Define the configuration section object decorated with ChoFireBirdConfigurationSectionAttribute as below. You must provide ConnectionString and TableName where the configuration data stored. The table will be created if not exists. The table will have two columns (CONFIG_DATA varchar(250), LAST_UPDATTED_TIME TimeStamp). ConnectionString and TableName can be overridden through configuration file.

[ChoFireBirdConfigurationSection("sample", @"User=SYSDBA;Password=masterkey;Database=C:\Sample.fdb;DataSource=localhost; Port=3050;Dialect=3; Charset=NONE;",
    "APP_SETTINGS")]
public class SampleConfigSection : ChoConfigurableObject
{
    #region Instance Data Members (Public)

    [ChoPropertyInfo("name", DefaultValue = "Mark")]
    public string Name;

    [ChoPropertyInfo("message", DefaultValue = "Hello World!")]
    public string Message;

    #endregion

    [ChoAfterConfigurationObjectLoadedHandler]
    void OnAfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
    {
        Console.WriteLine(sender.ToString());
    }
}

2. Now instantiate and use it as below

class Program
{
	static void Main(string[] args)
	{
		SampleConfigSection sampleConfigSection = new SampleConfigSection();
		Console.WriteLine(sampleConfigSection.ToString());

		//Shutdown the framework to stop the background services...otherwise the application will not terminate
		ChoFramework.Shutdown();
	}
}

The configuration section will be generated automatically for the first time in HelloWorld.exe.xml as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="sample" type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
  </configSections>
  <sample cinchoo:connectionString="User=SYSDBA;Password=masterkey;Database=C:\Sample.fdb;DataSource=localhost; Port=3050;Dialect=3; Charset=NONE;" cinchoo:tableName="APP_SETTINGS" cinchoo:configDataColumnSize="250" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
</configuration>
The corresponding meta-data looks as below
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <sample bindingMode="TwoWay" defaultable="true" silent="true">
    <configStorage>Cinchoo.Core.Configuration.ChoFireBirdConfigStorage, Cinchoo.Core.Configuration.Storage.FireBirdConfigStorage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</configStorage>
    <logInfo condition="true" directory="Settings" fileName="Cinchoo.Core.Configuration.ChoFireBirdClientSectionHandler.Test.SampleConfigSection.log" />
  </sample>
</configuration>

Cinchoo – Configuration framework, part 18

Using ChoSqlServerConfigStorage

UPDATE:

This article is outdated. Please visit the below codeproject article for more updated information on this storage plug-ins

Cinchoo – Using SQLServer database as configuration source

 

 

In this section, I’ll go over the details of using ChoSqlServerConfigStorage in your application. It provides name/value-pair configuration information from SqlServer Database.  ChoSqlServerConfigurationSectionAttribute is the corresponding attribute used to decorate the configuration object class.

1. Define the configuration section object decorated with ChoSqlServerConfigurationSectionAttribute as below. You must provide ConnectionString and TableName where the configuration data stored. The table will be created if not exists. The table will have two columns (CONFIG_DATA varchar(250), LAST_UPDATTED_TIME Timestamp). ConnectionString and TableName can be overridden through configuration file.

[ChoSqlServerConfigurationSection("sample", @"Data Source=shreeya-pc\SQLExpress;Initial Catalog=Cinchoo;Integrated Security=True", "APP_SETTINGS")]
public class SampleConfigSection : ChoConfigurableObject
{
    #region Instance Data Members (Public)

    [ChoPropertyInfo("name", DefaultValue = "Mark")]
    public string Name;

    [ChoPropertyInfo("message", DefaultValue = "Hello World!")]
    public string Message;

    #endregion

    [ChoAfterConfigurationObjectLoadedHandler]
    void OnAfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
    {
        Console.WriteLine(sender.ToString());
    }
}

2. Now instantiate and use it as below

class Program
{
	static void Main(string[] args)
	{
		SampleConfigSection sampleConfigSection = new SampleConfigSection();
		Console.WriteLine(sampleConfigSection.ToString());

		//Shutdown the framework to stop the background services...otherwise the application will not terminate
		ChoFramework.Shutdown();
	}
}

The configuration section will be generated automatically for the first time in HelloWorld.exe.xml as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <sample cinchoo:connectionString="Data Source=your-pc\SQLExpress;Initial Catalog=Cinchoo;Integrated Security=True" cinchoo:tableName="APP_SETTINGS" cinchoo:configDataColumnSize="250" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
  <configSections>
    <section name="sample" type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
  </configSections>
</configuration>
The corresponding meta-data looks as below
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <sample bindingMode="TwoWay" defaultable="true" silent="true">
    <configStorage>Cinchoo.Core.Configuration.ChoSqlServerConfigStorage, Cinchoo.Core.Configuration.Storage.ChoSqlServerNameValueConfigStorage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</configStorage>
    <logInfo condition="true" directory="Settings" fileName="ChoSqlServerConfigStorageDemo.SampleConfigSection.log" />
  </sample>
</configuration>

Cinchoo – Configuration framework, part 15

Available Configuration Section Handlers

Here is the list of available configuration section handlers in Cinchoo framework. This list will keep changing based on the more support to other data sources. Keep looking for new configuration section handlers.

  • ChoNameValueSectionHandler (Default)

Cinchoo – Configuration framework, part 14

Configuration Objects Logs

Cinchoo framework creates a  log file for each configuration object separately. Those log files are created under application binary folder. By default the log file name are generated from configuration object type full name.

For a sample configuration object below

namespace HelloWorld
{
	#region NameSpaces

	using System;
	using Cinchoo.Core.Configuration;

	#endregion NameSpaces

	[ChoConfigurationSection("sample")]
	public class SampleConfigSection : ChoConfigurableObject
	{
		#region Instance Data Members (Public)

		[ChoPropertyInfo("name", DefaultValue="Mark")]
		public string Name;

		[ChoPropertyInfo("message", DefaultValue="Hello World!")]
		public string Message;

		#endregion
	}
}

The log file will be created in the name of HelloWorld.SampleConfigSection.log under Application Bin/Settings folder.

2012-03-19 05:18:47.7648110
  -- HelloWorld.SampleConfigSection State --
	Name: Mark
	Message: Hello World!

-- MetaData Information --
	BindingMode: TwoWay
	Defaultable: True
	IgnoreError: True
	ConfigurationMetaDataLogInfo:
		-- Log Information --
			Condition: True
			TimeStampFormat:
			TraceOutputDirectory: Settings
			TraceOutputFileName: HelloWorld.SampleConfigSection.log

	ConfigStorageType: Cinchoo.Core.Configuration.Storage.ChoFileNameValueConfigStorage, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de

Application Log Folder can be overridden in both ways

  • Globally at the application level
  • Locally at each configuration object level

1. Overriding Global Log Folder

The application log folder can be overridden. Please visit Framework Configuration Parameters section on how to do it.

2. Overriding at individual configuration at object level

In some cases, you may want to redirect the log files of individual configuration object to some specific folder. In that moment, it can achieved as below.

namespace HelloWorld
{
	#region NameSpaces

	using System;
	using Cinchoo.Core.Configuration;

	#endregion NameSpaces

	[ChoConfigurationSection("sample", TraceOutputDirectory = @"C:\CustomLogsFolder")]
	public class SampleConfigSection : ChoConfigurableObject
	{
		#region Instance Data Members (Public)

		[ChoPropertyInfo("name", DefaultValue="Mark")]
		public string Name;

		[ChoPropertyInfo("message", DefaultValue="Hello World!")]
		public string Message;

		#endregion
	}
}

In this sample, we can route the log files of Configuration Object to C:/CustomLogsFolder directory using TraceOutputDirectory attribute member as below.

You can even override the individual configuration object log file through TraceOutputFileName attribute member as below

namespace HelloWorld
{
	#region NameSpaces

	using System;
	using Cinchoo.Core.Configuration;

	#endregion NameSpaces

	[ChoConfigurationSection("sample", TraceOutputDirectory = @"C:\CustomLogsFolder", TraceOutputFileName = "OverriddenSampleConfigObject.log")]
	public class SampleConfigSection : ChoConfigurableObject
	{
		#region Instance Data Members (Public)

		[ChoPropertyInfo("name", DefaultValue="Mark")]
		public string Name;

		[ChoPropertyInfo("message", DefaultValue="Hello World!")]
		public string Message;

		#endregion
	}
}

Cinchoo – Configuration framework, part 13

Managing Large Configuration file

Download sample source files (Require .NET 4.0 / Visual Studio 2010)

Suppose in a large application built on several components packaged in their own assemblies, managing configuration file in one file will be tedious. Using Cinchoo configuration framework, you can split them into manageable small files. I’ll walk you over the steps to split them and use them in a application.

There are couple of ways, you can achieve the splitting of configuration files

1. Automatic Split (Code first approach)

This approach is viable in case of new development where there are no existing configuration files available.  In this scenario, you just design and implement the configuration objects first and let the Cinchoo framework generate the configuration files for you. I’ll walk you over on how to do it using this approach.

You can specify the individual configuration file for each configuration object using ChoConfigurationSectionAttribute, where you must give the name of the configuration file in one of the property

  • ConfigFilePath - Configuration file path in string either full path or just file name.
  • ConfigFilePathFromTypeName – For a given type specified to this property, it takes the name of the type as configuration file name.
  • ConfigFilePathFromTypeFullName - For a given type specified to this property, it takes the fully qualified namespace name of the type as configuration file name.

For a sample console application named SplitConfigUsingCodeFirstApproach.exe, defining the below two configuration objects

[ChoNameValueConfigurationSection("serverDetails/environments", ConfigFilePathFromTypeName = typeof(ServerEnvironments))]
public class ServerEnvironments : ChoConfigurableObject
{
    public string Host = "WNYC1000234011";
    public string IP = "205.12.5.122";
    public int Port = 2001;
}

[ChoNameValueConfigurationSection("applicationSettings", ConfigFilePath = "ApplicationSettings" )]
public class ApplicationSettings : ChoConfigurableObject
{
    public string Mode = "LIVE";
    public bool IsConnected = true;
    public string Exchange = "NYSE";
}

Just instantiate the above objects in the Main entry method as below, Cinchoo framework creates the configuration files automatically.

class Program
{
    static void Main(string[] args)
    {
        ServerEnvironments serverEnvironments = new ServerEnvironments();
        ApplicationSettings applicationSettings = new ApplicationSettings();

        ChoFramework.Shutdown();
    }
}

The main application configuration file (SplitConfigUsingCodeFirstApproach.exe.xml)  is generated as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <applicationSettings cinchoo:path="C:\CinchooConfigurationFrxDemos\SplitConfigUsingCodeFirstApproach\SplitConfigUsingCodeFirstApproach\bin\Debug\ApplicationSettings.xml" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
  <serverDetails>
    <environments cinchoo:path="C:\CinchooConfigurationFrxDemos\SplitConfigUsingCodeFirstApproach\SplitConfigUsingCodeFirstApproach\bin\Debug\ServerEnvironments.xml" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
  </serverDetails>
</configuration>

And the splitted configuration files corresponding to two configuration objects defined in the code are generated automatically by the framework.

For the ServerEnvironments object, the file named ServerEnvironments.xml is generated as below

<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="serverDetails">
      <section name="environments" type="Cinchoo.Core.Configuration.Handlers.ChoNameValueSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
    </sectionGroup>
  </configSections>
  <serverDetails>
    <environments>
      <add key="Host" value="WNYC1000234011" />
      <add key="IP" value="205.12.5.122" />
      <add key="Port" value="2001" />
    </environments>
  </serverDetails>
</configuration>

For the ApplicationSettings object, the file named ApplicationSettings.xml is generated as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="applicationSettings" type="Cinchoo.Core.Configuration.Handlers.ChoNameValueSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
  </configSections>
  <applicationSettings>
    <add key="Mode" value="LIVE" />
    <add key="IsConnected" value="True" />
    <add key="Exchange" value="NYSE" />
  </applicationSettings>
</configuration>

For any reason, the configuration files needs to be re-created, all you have to do is delete the configuration files or removing the configuration sections from the configuration file will trigger the Cinchoo framework to regenerate them seamlessly.

2. Manual Step (Configuration model first approach)

This approach is suitable if you already have established configuration file, you can use this method to split them.

Lets say, you have application configuration file named HelloWorld.exe.xml 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="serverDetails" type="Cinchoo.Core.Configuration.Handlers.ChoNameValueSectionHandler, Cinchoo.Core" />
        <sectionGroup name="allSections">
            <section name="sample" type="Cinchoo.Core.Configuration.Handlers.ChoXmlSerializerSectionHandler, Cinchoo.Core" />
        </sectionGroup>
    </configSections>
    <serverDetails Name="Mark">
        <Message>Hello World!</Message>
    </serverDetails>
    <allSections>
        <sample Name="Mark">
            <Message>Hello World!</Message>
        </sample>
    </allSections>
</configuration>

Above configuration file contains two sections ‘serverDetails’ and ‘allSections/sample’. I’m going to split this file into two files and show you how to link them together in the main configuration file.

The main configuration file HelloWorld.exe.xml will look as below after the split

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <serverDetails cinchoo:path="ServerDetails.xml" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
    <allSections>
        <sample cinchoo:path="Sample.xml" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
    </allSections>
</configuration>

The splitted configuration files ServerDetails.xml and Sample.xml files will look below

ServerDetails.xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="serverDetails" type="Cinchoo.Core.Configuration.Handlers.ChoXmlSerializerSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
    </configSections>
    <serverDetails Name="Mark">
        <Message>Hello World!</Message>
    </serverDetails>
</configuration>

Sample.xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="allSections">
            <section name="sample" type="Cinchoo.Core.Configuration.Handlers.ChoXmlSerializerSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
        </sectionGroup>
    </configSections>
    <allSections>
        <sample Name="Mark">
            <Message>Hello World!</Message>
        </sample>
    </allSections>
</configuration>

Follow

Get every new post delivered to your Inbox.