Introduction
With custom configuration sections, validation is available to ensure that the values provided are correct values. There are a few built-in validators available in the System.Configuration namespace. Take, for example, the StringValidator validation class can limit the size of string values between a minimum and maximum length, as well as providing a list of string characters that are invalid. There are other validators, such as the TimeSpanValidator and the RegexStringValidator classes, all of which have a purpose in validating configuration data. But what if you have a need for custom validation?
By inheriting from ConfigurationValidationBase, you can implement your own validation. These validations can be used to perform whatever you desire.
Custom Validation
Below is my implementation of an AuthorNameValidator; it limits the size of the length of the author's name field:
This is a simple example of a validator class; it validates an author's name to ensure that it meets the right format. Remember that an author's name could be "Mr. Brian G. Mains Sr." as a maximum length; by splitting on the ' ', I break the name up into parts. If the name is locked down, not allowing initials, prefixes, and suffixes, only "Brian Mains" is allowed. The array count would be two, and if I have three parts there, an error is thrown. In addition, if the AllowPrefix is set to true, three parts are allowed, and so if I have a value of "Mr. Brian G. Mains", an error is thrown because the array is 4, however "Mr. Brian Mains" is acceptable.
CanValidate determines whether the value entered is of the correct type. Validate actually performs the validation. In this case, it is calculating the maximum length a string can be based on the boolean properties in the section. You define whatever properties you need for your class and expose them directly through the constructor.
Validator Attributes
These validators are linked through an attribute defined in the class; how does this class link correctly? A second class actually exposes this validator as an attribute. This class would be defined as AuthorNameValidatorAttribute. The AllowMiddleInitial, AllowPrefix, and AllowSuffix properties all would be exposed through this attribute, so they can be set in the attribute declaration. The following is an attribute declaration for a configuration file:
So, going back to the AuthorSection, we have a new definition for Name, which is the author name:
So, this only allows first name and last name. Obviously, this isn't perfect; it really doesn't validate the values. But it shows you how you can limit or validate the values in your configuration file.
Summary
Custom validation is available within configuration files. By doing this, you can ensure that the information provided in the configuration file is correct. You can perform whatever custom validation that you like, to ensure that the information is correct and according to the standards of the custom configuration section. Change the values in the string, separated by spaces, and test the validation capabilities.
References
This MSDN article shows how the validator is defined.
About Brian Mains
 |
Brian Mains is an application developer consultant with Computer Aid Inc. He formerly worked with the Department of Public Welfare.
In both places of business, he developed both windows and web applications, small and large, using the latest .NET technologies. In addition, he had spent many hou...
This author has published 49 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
Combined SSL HttpModule for SharePoint Webs and Pages
read more
Using NHibernate with multiple databases
read more
UppercuT - TeamCity Integration - Part 2 (Create a Build Configuration)
read more
Section 508 Compliant Renamed Accessibility Compliant
read more
Separating configuration from data lowers total cost of ownership
read more
Client Configuration in WCF 4.0
read more
Changing Configuration Settings in a Desktop Client Application
read more
Clean Web.Config Files (VS 2010 and .NET 4.0 Series)
read more
Avoid appSettings Usage in Controls or Shared Libraries
read more
UppercuT - TeamCity Integration - Part 2 (Create a Build Configuration)
read more
|
|
Please login to rate or to leave a comment.