I am using DataAnnotation
Attributes to apply validation to properties on my model, outside of MVC.
public class MyModel
{
[Required]
[CustomValidation]
public string Foo { get; set; }
}
I have implemented the following extension method to validate the model.
public static void Validate(this object source)
{
if (source == null)
throw new ArgumentNullException("source");
var results = new List<ValidationResult>();
bool IsValid = Validator.TryValidateObject(source, new ValidationContext(source, null, null), results, true);
if (!IsValid)
results.ForEach(r => { throw new ArgumentOutOfRangeException(r.ErrorMessage); });
}
I have to call this Validate()
method every time I set a property which is not convenient:
MyModel model = new MyModel();
model.Foo = "bar";
model.Validate();
model.Foo = SomeMethod();
model.Validate();
I would like that the Validate()
method be called automatically behind the scenes when the state of the model changes. Does anyone have any idead on how to achieve this?
For bonus points, does anyone know exactly how MVC achieve this automatic validation via DataAnnotations
?
Thanks.