日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
簡單驗證Smark.Data實體成員數(shù)據(jù)

一般的驗證,在ASP.NET中比較常見,這里我們將介紹這個驗機制的實現(xiàn)和擴展。驗證方法還可以通過查詢數(shù)據(jù)庫進行驗證。

在萬源等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、成都網(wǎng)站制作 網(wǎng)站設(shè)計制作按需求定制設(shè)計,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站建設(shè),萬源網(wǎng)站建設(shè)費用合理。

Smark.Data支持通過Atteribute的方式來描述一個實體對象在數(shù)據(jù)保存前進行數(shù)據(jù)的有效驗證,使用人員也可以通過擴展自己的Attribute實現(xiàn)新的驗證方式。

在Smark.Data中所有實體必須繼承DataObject,這個對象主要包裝了一些簡單的實體操作(詳細代碼可以到: http://smark.codeplex.com/ 獲?。?。先看一下DataObject數(shù)據(jù)添加的代碼:

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

紅色部分代碼部分描述,如果當(dāng)屬性存在驗證描述的情況,就執(zhí)行相關(guān)驗證;實際情況下有可能一個屬性配置多個驗證的,如:不能為空,范圍值等等。

首先定義一個驗證基礎(chǔ)類來描述需要干什么。

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

當(dāng)然還可以通過查詢數(shù)據(jù)庫進行驗證

 
 
 
  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("該用戶名已經(jīng)給其他用戶使用!")]
  29.        string UserName { getset; }

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


分享題目:簡單驗證Smark.Data實體成員數(shù)據(jù)
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/ccicego.html