下面我们再次考虑新的可能情况,如果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;
}
}
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; }
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);
}
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;
}
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;
}
这次的代码量增加了不少,不过经过我们的不懈努力,测试再一次通过了,但是,我们再次回来查看验证部分的代码,不难发现一个问题,每次我们新增了验证Attribute之后都必须手动在Validate方法中增加响应的代码,目前我们还只有两个Attribute,如果一个系统中有20甚至200个Attribute(当然只是打个比方),该方法的长度恐怕将是个恐怖的数字,这样的方法势必无比丑陋。
我们如何能忍受这样的代码出现在项目中呢,而且这样一来,该方法出错的几率将大大增加,甚至还没有直接手动编码验证来的容易,那么,有没有方法将这部分验证逻辑分割并使之变得优雅呢?这正是我们下一节将要解决的问题.