This project is read-only.


ValidationAspects provides State and Interception validation on .net Objects, Properties and Method Parameters. Validation can be declared via attributes and/or augmented/replaced with validation functions at runtime. Supports MVC, WPF, Silverlight, PostSharp, and Unity.


  • Object Validation - Objects can be queried to determine if their state is valid against a set of validation rules. Validation messages and context of what/where validation failed is reported via a ValidationResult. An object can also be validated as a method pre-condition by registering an object validator against the method.
  • Property Validation - Multi-Mode validation. The following modes can be enabled or disabled independantly to suit your chosen method of validation:
    1. Interception Validation - Validation is invoked when the property is set. If the validation fails, a validation exception will throw to ensure the model does not become invalid.
    2. State Validation - Validation is invoked when the object is validated. If the validation fails, the validation failure is added to the ValidationResult.
  • Parameter Validation - Parameter validation checks the validity of the value as determined by code contracts and business rules. If the validation fails, a validation exception will throw to ensure the model does not become invalid.
  • Real-time augmentation / replacement - Validation can be changed programmatically or via Xaml configuration.
  • AOP - Interception validation aspects are applied to your models by PostSharp or Unity. There are no base classes to derive from or interfaces to explicitly implement.
  • Custom Validators - Implement new validators easily with minimal code with lambda syntax.
  • Strongly-typed validation targets - Compile-time checking (PostSharp only) ensures validation is correctly declared - you can't declare a numeric validator to validate a string. Validators are implemented to target certain types. Incorrectly declared validators applied at run-time fail fast.
  • Integration - Support for WPF, / MVC (via, and Silverlight (State Validation only, PostSharp 1.5 interception in progress).
  • Localization - Validation exception messages can be customized with message formatting functions.
  • TDD'd - Near 100% code coverage from hundreds of unit tests.

ValidationAspects can run standalone for State Validation only. For Interception Validation it can integrate with an AOP framework. Currently PostSharp 1.0 SP1 / 1.5 RC1 and Unity Interception are supported.

I am no longer maintaining VA in the short term due to life and other project commitments. Unfortunately I've had to shelve open source projects like VA indefinitely. I'd like to get back into it sometime. If anyone wants to modify VA to support PostSharp 2.0, .net 4.0, or add further features or documentation, please send me a patch and I will publish a new VA release. Alternatively, if you want to get more involved and have some ownership such as providing support to VA users, please contact me and I'll add you to the project.


Attribute Registration:
public class Customer
  private string _email;

  // apply validation to property setters
  [NotNullOrEmpty] public string Name { get; set; }
  [Minimum(0)] public int Age { get; set; }

  // apply validation to method parameters
  public void SetEmail([NotNullOrEmpty] string email) { _email = email; }

  // provide validation for objects either with methods or declaring validators on the class
  public static void ValidateEmail(Customer customer)
    if (string.IsNullOrEmpty(customer._email))
      throw new ValidationException("Email is not valid");
Programmatic Registration:
// register lambda syntax validation functions
typeof(User).GetProperty("Name").AddValidation<string>((name, context) => 
  { if (!Exists(name)) { throw new ValidationException("Username is unknown"); } } );

// register validation factories (classes)
typeof(User).GetProperty("Name").AddValidation(new [] { new NotNullOrEmpty()} );

// don't like strings?
TypeOf<User>.Property(user => user.Name).AddValidation(new [] { new NotNullOrEmpty()} );
Object Validation:
ValidationResult result = customer.Validate();
if (!result.IsValid)
  foreach (string message in result.Messages) { ... }
Property Validation - Interception Validation Mode:
  customer.Name = "";
catch (ValidationException e)
  foreach (string message in e.Messages) { ... }

Last edited Dec 15, 2009 at 10:54 AM by mikesaunders, version 58