An application framework for .NET

Configuration

1. Introduction

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

In this section, I’ll run through with sample in defining and using User Defined Types (UDT) as configuration values. In general, all the configuration values are simple and intrinsic. Sometimes, we may want to manage read and save complex UDT data objects data.

2. Requirement

This configuration library is written in C# for the .NET 4.0 Framework. It is part of Cinchoo framework, which is a great library with lot of features like Configuration Management, common ApplicationHost, Shell features, etc.

3. “Hello World!” Sample

Let’s begin by looking into a simple example of an application consuming two configuration values name and address. name is required configuration value and address is a another required configuration value containing structured XML data as defined below.

The code above illustrates about defining configuration object. First thing defines a configuration (example, AppSettings) class from ChoConfigurableObject, it indicates that this object is a configuration object. And it must be decorated with one of the available configuration section attributes, will be discussed later. In this example, I’ve used ChoNameValueConfigurationSectionAttribute with ‘appSettings‘ as section name to complete the definition. You can use any configuration sections available in Cinchoo framework. It specifies that this configuration section is the NameValueConfigurationSection. With this definition, we turned the above configuration object provides name/value-pair configuration information from a configuration section.

Define configuration value members Name and Address either as public fields or properties with get and set in it. Decorate them with ChoPropertyInfo attribute to indicate that they are configuration members. In this sample, Name is the optional configuration member. It is given ‘name‘ as configuration member name with the DefaultValue as ‘Mark‘. It means that if the configuration value is missing from the source, it will be defaulted to DefaultValue.

Address configuration member is an optional member as well. It was given with ‘address‘ as configuration name, which will consume the payload in XML format from configuration file. It is of Address user defined type. Cinchoo framework makes it simplified the way of consuming/saving the structured Xml data into user defined type from configuration file. Below is the definition of Address UDT type.

The code above illustrates about defining Address UDT type. It must be derived from ChoEquatableObject, it indicates that this object will notify its changes to configuration framework. It defines three members (Street, City and State), with get and set accessors. Provides property change notifications via INotifyPropertyChanged pattern, which abstracted inside ChoEquatableObject. Once you have these change notification implemented for all properties, this object is eligible for sending all changes to the framework.

1. Introduction

Cinchoo is the application framework for .NET.One of the main functionality it provides to the users is the application configuration management. Application configuration is the information that 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 run through with sample in consuming xml data as configuration value. In general, all the configuration values are simple and intrinsic. Sometimes we may want to manage read and save xml data.

2. Requirement

This configuration library is written in C# for the .NET 4.0 Framework. It is part of Cinchoo framework, which is a great library with lot of features like Configuration Management, common ApplicationHost, Shell features, etc.

3. “Hello World!” Sample

Let’s begin by looking into a simple example of an application consuming two configuration values name and address. name is required configuration value and address is a another required configuration value containing structured xml data as below

The code above illustrates about defining configuration object. First thing define a configuration (ex. AppSettings) class from ChoConfigurableObject, it indicates that this object is a configuration object. And it must be decorated with one of the available configuration section attributes, will be discussed later. In this example, I’ve used ChoNameValueConfigurationSectionAttribute with ‘appSettings‘ as section name to complete the definition. You can use any available configuration sections except ChoSingleTagConfigurationSection, which does not support complex configuration values. It specifies that this configuration section is the NameValueConfigurationSection. With this definition, we turned the above configuration object provides name/value-pair configuration information from a configuration section. Define configuration value members Name and Address either as public fields or properties with get and set in it. Decorate them with ChoPropertyInfo attribute to indicate that they are configuration members. In this sample, Name is the optional configuration member. It is given ‘name‘ as configuration member name with the DefaultValue as ‘Mark‘. It means that if the configuration value is missing from the source, it will be defaulted to DefaultValue. Address configuration member is an optional member as well. It was given with ‘address‘ as name, which will consume the payload in xml format from configuration file. Once you have the configuration object defined as above, it is now ready to use in your application. Here is how you can create and use it.

We start by creating a new instance of AppSettings object, That’s all. All the heavy lifting of loading configuration values to the object is done by the framework under the hood. Now compile this program and run. It will create the section in [appExeName].xml (HelloWorld.exe.xml) file under bin/Config folder.

Cinchoo framework automatically checks and identifies the payload of each configuration values and consume them safely. It is very simple to work with configuration using Cinchoo framework. Try for yourself. Thanks.

By default, Cinchoo framework creates and uses App.exe.Config and other configuration files under the executable folder. In a protected environment, where the ‘program files’ folder is protected and no write access is available, you will need to instruct the framework to use ‘AppData’ folder or any other write accessable folder for all the configuration management. This can be done as below

Step 1. In the Main Method, subscribe to ChoApplication.AfterConfigurationManagerInitialized event

1. Introduction

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

In this section, I’ll show you with sample reading/writing AppSettings using the Cinchoo configuration framework. It gives the opportunity loading and using application settings values in your .NET applications.

