Asp.Net MVC杂谈之:—步步打造表单验证框架[重排版](1)

在实际使用中,我们可以考虑多种形式来进行这一验证(注:本文目前只研究服务器端验证的情况),最直接的方式莫过于对每个表单值手动用C#代码进行验证了,比如:

if(!Int32.TryParse(Request.Form["age"], out age)){

xxxx...

}

If(age < xxx || age > xxx){

xxxx...

}

然而正如上面看到的一样,这种方式枯燥而繁琐,需要用户对每个字段都要手动效验,或许开发人员的一不小心就会造成系统的漏洞.因此,制造出一个能对这种行为进行自动进行的轮子势在必行,当然,到本文写作的时候为止,国外已经出现了一些Mvc下使用的验证框架,然而天下轮子不怕多,我在此厚颜再造出个,只希望不被冠上山寨之名.

该框架的缔造源自4MVC团队的Infancy项目,去年年底开始这个项目的时候,正是mvc框架加入ModelBinder的时候,当时便想到了通过使用ModelBinder来实现一种服务器端自动验证框架,经过多次修改,该框架慢慢实现了我需要的功能,本系列文章将再次回顾该过程,将该框架的一步步的实现过程加以更细致的重现.

下面正式开始框架的开发,首先我们明确下我们的基本需求:

  1. 该框架针对简单实体类(POCO)

  2. 该框架能自动对实体类的属性进行效验

  3. 该实体能被ModelBinder使用

  4. 能方便或者自动的执行该效验,并取得效验结果和信息

为了实现上面的目标,我们首先来确定一些需要使用的技术手段:

  1. 要能访问任意POCO的属性,必然用到反射

  2. 要能对属性进行限制,可选择使用XML或者Attribute,对程序员来说,Attribute远比XML来的方便和友好,因此选择Attribute

  3. 实现实体验证方法,使用Command模式

OK,下面开始事件了,首先考虑测试代码,假设我拥有实体Student,Student拥有属性Source,要求Source是int类型,且范围为0-100,那么测试代码的模式应该如下:

Student student = new Student(){

Source = -1

};

bool validateResult = student.Validate();

Assert.IsFalse(validateResult);

也就是说,我们需要在一个验证方法中对该对象的所有属性进行验证,那么我们考虑对系统各部分的构建,首先我们需要一个RangeAttribute,这个类能包含对属性的验证信息,大致如下:

public class RangeAttribute : Attribute{

public int Mix{ get; set; } //范围下限

public int Max{ get; set; } //范围上限

public string Message{ get; set;} //出错信息

public RangeAttribute(int min, int max, string message){

Min = min;

Max = max;

Message = message;

}

}

这样一来我们的Student就可以如此构造:

public class Student{

Range(0, 100, "分数的范围必须在{0}和{1}之间.")

public int Source{ get; set; }

}

然而,这样仅仅是个花架子,在默认情况下这个Range没有起到任何作用,除了程序员看到代码之后知道了Source有这样的限制要求,那么,我们需要如何将这个Attribute和验证结合起来呢?自然就是反射.

我们在Student中实现如下方法:

public bool Validate(){

bool result = true;

Type type = this.GetType();

PropertyInfo property = type.GetProperty("Source"); //获取Source属性

RangeAttribute [] attributes =

property.GetCustomAttributes(typeof(Attribute), true)

as RangeAttribute []; //获取Range属性集合

//遍历集合对属性进行验证

foreach(var item in attribute){

int value = (int)property.GetValue(this, null);

if(value < item.Min || value > item.Max){

result = false;

}

}

return result;

}

那么再回过头看先前的测试,我们可以发现,测试成功运行了(相关代码见附带项目的Leven.Validate01和test项目的Validate01Test.cs).

我们在看目前的代码,现在我们能测试Source,如果我们的Student类中还有一项Age,范围为6-150呢,那么Student中加上如下代码:

Range(6, 150, "学生年龄必须在{0}和{1}之间.")

public int Age { get; set; }

那么我们的Validate方法是否能正确验证呢?为了验证结果,我们重新编写测试:

TestMethod()

public void ValidateTest() {

Student student = new Student() {

Source = 80,

Age = 0

};

bool validateResult = student.Validate();

Assert.IsFalse(validateResult);

student.

validateResult = student.Validate();

}

