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

一般的验证,在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.               if (MaxLength != null && data.Length > MaxLength)
  32.               {
  33.                   return false;
  34.              }
  35.          }
  36.           return true;
  37.       }  
  38.   }

正则

 
 
 
  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; }

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

文章题目:简单验证Smark.Data实体成员数据
URL标题:http://www.shufengxianlan.com/qtweb/news26/62826.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联