Introduction to Enterprise Library: Part III

In previous article, we had discussed about few attribute based validators. Now, we look into some more attribute validators and end up with rule sets. We will discuss about PropertyComparisonValidator, RegexValidator and ValidatorComposition. Open the application used in previous article. Add new properties to Employee class as shown below:

We are using PropertyComparsion validator to compare two properties of a class. Here, I assumed a condition, that bonus should be less than salary. So, I used this validator and compared it with salary property as shown above. Regular expression validator is used to validate data against a pattern like email, phone number etc. Just give the pattern for which the property's format should satisfy.

Now, add the code to Main method to validate these new conditions as shown below:

Run the application, the output will be like this:

There are lot more validators present in VAB. I will list all those for future reference.

Not Null - NotNullValidator Value must not be NULL
Contains Characters - ContainsCharactersValidator For checking, whether a string contains specified characters or not like special characters
Regular Expression - RegexValidator For checking, Format of the data
Range - RangeValidator For checking, data's valid values like age between 0 - 100
Relative DateTime - RelativeDateTimeValidator For comparing difference between a specific date and today date.
String Length - StringLengthValidator For checking character count in a string
Domain - DomainValidator For checking the domain for a value like IsManager property should have YES or NO only.
Enum Comparsion - EnumConversionValidator For checking,  Conversion of  a string  to Enum type is possible or not
Type Conversion - TypeConversionValidator For checking, Conversion of a string to a specified type is possible or not
Property Comparsion - PropertyComparisonValidator For comparing two properties of a class
Negated This attribute is applicable to above all validators for negating the condition like Value should be NULL by making Negated= true for Not Null validator

ValidatorComposition is used to combine two or more validators. When we add two or more validators for a property; by default, both validators should pass [Since, it uses internally logical AND on the validators]. We can change the behavior by using ValidatorComposition to OR. Add below validator to empname as shown below:

The above validator will fail only, if both empname more than 10 characters and empname not in E1, E2 and E3 validators fails. So, we can use ValidatorComposition to aggregate validators using either logical AND or OR.

Few restrictions on using Attribute based Validators:

  1. You should have to source code to implement these validators.
  2. Need to recompile the code, whenever we change the validator's properties.

Finally, we look into Rule sets and its use. By using rule sets, we can group validators and make it to work as a whole based on specific environment. For example, in Employee class, by setting rule set = "TEST" we can run only those validators having rule set as TEST. We can do this assigning it to TEST rule set and passing that rule set name to CreateValidator method.

I am ending up the Attribute based validators here. I am attaching source code for reference. In coming articles, we will go deep into this Application block. I hope this article will be helpful for all.


Similar Articles