执行测试,很遗憾,测试无法通过了.我们可以再看看Validate方法,可以发现,其中只对Source属性进行了验证,那么我们可以想办法修改代码,让其能对Age和Source方法同时验证.

public bool Validate() {

bool result = true;

Type type = this.GetType();

PropertyInfo[] properties = type.GetProperties();

foreach (var property in properties) {

RangeAttribute[] attributes =

property.GetCustomAttributes(typeof(RangeAttribute), true) as RangeAttribute[];

foreach (var item in attributes) {

int value = (int)property.GetValue(this, null);

if (value < item.Min || value > item.Max) {

result = false;

}

}

}

return result;

}

修改过的方法中将遍历所有的属性,然后进行验证,这时候再次运行测试,生动的绿色代表我们重新获得了成功.

下面我们再次考虑新的可能情况,如果Student需要一个Name属性,这是一个必须字段.我们考虑新增一个RequiredAttribute来实现该功能,该部分代码如下(参见项目Leven.Validate03):

AttributeUsage(AttributeTargets.Property)

public class RequiredAttribute : Attribute {

public bool IsRequired { get; set; }

public string Message { get; set; }

public RequiredAttribute(string message) {

IsRequired = true;

Message = message;

}

}

然后修改Student部分,新增下面部分:

Required

public string Name { get; set; }

再次修改测试代码:

TestMethod()

public void ValidateTest() {

Student student = new Student() {

Age = 20,

Source = 89,

Name = string.Empty

};

bool validateResult = student.Validate();

Assert.IsFalse(validateResult);

}

执行测试,结果失败了.查看原因,显然可以看到,是Validate方法中

RangeAttribute[] attributes =

property.GetCustomAttributes(typeof(RangeAttribute), true) as RangeAttribute[];

只验证了RangeAttribute,那针对我们加入的RequiredAttribute自然是无能为力了,为了能验证RequiredAttribute,我们再次修改了代码:

public bool Validate() {

bool result = true;

bool requiredResult = true;

Type type = this.GetType();

PropertyInfo[] properties = type.GetProperties();

foreach (var property in properties) {

RangeAttribute[] attributes =

property.GetCustomAttributes(typeof(RangeAttribute), true)

as RangeAttribute[]; //获取RangeAttribute集合

RequiredAttribute[] requiredAttributes =

property.GetCustomAttributes(typeof(RequiredAttribute), true)

as RequiredAttribute[]; //获取RequiredAttribute集合

//遍历RangeAttribute进行验证

foreach (var item in attributes) {

int value = (int)property.GetValue(this, null);

if (value < item.Min || value > item.Max) {

result = false;

}

}

//遍历RequiredAttr进行验证

foreach (var item in requiredAttributes) {

object value = property.GetValue(this, null);

if (value is string) {

if (String.IsNullOrEmpty((value as string))) {

requiredResult = false;

}

} else {

if (value == null) {

requiredResult = false;

}

}

}

}

return result && requiredResult;

}

相关推荐
fe7tQnVan2 小时前
三大 Agent-UI 协议深度剖析:AG-UI、A2UI 与 MCP-UI 的设计哲学与工程实践
ui·状态模式·命令模式
LcGero3 小时前
Lua + Cocos Creator 实战:用 Lua 驱动 UI 与游戏逻辑
游戏·ui·lua
ZC跨境爬虫3 小时前
Playwright进阶操作:鼠标拖拽与各类点击实战(含自定义拖拽实例)
前端·爬虫·python·ui
DYuW5gBmH3 小时前
如何一步步将 ASP.NET MVC 升级为.NET
ui
for_ever_love__4 小时前
Objective-C学习:UI的初步了解
学习·ui·objective-c
小灰灰搞电子4 小时前
Qt UI 线程详解-阻塞与解决方案
开发语言·qt·ui
spencer_tseng4 小时前
UI 2026.03.26
ui
MwEUwQ3Gx5 小时前
用户智能体交互协议AG-UI(下)
ui·状态模式·交互
互联网散修1 天前
鸿蒙应用开发UI基础第三十六节:Grid网格布局二维自适应宫格与不规则布局方案
ui