Programmatic Validation Configuration

When validating an object/Property/Parameter, the registered validator(s) is discovered and executed. This is so that validation can be modified at runtime as a result of business logic execution or self-validating code generation. The performance hit of this feature is minimized by the caching of Validators in validation registries. Validation can be reverted, augmented or replaced for Objects, Properties and Parameters by calling the correct method(s) on the validation registries which are implemented as extension methods to types Type, PropertyInfo, ParameterInfo. The extension methods are located in the root ValidationAspects namespace.
  • RevertValidation() reverts validation back to as declared in code.
  • ReplaceValidation(IEnumerable<IValidatorFactory>) and ReplaceValidation<TValue>(Action<TValue, IValidationContext>) replaces the current validation.
  • AddValidation(IEnumerable<IValidatorFactory>) and AddValidation<TValue>(Action<TValue, IValidationContext>) augments the current validation.

Examples:
typeof(Customer).RevertValidation();

TypeOf<Customer>.GetProperty(x => x.Name).ReplaceValidation(factories);
typeof(Customer).GetProperty("Name").AddValidation<string>((v, c) => if (v == "Mike") throw new ValidationException("Mike's not the list!"););

typeof(Customer).GetMethod("SetEmail").GetParameters()[0].AddValidation(factories);

If you apply validation on an object's Base types and interfaces, these validators will also be executed when the object is validated.

Validation can reverted for all Types or Properties or Parameters by calling the static ClearCache() on TypeValidationRegistry, PropertyValidationRegistry or ParameterValidationRegistry.

Last edited Feb 3, 2009 at 5:01 PM by mikesaunders, version 11

Comments

No comments yet.