您的位置:首页 > 编程语言 > ASP

ASP.NET MVC基于标注特性的Model验证:一个Model,多种验证规则

2014-06-28 15:12 309 查看
原文:ASP.NET MVC基于标注特性的Model验证:一个Model,多种验证规则对于Model验证,理想的设计应该是场景驱动的,而不是Model(类型)驱动的,也就是对于同一个Model对象,在不同的使用场景中可能具有不同的验证规则。举个简单的例子,对于一个表示应聘者的数据对象来说,针对应聘的岗位不同,肯定对应聘者的年龄、性别、专业技能等方面有不同的要求。但是ASP.NET MVC的Model验证确是Model驱动的,因为验证规则以验证特性的形式应用到Model类型及其属性上。这样的验证方式实际上限制了Model类型在基于不同验证规则的使用场景中的重用。通过上一篇文章《将ValidationAttribute应用到参数上》的扩展我们将验证特性直接应用在参数上变成了可能,这从一定程度上解决了这个问题,但是只能解决部分问题,因为应用到参数的验证特性只能用于针对参数类型级别的验证,而不能用于针对参数类型属性级别的验证(源代码从这里下载)。[本文已经同步到《How ASP.NET MVC Works?》中]

目录
一、同一个Model在采用不同的验证规则
二、新的基类ValidatorAttribute
三、指定当前采用的验证规则:ValidationRuleAttribute
四、新的Controller基类:RuleBasedController
五、自定义ModelValidatorProvider:RuleBasedValidatorProvider

一、同一个Model在采用不同的验证规则

现在我们通过利用对ASP.NET MVC的扩展来实现一种基于不同验证规则的Model验证。为了让读者对这种认证方式有一个感官的认识,我们来看看这个扩展最终实现怎样的验证效果。在通过Visual Studio的ASP.NET MVC 项目模板创建的空Web应用中,我们定义了如下一个Person类型作为Model。

[code]     [code] public class Person


{


[DisplayName("姓名")]


     public string Name{ get; set;}


 


[DisplayName("性别")]


     public string Gender{ get; set;}


 


[DisplayName("年龄")]


[RangeValidator(10, 20, RuleName = "Rule1",  ErrorMessage = "{0}必须在{1}和{2}之间!")]


[RangeValidator(20, 30, RuleName = "Rule2", ErrorMessage = "{0}必须在{1}和{2}之间!")]


[RangeValidator(30, 40, RuleName = "Rule3", ErrorMessage = "{0}必须在{1}和{2}之间!")]


     public int Age{ get; set;}     


}

[/code]
[/code]

在表示年龄的Age属性上应用了三个RangeValidatorAttribute(不是RangeAttribute),它们对应针对年龄的三种不同的验证规则,RuleName属性表示规则名称。三种验证规则(Rule1、Rule2和Rule3)分别要求年龄分别在10到20、20到30和30到40岁之间。

然后我们定义了具有如下定义HomeController,它具有三组Action方法(Index、Rule1和Rule2)。方法Rule1、Rule2和HomeController类上应用了一个ValidationRuleAttribute特性用于指定了当前采用的验证规则。用于指定验证规则的ValidationRuleAttribute特性可以同时应用于Controller类型和Action方法上,应用于后者的ValidationRuleAttribute特性具有更高的优先级。针对HomeController的定义,Action方法Index、Rule1和Rule2分别采用的验证规则为Rule3、Rule1和Rule2。

[code]
[code] [ValidationRule("Rule3")]


public class HomeController : RuleBasedController


{


public ActionResult Index()


{


return View("person", new Person());


}


[HttpPost]


public ActionResult Index(Person person)


{


return View("person", person);


}


 


[ValidationRule("Rule1")]


public ActionResult Rule1()


{


return View("person", new Person());


}


[HttpPost]


[ValidationRule("Rule1")]


public ActionResult Rule1(Person person)


{


return View("person", person);


}


 


[ValidationRule("Rule2")]


public ActionResult Rule2()


{


return View("person", new Person());


}


[HttpPost]


[ValidationRule("Rule2")]


public ActionResult Rule2(Person person)


{


return View("person", person);


}


}

[/code]
[/code]

定义在HomeController中的6个方法均将创建的/接收的Person对象呈现到名为Person的View中,该View的定义如下所示。这是一个将Person类型作为Model的强类型View,在该View中我们将作为Model的Person对象以编辑模式呈现在一个表单中,并在表单中提供一个提交按钮。

[code]
[code] @model Person


@using (Html.BeginForm())


{ 


@Html.EditorForModel()


<input type="submit" value="保存" />


}

[/code]
[/code]

