Export (0) Print
Expand All

Details of 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.

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( /* ... */ );
ValidationResults r = Validation.Validate<Customer>(myCustomer);

This code is equivalent to the following code example.

Customer myCustomer = new Customer( /* ... */ );
Validator<Customer> v = ValidationFactory.CreateValidator<Customer>();
ValidationResults r = 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, then you can use the CreateValidator method on 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");

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 that are on 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 Microsoft.Practices.EnterpriseLibrary.Validation Namespace in the API Reference section.

Validating an Object

Once 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( /* ... */ );
Validator<Customer> customerValidator =   
     ValidationFactory.CreateValidator<Customer>();
ValidationResults results = customerValidator.Validate(myCustomer);

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

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.
Show:
© 2014 Microsoft