简单验证Smark.Data实体成员数据

开发
这里将介绍如何简单验证Smark.Data实体成员数据,Smark.Data支持通过Atteribute的方式来描述一个实体对象。希望本文能对大家有所帮助。

一般的验证,在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数据验证控件使用浅析
责任编辑:彭凡 来源: 博客园
相关推荐

2014-04-01 10:11:33

C语言指针

2009-09-09 16:07:16

Linq实体关系

2009-09-10 09:09:40

Linq实体继承

2009-10-13 14:42:30

VB.NET静态成员

2009-05-15 10:34:09

C#实体验证Entity Vali

2010-07-01 09:21:22

MemBaseNoSQL

2009-11-12 14:55:16

ADO.NET实体框架

2017-08-07 10:04:49

数据数据治理数据管理

2020-11-04 08:40:23

C++多元组Tuple

2018-07-02 13:10:05

Android短信验证

2009-12-11 15:17:52

PHP验证码调用

2021-11-11 08:34:54

DataInputStDataOutputSPrintStream

2011-06-22 16:02:37

Qt 多线程 重入

2022-02-14 21:31:00

用户身份验证

2010-01-05 09:15:45

Java EE 6Bean验证

2010-02-01 10:22:51

C++数据指针

2009-07-10 11:25:48

Swing应用数据验证

2009-08-04 15:02:18

ASP.NET数据验证

2010-02-04 15:58:39

C++浅拷贝

2022-02-17 13:04:57

网络验证运营商
点赞
收藏

51CTO技术栈公众号