在Web开发中,表单提交算是一种很常见的从客户端获取数据的方式了,然而,用户的行为永远都是无法预料的,为此,我们在程序中不得已必须对用户输入的数据进行严格效验,在WebForm时代我们常用的手段是验证控件,但是到了Mvc时代,再使用控件变得困难了,因此我们必须找到新的方式来解决这个问题.
    在实际使用中,我们可以考虑多种形式来进行这一验证(注:本文目前只研究服务器端验证的情况),最直接的方式莫过于对每个表单值手动用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{ getset; } //范围下限
    public int Max{ getset; } //范围上限
    public string Message{ getset;} //出错信息
    
    
public RangeAttribute(int min, int max, string message){
    Min 
= min;
    Max 
= max;
    Message 
= message;
}
}
    这样一来我们的Student就可以如此构造:
public class Student{
    [Range(
0100, “分数的范围必须在{0}和{1}之间.”)]
    
public int Source{ getset; } 
}
    然而,这样仅仅是个花架子,在默认情况下这个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(thisnull);
        
if(value < item.Min || value > item.Max){
            result 
= false;
            }
        }
        
return result;
    }
    那么再回过头看先前的测试,我们可以发现,测试成功运行了(相关代码见附带项目的Leven.Validate01和test项目的Validate01Test.cs).
    我们在看目前的代码,现在我们能测试Source,如果我们的Student类中还有一项Age,范围为6-150呢,那么Student中加上如下代码:
[Range(6150"学生年龄必须在{0}和{1}之间.")]
public int Age { getset; }
     那么我们的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), trueas RangeAttribute[];
        
foreach (var item in attributes) {
            
int value = (int)property.GetValue(thisnull);
            
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 { getset; }
    
public string Message { getset; }
    
public RequiredAttribute(string message) {
        IsRequired 
= true;
        Message 
= message;
    }
}
    然后修改Student部分,新增下面部分:
[Required]
public string Name { getset; }
    再次修改测试代码:
[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), trueas 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(thisnull);
            
if (value < item.Min || value > item.Max) {
                result 
= false;
            }
        }
        
//遍历RequiredAttr进行验证
        foreach (var item in requiredAttributes) {
            
object value = property.GetValue(thisnull);
            
if (value is string) {
                
if (String.IsNullOrEmpty((value as string))) {
                    requiredResult 
= false;
                }
            } 
else {
                
if (value == null) {
                    requiredResult 
= false;
                }
            }
        }
    }
    
return result && requiredResult;
}
    这次的代码量增加了不少,不过经过我们的不懈努力,测试再一次通过了,但是,我们再次回来查看验证部分的代码,不难发现一个问题,每次我们新增了验证Attribute之后都必须手动在Validate方法中增加响应的代码,目前我们还只有两个Attribute,如果一个系统中有20甚至200个Attribute(当然只是打个比方),该方法的长度恐怕将是个恐怖的数字,这样的方法势必无比丑陋,我们如何能忍受这样的代码出现在项目中呢,而且这样一来,该方法出错的几率将大大增加,甚至还没有直接手动编码验证来的容易,那么,有没有方法将这部分验证逻辑分割并使之变得优雅呢?这正是我们下一节将要解决的问题.
    附:本次内容的源代码/Files/leven/LevenValidateLibrary01.rar


posted on 2009-03-26 09:28  Leven  阅读(9050)  评论(23编辑  收藏  举报
CopyRight 2008, Leven's Blog xhtml | css
Leven的个人Blog