ValidationAspects looks to be a very nice library. Wery well tested with ~100% test coverage and wrapped with PostSharp (as a separate dll).
I've checked it into lib/ - use it on all Entities to set validations.
[Validate] 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 [ValidationMethod] 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) { ... } }