现在运行我们的程序,并通过在浏览器中指定相应的地址分别访问定义在HomeController的三个Action(Index、Rule1和Rule2),一个用于编辑个人信息的表单会呈现出来。然后我们根据三个Action方法采用的验证规则输入不合法的年龄,然后点击“保存”按钮,我们会看到输入的年龄按照对应的规则被验证了,具体的验证效果如下图所示。





二、新的基类ValidatorAttribute

我们现在就来具体谈谈上面这个例子所展示的基于不同规则的Model验证是如何实现的。首先我们需要重建一套新的验证特性体系,只为我们能够指定具体的验证规则。为此我们定义了一个抽象的ValidatorAttribute类型,如下面的代码片断所示,ValidatorAttribute直接继承自ValidationAttribute,属性RuleName表示采用的验证规则名称。我们重写了TypeId属性,因为我们需要在相同的属性或者类型上应用多个同类的ValidatorAttribute。

[code]
[code] [AttributeUsage( AttributeTargets.Class| AttributeTargets.Property,AllowMultiple = true)]


public abstract class ValidatorAttribute: ValidationAttribute


{


private object typeId;


     public string RuleName{ get; set;}


public override object TypeId


{


get{return typeId ?? (typeId = new object());}


}


}

[/code]
[/code]

上面演示实例采用的RangeValidatorAttribute定义如下,我们可以看到它仅仅是对RangeAttribute的封装。RangeValidatorAttribute具有与RangeAttribute一致的构造函数定义,并直接使用被封装的RangeAttribute实施验证。除了能够通过RuleName指定具体采用的验证规则之外,其他的使用方式与RangeAttribute完全一致。

[code]
[code] [AttributeUsage( AttributeTargets.Property, AllowMultiple = true)]


public class RangeValidatorAttribute:ValidatorAttribute


{


private RangeAttribute rangeAttribute;


public RangeValidatorAttribute(int minimum, int maximum)


{


rangeAttribute = new RangeAttribute(minimum, maximum);


}


public RangeValidatorAttribute(double minimum, double maximum)


{


rangeAttribute = new RangeAttribute(minimum, maximum);


}


public RangeValidatorAttribute(Type type, string minimum, string maximum)


{


rangeAttribute = new RangeAttribute(type, minimum, maximum);


}


public override bool IsValid(object value)


{


return rangeAttribute.IsValid(value);


}


 


public override string FormatErrorMessage(string name)


{


         return string.Format(CultureInfo.CurrentCulture, base.ErrorMessageString, new object[]{ name, rangeAttribute.Minimum, rangeAttribute.Maximum});


}


}

[/code]
[/code]

三、指定当前采用的验证规则:ValidationRuleAttribute

ValidatorAttribte的RuleName属性仅仅指定了验证特性采用的验证规则名称,当前应在采用的验证规则通过应用在Action方法或者Controller类型上的ValidationRuleAttribute特性还指定。如下所示的就是ValidationRuleAttribute的定义,它仅仅包含一个表示当前采用的验证规则名称的RuleName属性的特性而已。

[code]
[code] [AttributeUsage( AttributeTargets.Class| AttributeTargets.Method)]


public class ValidationRuleAttribute: Attribute


{


     public string RuleName{ get; private set;}


public ValidationRuleAttribute(string ruleName)


{


this.RuleName = ruleName;


}


}

[/code]
[/code]

四、新的Controller基类:RuleBasedController

对于这个用于实现针对不同验证规则的扩展来说,其核心是如何将通过ValidationRuleAttribute特性设置的验证规则应用到ModelValidator的提供机制中,使之筛选出与当前验证规则匹配的验证特性,在这里我们依然使用Controller上下文来保存这个这个验证规则名称。细心的读者应该留意到了上面演示实例中创建的HomeController不是继承自Controller,而是继承自RuleBasedController,这个自定义的Controller基类定义如下。

[code]
[code] public class RuleBasedController: Controller


{


private static Dictionary<Type, ControllerDescriptor> controllerDescriptors = new Dictionary<Type, ControllerDescriptor>();


public ControllerDescriptor ControllerDescriptor


{


get


{


ControllerDescriptor controllerDescriptor;


if (controllerDescriptors.TryGetValue(this.GetType(), out controllerDescriptor))


{


return controllerDescriptor;


}


lock (controllerDescriptors)


{


if (!controllerDescriptors.TryGetValue(this.GetType(), out controllerDescriptor))


{


controllerDescriptor = new ReflectedControllerDescriptor(this.GetType());


controllerDescriptors.Add(this.GetType(), controllerDescriptor);


}


return controllerDescriptor;


}


}


}


protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)


{


SetValidationRule();


return base.BeginExecuteCore(callback, state);


}        


protected override void ExecuteCore()


{


SetValidationRule();


base.ExecuteCore();


}


private void SetValidationRule()


