Quantcast
Channel: Cinchoo » type-safe
Viewing all articles
Browse latest Browse all 10

Cinchoo – Configuration framework, part 17

$
0
0

Configuration Object Validation

This section is the continuation of previous articles. In here, I’ll go over seamless automatic configuration member validations,  another feature provided by Cinchoo configuration framework.

At present, Cinchoo framework supports the following family of Validation attributes

Look below the sample, where the Name property decorated with ChoStringValidator, It restricts the maximum length of the Name is 5.

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

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

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

	#endregion

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

During the configuration object initialization or when there is explicit assignment to Name member, if the length of value over 5, it will silently (default behavior) report the error in the log file as below. Framework will try to assign the value either DefaultValue or FallbackValue to Name.

Below code trying to assign Name value to ‘123456’

class Program
{
	static void Main(string[] args)
	{
		SampleConfigSection sampleConfigSection = new SampleConfigSection();
        sampleConfigSection.Name = "123456";

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

If you look at the configuration object log file (\\Logs\Settings\HelloWorld.SampleConfigSection.log), it reports the error


2012-03-29 04:53:08.0241140
  -- HelloWorld.SampleConfigSection State --
	Name: Mark
	Message: Hello World!

-- MetaData Information --
	BindingMode: TwoWay
	Defaultable: True
	Silent: True
	ConfigurationMetaDataLogInfo:
		-- Log Information --
			LogCondition: True
			LogTimeStampFormat:
			LogDirectory: Settings
			LogFileName: HelloWorld.SampleConfigSection.log

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

2012-03-29 04:53:08.0397395
* -- HelloWorld.SampleConfigSection State --
	Name: Mark
	Message: Hello World!

	-- Following errors produced while construction --
		Name: Failed to assign `123456` value. The string must be no more than 5 characters long.

-- MetaData Information --
	BindingMode: TwoWay
	Defaultable: True
	Silent: True
	ConfigurationMetaDataLogInfo:
		-- Log Information --
			LogCondition: True
			LogTimeStampFormat:
			LogDirectory: Settings
			LogFileName: HelloWorld.SampleConfigSection.log

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



Viewing all articles
Browse latest Browse all 10

Trending Articles