ASP.NET MVC Model验证-DataAnnotationsModelValidator

  对于ASP.NET MVC基于标注特性的Model验证,很多人只知道应用在数据类型及其属性上用于定义验证规则和错误消息的ValidationAttribute。通过《ASP.NET MVC Model验证-ModelValidator》 的介绍,我们知道了最终用于进行Model验证的是一个叫做ModelValidator的组件。ValidationAttribute对应的 ModelValidator为DataAnnotationsModelValidator,这篇简短的文章为你介绍ASP.NET MVC是如何针对Validation来创建DataAnnotationsModelValidator,以及后者如何利用前者实施Model验证 的。

一、DataAnnotationsModelValidator

  ModelValidator 是真正用于进行Model验证的组件,上面介绍的验证特性最终被封装成DataAnnotationsModelValidator对象进而被应用到 Model验证系统中。如下面的代码片断所示,被封装的ValidationAttribute通过只读属性Attribute表示,该属性在构造函数中 被初始化。

   1: public class DataAnnotationsModelValidator : ModelValidator

   2: {

   3:     public DataAnnotationsModelValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute);

   4:     public override IEnumerable<ModelClientValidationRule>  GetClientValidationRules();

   5:  

   6:     public override IEnumerable<ModelValidationResult> Validate(object container)

   7:     {

   8:         ValidationContext validationContext = new ValidationContext(container ?? this.Metadata.Model, null, null)

   9:         {

  10:             DisplayName = this.Metadata.GetDisplayName()

  11:         };

  12:         ValidationResult validationResult = this.Attribute.GetValidationResult(this.Metadata.Model, validationContext);

  13:         if (validationResult != ValidationResult.Success)

  14:         {

  15:             ModelValidationResult iteratorVariable2 = new ModelValidationResult

  16:             {

  17:                 Message = validationResult.ErrorMessage

  18:             };

  19:             yield return iteratorVariable2;

  20:         }

  21:         else

  22:         {

  23:             yield break;

  24:         }

  25:     }

  26:     protected ValidationAttribute Attribute { get; }

  27:     protected string ErrorMessage { get; }

  28:     public override bool IsRequired { get; }

  29: }

  我们给出了用于实施验证的核心方法Validate的完整定义。在该方法中,基于被验证对象(如果为Null则采用Model元数据的Model属 性)创建出表示当前验证上下文的ValidationContext对象,并采用Model元数据的DisplayName属性作为该上下文的显示名称。 最后直接调用被封装的ValidationAttribute的GetValidationResult方法对指定对象实施验证,如果返回的 ValidationResult对象不为空,则以此创建ModelValidationResult对象并返回。

  顺便在说说定义在DataAnnotationsModelValidator中的另外两个受保护只读属性的逻辑。用于返回错误消息的ErrorMessage属性来源对对ValidationAttribute的FormatErrorMessage方法的调用,而指定的参数就是当前Model元数据的DisplayName属性。由于只有RequiredAttribute才用于必需字段的验证,所有只有被封装ValidationAttribute为RequiredAttribute时其IsRequired属性返回True。

二、DataAnnotationsModelValidator<TAttribute>

  DataAnnotationsModelValidator<TAttribute>是DataAnnotationsModelValidator的子类,其泛型参数为相应的ValidationAttribute的类型,下面的代码片断反映了其定义:

   1: public class DataAnnotationsModelValidator<TAttribute> : DataAnnotationsModelValidator where TAttribute: ValidationAttribute

   2: {    

   3:     public DataAnnotationsModelValidator(ModelMetadata metadata, ModelBindingExecutionContext context, TAttribute attribute);

   4:     protected TAttribute Attribute { get; }

   5: }

  作为DataAnnotationsModelValidator与相应ValidationAttribute之间的适配,ASP.NET MVC为常用的ValidationAttribute(RequiredAttribute、RangeAttribute、 RegularExpressionAttribute和StringLengthAttribute)定义相应的适配类型。如下面的代码片断所示,它们 都是泛型的DataAnnotationsModelValidator<TAttribute>的子类。当我们将这些 ValidationAttribute应用到Model类型时,真正用于Model验证的实际上就是这些作为适配的ModelValidator。

   1: public class RequiredAttributeAdapter : DataAnnotationsModelValidator<RequiredAttribute>

   2: {

   3:     public RequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute);

   4:     public override IEnumerable<ModelClientValidationRule> GetClientValidationRules();

   5: }

   6:  

   7: public class RangeAttributeAdapter : DataAnnotationsModelValidator<RangeAttribute>

   8: {    

   9:     public RangeAttributeAdapter(ModelMetadata metadata, ControllerContext context, RangeAttribute attribute);

  10:     public override IEnumerable<ModelClientValidationRule> GetClientValidationRules();

  11: }

  12:  

  13: public class RegularExpressionAttributeAdapter : DataAnnotationsModelValidator<RegularExpressionAttribute>

  14: {    

  15:     public RegularExpressionAttributeAdapter(ModelMetadata metadata, ControllerContext context, RegularExpressionAttribute attribute);

  16:     public override IEnumerable<ModelClientValidationRule> GetClientValidationRules();

  17: }

  18:  

  19: public class StringLengthAttributeAdapter : DataAnnotationsModelValidator<StringLengthAttribute>

  20: {    

  21:     public StringLengthAttributeAdapter(ModelMetadata metadata, ControllerContext context, StringLengthAttribute attribute);

  22:     public override IEnumerable<ModelClientValidationRule>GetClientValidationRules();

  23: }

 

加支付宝好友偷能量挖...


评论(0)网络
阅读(112)喜欢(0)asp.net-mvc