Smark.Data支持通过Atteribute的方式来描述一个实体对象在数据保存前进行数据的有效验证,使用人员也可以通过扩展自己的Attribute实现新的验证方式。下面介绍一下这个验机制的实现和扩展。
在Smark.Data中所有实体必须继承DataObject,这个对象主要包装了一些简单的实体操作(详细代码可以到: http://smark.codeplex.com/ 获取)。先看一下DataObject数据添加的代码:
private void NewData(IConnectinContext cc, ObjectMapper om)
{
Insert insert = new Insert(om.Table);
object value = null;
if (om.ID != null)
{
if (om.ID.Value != null)
{
if (!om.ID.Value.AfterByUpdate)
{
om.ID.Value.Executing(cc, this, om.ID,om.Table);
insert.AddField(om.ID.ColumnName, om.ID.Handler.Get(this));
}
}
else
{
insert.AddField(om.ID.ColumnName, om.ID.Handler.Get(this));
}
}
foreach (PropertyMapper pm in om.Properties)
{
if (!EntityState._FieldState.ContainsKey(pm.Handler.Property.Name))
{
if (pm.Value != null && !pm.Value.AfterByUpdate)
{
pm.Value.Executing(cc, this, pm, om.Table);
}
}
value = pm.Handler.Get(this);
foreach (Validates.ValidaterAttribute val in pm.Validaters)
{
val.Validating(value, this, pm, cc);
}
if (EntityState._FieldState.ContainsKey(pm.Handler.Property.Name))
{
if (pm.Cast != null)
{
value = pm.Cast.ToColumn(value, pm.Handler.Property.PropertyType, this);
}
insert.AddField(pm.ColumnName, value);
}
}
insert.Execute(cc);
if (om.ID != null && om.ID.Value != null && om.ID.Value.AfterByUpdate)
om.ID.Value.Executed(cc, this, om.ID,om.Table);
}
红色部分代码部分描述,如果当属性存在验证描述的情况,就执行相关验证;实际情况下有可能一个属性配置多个验证的,如:不能为空,范围值等等。
首先定义一个验证基础类来描述需要干什么。
[AttributeUsage(AttributeTargets.Property)]
public abstract class ValidaterAttribute : Attribute
{
public void Validating(object value, object source,Mappings.PropertyMapper pm,IConnectinContext cc )
{
if (!OnValidating(value, source, pm,cc))
throw new ValidaterException(Message);
}
protected abstract bool OnValidating(object value, object source, Mappings.PropertyMapper pm, IConnectinContext cc);
public string Message
{
get;
set;
}
}
既然明确了要干什么,那下面就好扩展了。
不为空
[AttributeUsage(AttributeTargets.Property)]
public class NotNull : ValidaterAttribute
{
public NotNull(string message)
{
Message = message;
}
protected override bool OnValidating(object value, object source, Mappings.PropertyMapper pm, IConnectinContext cc)
{
return value != null && !string.IsNullOrEmpty(value.ToString());
}
}
长度限制
[AttributeUsage(AttributeTargets.Property)]
public class Length : ValidaterAttribute
{
public Length(string min, string max, string message)
{
if (!string.IsNullOrEmpty(min))
MinLength = int.Parse(min);
if (!string.IsNullOrEmpty(max))
MaxLength = int.Parse(max);
Message = message;
}
public int? MinLength