{


string actionName = this.ControllerContext.RouteData.GetRequiredString("action");


ActionDescriptor actionDescriptor = this.ControllerDescriptor.FindAction(this.ControllerContext, actionName);


if (null != actionDescriptor)


{


ValidationRuleAttribute validationRuleAttribute = actionDescriptor.GetCustomAttributes(true).OfType<ValidationRuleAttribute>().FirstOrDefault() ??


this.ControllerDescriptor.GetCustomAttributes(true).OfType<ValidationRuleAttribute>().FirstOrDefault() ??


new ValidationRuleAttribute(string.Empty);


this.ControllerContext.RouteData.DataTokens.Add("ValidationRuleName", validationRuleAttribute.RuleName);


}


}


}

[/code]
[/code]

在继承自Controller的RuleBasedController中,ExecuteCore和BeginExecuteCore方法被重写,在调用基类的同名方法之前,方法SetValidationRule方法被调用将应用在当前Action方法或者Controller类型上的ValidationRuleAttribute特性指定的验证规则名称保存到当前Controller上下文中。由于对Action方法和Controller类上特性的解析需要使用到用于描述Controller的ControllerDescriptor对象,处于性能考虑,我们对该对象进行了全局缓存。

五、自定义ModelValidatorProvider:RuleBasedValidatorProvider

对于应用在同一个属性或者类型上的多个基于不同验证规则的ValidatorAttribute,对应的验证规则名称并没有应用到具体的验证逻辑中。以上面定义的RangeValidatorAttribute为例,具体的验证逻辑通过被封装的RangeAttribute来实现,如果我们不做任何的处理,所有的基于不同规则的RangeValidatorAttribute都还参与到最终的Model验证过程中。我们必须作的是在根据验证特性创建ModelValidator的时候只选择那些与当前验证规则一直的ValidatorAttribute,这样的操作实现在具有如下定义的RuleBasedValidatorProvider中。

[code]
[code] public class RuleBasedValidatorProvider : DataAnnotationsModelValidatorProvider


{


protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)


{


object validationRuleName = string.Empty;


context.RouteData.DataTokens.TryGetValue("ValidationRuleName", out validationRuleName);


string ruleName = validationRuleName.ToString();


attributes = this.FilterAttributes(attributes, ruleName);


return base.GetValidators(metadata, context, attributes);


}


 


private IEnumerable<Attribute> FilterAttributes(IEnumerable<Attribute> attributes, string validationRule)


{


var validatorAttributes = attributes.OfType<ValidatorAttribute>();


var nonValidatorAttributes = attributes.Except(validatorAttributes);


List<ValidatorAttribute> validValidatorAttributes = new List<ValidatorAttribute>();


 


if (string.IsNullOrEmpty(validationRule))


{


validValidatorAttributes.AddRange(validatorAttributes.Where(v => string.IsNullOrEmpty(v.RuleName)));


}


else


{


var groups = from validator in validatorAttributesgroup validator by validator.GetType();


foreach (var group in groups)


{


ValidatorAttribute validatorAttribute = group.Where(v => string.Compare(v.RuleName, validationRule, true) == 0).FirstOrDefault();


if (null != validatorAttribute)


{


validValidatorAttributes.Add(validatorAttribute);


}


        else


{


validatorAttribute = group.Where(v => string.IsNullOrEmpty(v.RuleName)).FirstOrDefault();


    if (null != validatorAttribute)


{


    validValidatorAttributes.Add(validatorAttribute);


}


}


}


}


return nonValidatorAttributes.Union(validValidatorAttributes);


}


}

[/code]
[/code]

如上面的代码所示,RuleBasedValidatorProvider继承自DataAnnotationsModelValidatorProvider,基于当前验证规则(从当前的Controller上下文中提取)对ValidatorAttribute的筛选,以及ModelValidator的创建通过重写的GetValidators方法实现。具体的筛选机制是:如果当前的验证规则存在,则选择与之具有相同规则名称的第一个ValidatorAttribute,如果这样的ValidatorAttribute找不到,则选择第一个没有指定验证规则的ValidatorAttribute;如果当前的验证规则没有指定,那么也选择第一个没有指定验证规则的ValidatorAttribute。

在让我们的Controller继承自RuleBasedController之后,我们需要在Global.asax中通过如下的方式对自定义的RuleBasedValidatorProvider进行注册,然后我们的应用就能按照我们期望的方式根据你指定的验证规则实施Model验证了。

[code]
[code] public class MvcApplication : System.Web.HttpApplication


{


//其他成员


protected void Application_Start()


{


//其他操作


DataAnnotationsModelValidatorProvider validator = ModelValidatorProviders.Providers.OfType<DataAnnotationsModelValidatorProvider>().FirstOrDefault();


if(null != validator)


{


ModelValidatorProviders.Providers.Remove(validator);


}


ModelValidatorProviders.Providers.Add(new RuleBasedValidatorProvider());


}


}

[/code]
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: