Share via


Validating Objects

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

You can validate an object once you have attached the validators and the rule sets. The easiest way to do this is to use the Validation façade. Another approach is to get a reference to a Validator instance and invoke its Validate method. This topic demonstrates the following approaches:

  • Using the Validation Façade
  • Using the CreateValidator Method
  • Validating an Object
  • Validating Objects Using Rule Sets

Using the Validation Façade

In most situations, you can take advantage of the Validation façade. This class allows you to create a Validator instance and validate an object with a single line of code. This is shown in the following code example.

Customer myCustomer = new Customer();
// Set properties of Customer object here
ValidationResults r = Validation.Validate<Customer>(myCustomer);
'Usage
Dim myCustomer As New Customer()
' Set properties of Customer object here
Dim r As ValidationResults = Validation.Validate(Of Customer)(myCustomer)

This code is equivalent to the following code example.

Customer myCustomer = new Customer();
// Set properties of Customer object here
Validator<Customer> v = ValidationFactory.CreateValidator<Customer>();
ValidationResults r = v.Validate(myCustomer);
'Usage
Dim myCustomer As New Customer()
' Set properties of Customer object here
Dim v As Validator(Of Customer) = ValidationFactory.CreateValidator(Of Customer)()
Dim r As ValidationResults = v.Validate(myCustomer)

The façade is appropriate in most situations. One case where the façade may not be the best approach is if you want to reuse the same validator instance to validate several different objects. The façade may be inefficient because it creates a new validator instance for each object that you want to validate.

Using the CreateValidator Method

If you do not want to use the façade, you can use the CreateValidator method of the ValidationFactory class to first get a reference to the Validator instance. This is shown in the following code example.

// assumes default rule set
Validator<Customer> customerValidator = ValidationFactory.CreateValidator<Customer>();

// uses named rule set
Validator<Customer> goldCustomerValidator = ValidationFactory.CreateValidator<Customer>("GoldCustomer");
'Usage
' assumes default rule set
Dim customerValidator As Validator(Of Customer) = ValidationFactory.CreateValidator(Of Customer)()

' uses named rule set
Dim goldCustomerValidator As Validator(Of Customer) = ValidationFactory.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.

There are specialized methods named CreateValidatorFromConfiguration and CreateValidatorFromAttributes available in the ValidationFactory class. These may be more efficient if you know you that you only need to examine configuration or attributes. The CreateValidator method always examines both. For more information, see the Microsoft.Practices.EnterpriseLibrary.Validation namespace in the API Reference section.****

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.

Customer myCustomer = new Customer();
// Set properties of Customer object here
Validator<Customer> customerValidator = ValidationFactory.CreateValidator<Customer>();
ValidationResults results = customerValidator.Validate(myCustomer);
'Usage
Dim myCustomer As New Customer()
' Set properties of Customer object here
Dim customerValidator As Validator(Of Customer) = ValidationFactory.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 Validate method of the Validation façade accepts parameters that allow 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. The Validate method takes as parameters a reference to the object to validate and the rule set name(s).

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.

ValidationResults results = Validation.Validate(myCustomer, "RuleSetA");
'Usage
Dim results As ValidationResults = Validation.Validate(myCustomer, "RuleSetA")

If you have two rule sets nam RuleSetA and RuleSetB defined in the configuration of the application, you can apply the rules from both using the following code.

ValidationResults results = Validation.Validate(myCustomer, "RuleSetA", "RuleSetB");
'Usage
Dim results As ValidationResults = Validation.Validate(myCustomer, "RuleSetA", "RuleSetB")