Data Annotations
I've recently discovered the System.ComponentModel.DataAnnotations namespace. This is a collection of Attributes you can use to markup your data objects with conditions, like the following.
[Range(0, 150)]
public int Age { get; set; }
[RegularExpression(@"(?i:^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$)")]
public string Email { get; set; }
}
Let's say that you use this Person object to store user input, you can easily validate that input meets the requirements of the annotations.
/* Validate */ var result = person.ValidateAnnotations();
What I find even more interesting is that you can annotate a contract and just let your model object inherit the annotations. Consider the following.
public int Age { get; set; }
public string Email { get; set; }
}
public interface IPerson { [StringLength(20), Required(ErrorMessage = "Name is a required property")] string Name { get; set; }
[Range(0, 150)]
int Age { get; set; }
[RegularExpression(@"(?i:^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$)")]
string Email { get; set; }
}
Now that's hot! The annotations are actually part of the interface IPerson that specifies what an IPerson is. Then the concrete Person class just implements that contract. Neat! My validating extension method and some tests can be downloaded from here.
Enjoy!