简单验证Smark.Data实体成员数据
创始人
2024-06-07 02:21:01
0

一般的验证,在ASP.NET中比较常见,这里我们将介绍这个验机制的实现和扩展。验证方法还可以通过查询数据库进行验证。

Smark.Data支持通过Atteribute的方式来描述一个实体对象在数据保存前进行数据的有效验证,使用人员也可以通过扩展自己的Attribute实现新的验证方式。

在Smark.Data中所有实体必须继承DataObject,这个对象主要包装了一些简单的实体操作(详细代码可以到: http://smark.codeplex.com/ 获取)。先看一下DataObject数据添加的代码:

  1. private void NewData(IConnectinContext cc, ObjectMapper om)  
  2.         {  
  3.             Insert insert = new Insert(om.Table);  
  4.             object value = null;  
  5.             if (om.ID != null)  
  6.             {  
  7.                 if (om.ID.Value != null)  
  8.                 {  
  9.                     if (!om.ID.Value.AfterByUpdate)  
  10.                     {  
  11.                         om.ID.Value.Executing(cc, this, om.ID,om.Table);  
  12.                         insert.AddField(om.ID.ColumnName, om.ID.Handler.Get(this));  
  13.                     }  
  14.                 }  
  15.                 else 
  16.                 {  
  17.                     insert.AddField(om.ID.ColumnName, om.ID.Handler.Get(this));  
  18.                 }  
  19.             }  
  20.             foreach (PropertyMapper pm in om.Properties)  
  21.             {  
  22.                 if (!EntityState._FieldState.ContainsKey(pm.Handler.Property.Name))  
  23.                 {  
  24.                     if (pm.Value != null && !pm.Value.AfterByUpdate)  
  25.                     {  
  26.                         pm.Value.Executing(cc, this, pm, om.Table);  
  27.                     }  
  28.                 }  
  29.                 value = pm.Handler.Get(this);  
  30.                 foreach (Validates.ValidaterAttribute val in pm.Validaters)  
  31.                 {  
  32.                     val.Validating(value, this, pm, cc);  
  33.                 }  
  34.                 if (EntityState._FieldState.ContainsKey(pm.Handler.Property.Name))  
  35.                 {  
  36.                     if (pm.Cast != null)  
  37.                     {  
  38.                         value = pm.Cast.ToColumn(value, pm.Handler.Property.PropertyType, this);  
  39.                     }  
  40.                     insert.AddField(pm.ColumnName, value);  
  41.                 }  
  42.            }  
  43.             insert.Execute(cc);  
  44.             if (om.ID != null && om.ID.Value != null && om.ID.Value.AfterByUpdate)  
  45.                 om.ID.Value.Executed(cc, this, om.ID,om.Table);  
  46.         } 

红色部分代码部分描述,如果当属性存在验证描述的情况,就执行相关验证;实际情况下有可能一个属性配置多个验证的,如:不能为空,范围值等等。

首先定义一个验证基础类来描述需要干什么。

  1. [AttributeUsage(AttributeTargets.Property)]  
  2.   public abstract class ValidaterAttribute : Attribute  
  3.   {  
  4.       public void Validating(object value, object source,Mappings.PropertyMapper pm,IConnectinContext cc )  
  5.       {  
  6.           if (!OnValidating(value, source, pm,cc))  
  7.               throw new ValidaterException(Message);  
  8.      }  
  9.       protected abstract bool OnValidating(object value, object source, Mappings.PropertyMapper pm, IConnectinContext cc);  
  10.       public string Message  
  11.       {  
  12.           get;  
  13.           set;  
  14.       }  
  15.   } 

既然明确了要干什么,那下面就好扩展了。

不为空

  1. [AttributeUsage(AttributeTargets.Property)]  
  2.  public class NotNull : ValidaterAttribute  
  3.  {  
  4.      public NotNull(string message)  
  5.     {  
  6.          Message = message;  
  7.      }  
  8.      protected override bool OnValidating(object value, object source, Mappings.PropertyMapper pm, IConnectinContext cc)  
  9.      {  
  10.          return value != null && !string.IsNullOrEmpty(value.ToString());  
  11.      }  
  12.  } 

长度限制

  1. [AttributeUsage(AttributeTargets.Property)]  
  2.   public class Length : ValidaterAttribute  
  3.   {  
  4.       public Length(string min, string max, string message)  
  5.      {  
  6.          if (!string.IsNullOrEmpty(min))  
  7.               MinLength = int.Parse(min);  
  8.           if (!string.IsNullOrEmpty(max))  
  9.               MaxLength = int.Parse(max);  
  10.           Message = message;  
  11.       }  
  12.       public int? MinLength  
  13.       {  
  14.           get;  
  15.           set;  
  16.       }  
  17.       public int? MaxLength  
  18.       {  
  19.           get;  
  20.           set;  
  21.       }  
  22.       protected override bool OnValidating(object value, object source, Mappings.PropertyMapper pm, IConnectinContext cc)  
  23.       {       
  24.           if (value != null && !string.IsNullOrEmpty(value.ToString()))  
  25.           {  
  26.               string data = Convert.ToString(value);  
  27.               if (MinLength != null && MinLength > data.Length)  
  28.               {  
  29.                   return false;  
  30.               }  
  31.  
  32.               if (MaxLength != null && data.Length > MaxLength)  
  33.               {  
  34.                   return false;  
  35.              }  
  36.          }  
  37.           return true;  
  38.       }    
  39.   } 

正则

  1. [AttributeUsage(AttributeTargets.Property)]  
  2.     public class Match : ValidaterAttribute  
  3.     {  
  4.         public Match(string regex, string message)  
  5.         {  
  6.             Regex = regex;  
  7.             Message = message;  
  8.         }  
  9.         public string Regex  
  10.         {  
  11.             get;  
  12.             set;  
  13.         }  
  14.       protected override bool OnValidating(object value, object source, Mappings.PropertyMapper pm, IConnectinContext cc)  
  15.         {        
  16.             if (value != null && !string.IsNullOrEmpty(value.ToString()))  
  17.             {  
  18.                 string data = Convert.ToString(value);  
  19.                 if (System.Text.RegularExpressions.Regex.Match(  
  20.                     data, Regex, RegexOptions.IgnoreCase).Length == 0)  
  21.                 {  
  22.                     return false;  
  23.                 }  
  24.             }  
  25.             return true;  
  26.         }  
  27.      }  
  28.     [AttributeUsage(AttributeTargets.Property)]  
  29.     public class EMail : Match  
  30.     {  
  31.         public EMail(string msg) : base(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", msg) { }  
  32.     }  
  33.     [AttributeUsage(AttributeTargets.Property)]  
  34.     public class CardID : Match  
  35.     {  
  36.         public CardID(string msg) : base(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/", msg) { }  
  37.     } 

当然还可以通过查询数据库进行验证

  1.   [AttributeUsage(AttributeTargets.Property)]  
  2.     public class Unique : ValidaterAttribute  
  3.     {  
  4.         public Unique(string err)  
  5.         {  
  6.             Message = err;  
  7.         }  
  8.         protected override bool OnValidating(object value, object source, Mappings.PropertyMapper pm, IConnectinContext cc)  
  9.         {  
  10.             if (value == null)  
  11.                 return true;  
  12.             if (string.IsNullOrEmpty((string)value))  
  13.                return true;  
  14.             string sql = "select {0} from {1} where {0}=@p1";  
  15.             Command cmd = new Command(string.Format(sql, pm.ColumnName, pm.OM.Table));  
  16.             cmd.AddParameter("p1", value);  
  17.             object result = cc.ExecuteScalar(cmd);  
  18.             return result == null || result == DBNull.Value;  
  19.         }  
  20.     }  
  21. 对于使用也很简单  
  22.         ///   
  23.         /// 用户名称  
  24.         ///   
  25.         [Column]  
  26.         [NotNull("用户名不能为空!")]  
  27.         [Length("5""16""用户名长度必须5-16个字符!")]  
  28.         [Unique("该用户名已经给其他用户使用!")]  
  29.        string UserName { getset; } 

原文标题:Smark.Data实体成员数据验证

链接:http://www.cnblogs.com/henryfan/archive/2009/09/15/1566951.html

【编辑推荐】

  1. ASP.NET应用执行验证
  2. ASP.NET数据验证控件CustomValidator的使用浅析
  3. ASP.NET数据验证技术研究详解
  4. ASP.NET数据验证五大常用控件浅析
  5. ASP.NET数据验证控件使用浅析

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...