摘要:在我们录入数据时,对数据有效性的验证是必不可少的, ActiveRecord中如何去验证数据的有效性呢?本文将详细介绍这一内容。
主要内容
1.概述
2.使用Validation
3.如何扩展
4
.深入分析验证
一.概述
在录入数据时,对数据有效性的验证是必不可少的,很多时候我们在UI层上就会做一层验证,但有时也需要在底层做一些必要的处理,这就要用到ActiveRecord中的数据有效性的验证。ActiveRecord为我们提供了如下几个验证:
n ValidateEmail
n ValidateIsUnique
n ValidateRegExp
n ValidateNotEmpty
n ValidateConfirmation
二.如何使用
为了使用上面这些验证,我们必须用ActiveRecordValidationBase来代替ActiveRecordBase,即实体类必须继承于ActiveRecordValidationBase。
[ActiveRecord(
"
Customs
"
)]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public
class
Custom : ActiveRecordValidationBase
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
ActiveRecordValidationBase类为我们提供了如下一个方法和属性:
方法|属性
| 说明
|
IsValid()
| 返回验证是否通过
|
ValidationErrorMessages
| 获取验证错误信息数组
|
下面看一个完整的例子代码,在这个程序中我们需要验证用户名不能为空,Email地址、邮政编码、电话号码的格式是否正确
[ActiveRecord(
"
Customs
"
)]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public
class
Custom : ActiveRecordValidationBase
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _id;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string _name;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string _email;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string _address;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string _post;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string _phone;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[PrimaryKey(PrimaryKeyType.Native)]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int ID
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this._id; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this._id = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property,ValidateNotEmpty]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Name
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this._name; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this._name = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property,ValidateEmail]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Email
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this._email; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this._email = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Address
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this._address; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this._address = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property,ValidateRegExp(@"/d{6}")]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Post
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this._post; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this._post = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property,ValidateRegExp(@"(/(/d{3,4}/)|/d{3,4}-)?/d{8}")]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Phone
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this._phone; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this._phone = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static void DeleteAll()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ActiveRecordBase.DeleteAll(typeof(Custom));
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Custom[] FindAll()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return ((Custom[])(ActiveRecordBase.FindAll(typeof(Custom))));
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
编写一些简单的测试代码,大家有兴趣可以看一下:
[Test]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public
void
TestNameValidation()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//姓名为空
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Custom custom = new Custom();
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Address = "TianJin";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Email = "lhj_cauc@hotmail.com";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Phone = "022-24096356";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Post = "300192";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//错误消息数
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int expectedError = 1;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Assert.IsFalse(custom.IsValid());
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Assert.AreEqual(expectedError,custom.ValidationErrorMessages.Length);
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
[Test]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public
void
TestPostValidation()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//邮政编码错误、Email错误
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Custom custom = new Custom();
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Name = "Terry Lee";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Email = "lhj_cauc#hotmail.com";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Phone = "022-24096356";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Post = "222t";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Address = "Tianjin";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//错误消息数
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int expectedError = 2;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Assert.IsFalse(custom.IsValid());
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Assert.AreEqual(expectedError,custom.ValidationErrorMessages.Length);
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
[Test]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public
void
TestAllValidation()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//全部正确
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Custom custom = new Custom();
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Name = "Terry Lee";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Email = "lhj_cauc@hotmail.com";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Phone = "022-24096335";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Address = "TianJin";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
custom.Post = "300192";
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//错误消息数
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int expectedError = 0;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Assert.IsTrue(custom.IsValid());
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Assert.AreEqual(expectedError,custom.ValidationErrorMessages.Length);
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
三.如何扩展
上面这些验证已经能够满足我们绝大多数的需求,但是我们也可以去添加自己的验证。来看看ActiveRecord中的Validation的类结构图(只画出了部分)
通过上图可以看到,如果想添加自己的验证,需要有一个继承AbstractValidator和继承于AbstractValidationAttribute的类就可以了,具体可以参考ActiveRecord的代码。
四.深入分析验证
通过上面的分析我们都知道所有的实体类都继承于ActiveRecordValidationBase基类,那么ActiveRecord是如何通过特性来进行验证的呢?下面我们结合源码进一步分析一下。
我们在属性上加上了验证, Attribute并不做任何实质性的工作,它只是调用验证器进行验证,先来看一下ValidateNotEmptyAttribute的代码:
[Serializable]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public
class
ValidateNotEmptyAttribute : AbstractValidationAttribute
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ValidateNotEmptyAttribute() : base(new NullCheckValidator())
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ValidateNotEmptyAttribute(String errorMessage) : base(new NullCheckValidator(), errorMessage)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
所有验证工作都是在Validator中进行的,以NullCheckValidator为例来看它做了什么操作:
[Serializable]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public
class
NullCheckValidator : AbstractValidator
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public NullCheckValidator()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override bool Perform(object instance, object fieldValue)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return fieldValue != null && fieldValue.ToString().Length != 0;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override string BuildErrorMessage()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return String.Format("{0} is not optional.", Property.Name);
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
这个类其实很简单,但我们注意到有一个Perform的方法,正是这个方法完成了验证工作,拿这个例子来说,如果字段的值为空或长度等于零就返回false,否则为true。对于正则验证等其他的也都是在这个方法中完成。回到ActiveRecordValidationBase中去,看这个类初始化的时候做了什么操作?
/**/
/// <summary>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// Constructs an ActiveRecordValidationBase
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// </summary>
public
ActiveRecordValidationBase()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CollectValidators( this.GetType() );
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// Collect the validations applied to this class properties.
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// <param name="targetType"></param>
private
void
CollectValidators( Type targetType )
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ActiveRecordModel model = GetModel( targetType );
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (model == null)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
throw new ActiveRecordException("Seems that the framework wasn't initialized properly. (ActiveRecordModel could not obtained)");
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
__validators.AddRange( model.Validators );
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while( model.Parent != null )
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
__validators.AddRange( model.Parent.Validators );
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
model = model.Parent;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
在初始化的时候,通过ActiveRecordModel获取到当前实体所有属性对应的Validator,并放在了一个ArrayList中去,然后在IsValid()方法中再逐一调用Validator的Perform()方法来判断验证是否通过,因为所有的验证器都实现了Ivalidator。如果有验证发生错误,则把错误信息保存再一个字符数组中,可以通过属性ValidationErrorMessages来获取错误信息。
public
bool
IsValid()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ArrayList errorlist = new ArrayList();
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
__failedProperties = new Hashtable();
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach(IValidator validator in __validators)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!validator.Perform(this))
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String errorMessage = validator.ErrorMessage;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
errorlist.Add( errorMessage );
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ArrayList items = null;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (__failedProperties.Contains(validator.Property))
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
items = (ArrayList) __failedProperties[validator.Property];
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
else
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
items = new ArrayList();
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
__failedProperties[validator.Property] = items;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
items.Add(errorMessage);
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_errorMessages = (String[]) errorlist.ToArray( typeof(String) );
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return errorlist.Count == 0;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
在ActiveRecord中的数据有效性验证就介绍到这儿了,下篇我会介绍ActiveRecord常用的一些技巧。[非常感谢idior大哥提出的意见]
参考资料
Castle
的官方网站http://www.castleproject.org 作者:
TerryLee
出处:
http://terrylee.cnblogs.com