ValidationAspects
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 asp.net MVC, WPF, Silverlight, PostSharp, and Unity.
Features
- 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:
- 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.
- 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, asp.net / asp.net MVC (via http://www.codeplex.com/xval), 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
http://www.postsharp.org and Unity Interception
http://www.codeplex.com/unity are supported.
Documentation
Blog
http://thetreeknowseverything.net/ NEW
Examples
Attribute Registration:
[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) { ... }
}
Property Validation - Interception Validation Mode:
try
{
customer.Name = "";
}
catch (ValidationException e)
{
foreach (string message in e.Messages) { ... }
}
Road Map
- Silverlight interception validation using PostSharp 1.5
- Quick Start examples.