Validating Objects
Collapse the table of content
Expand the table of content

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.

After you attach the validators and rule sets, you can validate an object. The easiest way to do this is to use the Validation façade. The façade allows you to 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 cust = new Customer();
ValidationResults results = Validation.Validate<Customer>(cust, customerRuleSetCombo.Text);

If you do not want to use the façade, you can use the following code, which is the equivalent.

Customer cust = new Customer();
Validator<Customer> validator = ValidationFactory.CreateValidator<Customer>(customerRuleSetCombo.Text);
ValidationResults results = validator.Validate(cust);

Use the CreateValidator method on the ValidationFactory class to first get a reference to the Validator instance, and then use the Validate method to validate the object.

The Validate method of the Validation façade accepts as parameters the name of the object to validate, and optionally the names of the validation rule sets (defined in the application configuration) sets to apply. 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 customer using the following code.

ValidationResults results = Validation.Validate(cust, "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(cust, "RuleSetA", "RuleSetB");


Show:
© 2016 Microsoft