Validating Objects

Microsoft Enterprise Library 5.0

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

You can validate an object once you have obtained a reference to a validation factory and attached the validators and the rule sets. The easiest way to obtain a reference to a validation factory is through the Enterprise Library container, as described in Creating and Referencing Enterprise Library Objects. This topic demonstrates the following techniques:

Note:
In previous releases of Enterprise Library, you could use the static Validation facade to validate objects without first creating a validator. You could also use the static ValidationFactory class (as opposed to the replacement non-static ValidatorFactory class) to create validator instances. These approaches are supported in this release for backwards compatibility with existing application code. However, to take advantage of benefits available when using dependency injection, you should use the techniques described in this section. For information about using the static facades, see the Enterprise Library documentation on MSDN.

Creating Validator Instances

The CreateValidator method of the ValidatorFactory class allows you to obtain a reference to an appropriate Validator instance. This is shown in the following code example, which assumes you have resolved an instance of the ValidatorFactory class and stored it in a variable named valFactory.

C# Copy Code
// assumes default rule set
Validator<Customer> customerValidator = valFactory.CreateValidator<Customer>();

// uses named rule set
Validator<Customer> goldCustomerValidator = valFactory.CreateValidator<Customer>("GoldCustomer");
Visual Basic Copy Code
' assumes default rule set
Dim customerValidator As Validator(Of Customer) = valFactory.CreateValidator(Of Customer)()

' uses named rule set
Dim goldCustomerValidator As Validator(Of Customer) = valFactory.CreateValidator(Of Customer)("GoldCustomer")

The first case retrieves a reference to an instance of the Validator class that will check all Customer rules from the anonymous rule set. The second case retrieves an instance of the Validator class that will check all Customer rules in the "GoldCustomer" rule set.

Specifying the Location of Validation Rules

By default, when you create a validator using the ValidatorFactory class, that validator will look for rules defined in configuration and in attributes in the validated classes. If you know you that only need to examine configuration or attributes for rules, you can use an alternative factory class to create validators. The other types of factory you can use are:

  • ConfigurationValidatorFactory. This factory creates validators that only apply rules defined in a configuration file, or in a configuration source you provide. By default it looks for configuration in the default configuration file (App.config or Web.config). However, you can create an instance of a class that implements the IConfigurationSource interface, populate it with configuration data from another file or configuration storage media, and use this when you create this validator factory.
  • AttributeValidatorFactory. This factory creates validators that only apply rules defined in Validation block attributes located in the target class, and rules defined through Self validation methods.
  • ValidationAttributeValidatorFactory. This factory creates validators that only apply rules defined in .NET Data Annotations validation attributes.

You can resolve an instance of these classes through the Enterprise Library container in exactly the same way as you would the ValidatorFactory class. All of the validator factory classes expose the same interface for creating validators. The only difference is the location(s) in which they target validation rules.

Validating an Object

After you have the reference to a Validator instance, you can validate objects. Use the Validate method to do this. This is shown in the following code example.

C# Copy Code
Customer myCustomer = new Customer();
// Set properties of Customer object here
Validator<Customer> customerValidator = valFactory.CreateValidator<Customer>();
ValidationResults results = customerValidator.Validate(myCustomer);
Visual Basic Copy Code
Dim myCustomer As New Customer()
' Set properties of Customer object here
Dim customerValidator As Validator(Of Customer) = valFactory.CreateValidator(Of Customer)()
Dim results As ValidationResults = customerValidator.Validate(myCustomer)

This example creates a Customer object and invokes the Validate method of the Customer object's validator for that customer.

Validating Objects Using Rule Sets

The CreateValidator method of the ValidatorFactory façade accepts a parameter that allows you to specify a rule set containing the validation rules. You can use this approach instead of using attributes to apply validators directly to members of a class, or in conjunction with validators defined in attributes.

For example, if you have only a single rule set named RuleSetA defined in the configuration of the application, you can apply this rule set to an object named myCustomer using the following code.

C# Copy Code
Customer myCustomer = new Customer();
// Set properties of Customer object here
Validator<Customer> customerValidator = valFactory.CreateValidator<Customer>("RuleSetA");
ValidationResults results = customerValidator.Validate(myCustomer);
Visual Basic Copy Code
Dim myCustomer As New Customer()
' Set properties of Customer object here
Dim customerValidator As Validator(Of Customer) = valFactory.CreateValidator(Of Customer)("RuleSetA")
Dim results As ValidationResults = customerValidator.Validate(myCustomer)