Let’s start this exercise using the below appSettings values (FYI, this section is not mandatory to present in App.Config when using Cinchoo framework. It will be created automatically very first time the application starts.)

The code above illustrates about defining configuration object. First thing define a configuration object (ex. ApplicationSettings) class from ChoConfigurableObject, it indicates that this object is a configuration object. And it must be decorated with ChoStandardAppSettingsConfigurationSectionAttribute to complete the definition. This attribute enables the configuration object to read and write configuration values under ‘appSettings’ section in App.Config file.

Define three members Name, Address and InvoiceAmount as public fields or properties with get and set in it. Decorate them with ChoPropertyInfo attribute to indicate that they are configuration members. In this sample, Name property is given name as property name with defaultvalue as ‘Mark’. It means that this member will be defaulted to default value when the value is missing in the configuration file. Respectively declare other members for each appSettings key. It is very simple and clear way of specifying configuration members using attributes.

Once you have class declared as above, it is now ready consume appSettings values as simple as creating object out of it. Any changes made to this object and/or in the app.Config file will be exchanged automatically. The sample code as below

Now let’s see how the application picks up the changes made to the [AppExeName].Config file reflected in the output. Open [AppExeName].config file, edit the name from ‘Mark’ to ‘Tom’ and save the file.

Listing 2.4 Output of Test1.exe when changing the values in the config file

2. Advanced Settings

2.1 Override ConfigurationManager

Cinchoo automatically instantiates and uses appropriate ConfigurationManager depending on the type of the application. Your application works seamlessly out of the box to read and write appSettings. Rarely, you may want to override this behaviour to take control of using different ConfigurationManager. You can do so as below:

2.2 External AppSettings file

Many of you are aware that you can specify external file containing application settings. Cinchoo framework works with this external file as well. Any filepath specified as appSettings external filepath will be automatically created if not present, watch for any changes and save the changes to them, etc. The filepath can be either relative or absolute path. The path specified is relative to the main executable location. For a Windows Forms application, this would be the binary folder (such as /bin/debug), not the location of the application configuration file. For Web Forms applications, the path is relative to the application root, where the web.config file is located. Note that the runtime ignores the attribute if the specified file cannot be found.

Disable logging

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.

ChoSwitch is a TraceSwitch, you can assign different levels to it to specify the types of trace or debug messages the application outputs. Setting the value to “0” will disable all the logging of the framework.

Other possible values can be assigned to control the output of the logs

Consume configuration values from any database.

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.

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.

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

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.

PROVIDER_NAMESPACE (string) – Required. Namespace of the ADO.NET provider (Ex. for Sqlite, it is System.Data.Sqlite)

KEY_COLUMN_NAME (string) – Optional. Default is ‘KEY’. If you have different key column name, please specify it here.

VALUE_COLUMN_NAME (string) – Optional. Default is ‘VALUE’. If you have different value column name, please specify it here.

LAST_UPDATE_DATETIME_KEY_NAME (string) – Optional. Default is Empty string. In such case, this provider checks the configuration values for any changes in the underlying table. If you specify a valid key name, it must carry the value as timestamp. To notify the values changes by touching this value to last updated time stamp.

PARAM_PREFIX_CHAR (char) – Optional. Default is ‘@’.

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

Support of Complex Object as configuration value

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.

In this section, I’ll detail about new feature, using and saving custom user defined object as configuration values. So far Cinchoo framework supports only scalar text values. It limits the configuration object member values from having any complex objects.

In standard .NET framework, it is not supported in straight way. You will have to create custom configuration section and put a plumbing code to parse the values. In Cinchoo framework, it is simplified to handle the complex objects as configuration values.

Here is how you can define configuration members to support custom user defined type

Support of CDATA / Xml as configuration value

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.

In this section, I’m going to detail about new feature, using and saving CDATA / Xml as configuration values. So far Cinchoo supports the configuration object member values as simple text values. It limits the configuration object member values from having any complex text.

In standard .NET framework, it is not supported in straight way. You will have to create custom configuration section and put a plumbing code to parse the values. In Cinchoo, it is simplified to handle CDATA/Xml values.

Here is how you can define configuration members to support CDATA / Xml values

In the above sample configuration object, Address member defined as ChoCDATA type to accommodate CDATA value. Employer member is defined as string type, to hold the xml value. Cinchoo automatically checks the configuration member value for any xml tags present, if it does then the section value will be created as Xml element, otherwise it will be created as Xml attribute.

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

Since Address is CDATA member, any modification must be done by creating new ChoCDATA object with new value and assign to it. And for the Employer member, it is straight forward to modify. Cinchoo framework will take care of it under the hood.

If you compile and run this sample, the output of the configuration will be seen as below

Cinchoo is the application framework for .NET. One of the main functionality it provides to the users is application configuration management. Application configuration is the information that 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 talk about a way to hook up the existing configuration files to your files through application configuration file. And how to set environment specific configuration files to the applications.

For more up to date information about this article, please visit the below CodeProject link

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.

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