[英]MVC2 client/server validation of DateTime/Date using DataAnnotations
[英]Automatic Model Validation using DataAnnotations outside MVC
我正在使用DataAnnotation
属性将验证应用于MVC之外的模型上的属性。
public class MyModel
{
[Required]
[CustomValidation]
public string Foo { get; set; }
}
我已经实现了以下扩展方法来验证模型。
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); });
}
每次设置不方便的属性时,我都必须调用此Validate()
方法:
MyModel model = new MyModel();
model.Foo = "bar";
model.Validate();
model.Foo = SomeMethod();
model.Validate();
我希望在模型状态更改时在后台自动调用Validate()
方法。 是否有人对如何实现这一目标有任何想法?
对于奖励积分,没有人确切知道MVC如何通过DataAnnotations
实现此自动验证吗?
谢谢。
您可以在每次setter调用之后使用proxies包装类,拦截属性setter和验证对象。 对于Castle DynamicProxy,这将是:
public class ValidationInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
if (invocation.Method.IsSpecialName && invocation.Method.Name.StartsWith("set_"))
{
invocation.InvocationTarget.Validate();
}
}
}
然后,您将使用DynamicProxy而不是new运算符创建模型:
ProxyGenerator proxyGenerator = new ProxyGenerator();
MyModel model = proxyGenerator.CreateClassProxy<MyModel>(new ValidationInterceptor());
model.Foo = "bar";
model.Foo = SomeMethod();
重要的是MyModel的所有属性都必须是虚拟的,以便代理工作:
public class MyModel
{
public virtual string Foo { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.