Declaring Validation

A class must be decorated with the ValidationAspects.PostSharp.ValidateAttribute to have the relevant PostSharp Interception Validation aspects applied to it at compilation. The ValidateAttribute can instead be applied to an assembly so that all classes of that assembly or a specified namespace can be validated. Unity Interception does not require the use of a custom attribute.
[Validate]
class Customer { ... }

Validation of Properties and Parameters can be declared in code by decorating Properties and Parameters with validation attributes or registering validation functions on them. Attributes exist for all the Built-in Validators and are located in the ValidationAspects namespace.

Validators are generated by validation factories for the validation target type. This results in type safety and compilation errors (when using PostSharp) when a validator is applied to an unsupported type.
// build failure - the Minimum validator factory does not support generating a validator for typeof(string)
[Minimum(1.23)]
public string Name { get; set; }

Property Validation

Validators can be applied to properties with a non-abstract setter. When ValidationSettings.InterceptValidateProperties is enabled and the setter is executed, the property invocation is intercepted and the validators applied to the property are invoked. All validators will be invoked and failing validators will throw a ValidationException derived exception. ValidationExceptions have a Context member which provides where applicable the (declaring) Instance, Declaring Type, Property, Method and Parameter being executed when validation failed.
[InRange(0.0, 100.0]
public decimal Total{ get; set; }

Parameter Validation

Validators can be applied to non out parameters. Failing parameter validators will throw a ValidationException derived exception.
public void Update([NotNullOrEmpty] string name, [GreaterThan(0)] int age) { ... }

Method Validation

Object validators can be registered on methods. This results in the object validator being invoked as a pre-condition to executing the method.

Object Validation

Validation can be applied to objects by implementing one or more validation methods and/or registering validator functions/factories on the object's Type (and base type/interface).

Validation Methods:
The validation method should be static, have the void return type, have one parameter of type the type of the validation target and be decorated with the ValidationAspects.ValidationMethodAttribute. The name of the method is not important. The method should throw a ValidationException derived exception for a failed validation.
class Customer 
{
  [ValidationMethod]
  public static void ValidateAddress(Customer customer)
  { 
    ...
    throw new ValidationException("Address is invalid!");
  }
}
Type Validation:
[AddressValidation]
class Address { ... }
// or
typeof(Manager).AddValidation(new [] { new ManagerValidation() } );
Object validation will also invoke the object's property validators if ValidationSettings.StateValidateProperties is enabled.

Use:
Object validation can be invoked directly on the object or when the object is passed to a Property setter by use of the ValidateObject validator.
Customer customer = new Customer();
...
ValidationResult result = customer.Validate();
customer.Validate() is achieved by an extension method on typeof(object). The method constructs and executes the ValidateObject validator. The ValidationResult can be queried for an IsValid value, the validation messages and the validation exception hierarchy which provides context on what/where validation failed.
[ValidateObject]
public Customer TheCustomer { get; set; }
When the TheCustomer setter is executed, the ValidateObject validator is invoked and a validation exception is thrown if the object is invalid.

Last edited Feb 3, 2009 at 4:59 PM by mikesaunders, version 16

Comments

No comments yet.