The first CTP release of the Enterprise Library, which is available for download from CodePlex, contains the first release of the long awaited Validation block.
Tom already gave some insight in what could be expected. Today I downloaded the bits and investigated the current implementation.
First of all: the help documentation is not ready and the EntLib Config application does not handle the validation block yet.
I will demo here two of the posible validation scenario's. First of all the validation based on Attributes. You simply take the data class and decorate the properties or fields with the validation attributes.
public class Customer
{
[StringLengthValidator(2,15)]
public string Name;
[TodayValidator()]
public DateTime BirthDate;
}
On the Name field we see StringLengthValidator that will validate if a Name has a length of minimum 2 and maximum 15.
On the BirthDate field I created a Validator myself. It validates if the Birthdate is less than the current date. Creating a custom validator is simple by just inheriting ValidatorAttribute:
public class TodayValidator : ValidatorAttribute
{
public override IValidator CreateValidator()
{
return new RangeValidator<DateTime>(DateTime.Now);
}
}
To check if the defined validations are met the following code is used:
ValidationResults attributedValidation = Validation.Validate<Customer>(customer);
foreach (ValidationResult result in attributedValidation)
{
Console.WriteLine("{0}:{1}", result.Key, result.Message);
}
The ValidationResults class has also an IsValid property which is set accordingly.
One of the nice things is the posibility to define the validation rules in a configurationsource, for example the app.config file.
The following config defines two rulesets and the stringlength validator on the name attribute of the Customer class:
<configSections>
<section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation" />
</configSections>
<validation>
<type name="Entlib3Ctp.Customer" defaultRule="relaxed">
<rule name="relaxed">
<fields>
<field name="Name">
<add name="NameMaxLength" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation" upperBound="15" upperBoundType="Inclusive"/>
</field>
</fields>
</rule>
<rule name="strong">
<fields>
<field name="Name">
<add name="NameMaxLength" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation" upperBound="10" upperBoundType="Inclusive"/>
</field>
</fields>
</rule>
</type>
</validation>
In the validaton config first define the Type for which the rules are to be used. Then you define one or more rules. Within each rule you configurate the field the validators should act on.
By using a name for the ruleset you can switch in code between relaxed and a more tight validation. The relaxed validation can be used for example if the user needs to store the data temporary.
To choose which ruleset to use call the ValidationFactory:
IValidator<Customer> strongRules = ValidationFactory.CreateValidatorFromConfiguration<Customer>("strong");
In this case the "strong" rules are loaded and therefore the Name field of the Customer object should be no more than 10 characters.
I do not want to give any indications on performance of the validation logic but I will share some of my observations. First of all the call to Validation.Validate will create the validators based on the configuration source AND attributes in each call. If you need to validate the same object type multiple times create the validators only once by calling the appropiate ValidationFactory method. In the current CTP version creating the validators based on attributes takes approximately twice the time than creation of the validators defined in the app.config. If you know you're ruleset is in the config file use the methods ...FromConfiguration to prevent reflecting over your type.
The first look at the Validation block from the Enterprise Library 3.0 looks very promising. It will be a good fit for most of the projects.
In the attached zip you will find a small demo project.