新聞中心
一般的驗證,在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ù)添加的代碼:
- 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);
- }
紅色部分代碼部分描述,如果當(dāng)屬性存在驗證描述的情況,就執(zhí)行相關(guān)驗證;實際情況下有可能一個屬性配置多個驗證的,如:不能為空,范圍值等等。
首先定義一個驗證基礎(chǔ)類來描述需要干什么。
- [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
- {
- get;
- set;
- }
- public int? MaxLength
- {
- get;
- set;
- }
- protected override bool OnValidating(object value, object source, Mappings.PropertyMapper pm, IConnectinContext cc)
- {
- if (value != null && !string.IsNullOrEmpty(value.ToString()))
- {
- string data = Convert.ToString(value);
- if (MinLength != null && MinLength > data.Length)
- {
- return false;
- }
- if (MaxLength != null && data.Length > MaxLength)
- {
- return false;
- }
- }
- return true;
- }
- }
正則
- [AttributeUsage(AttributeTargets.Property)]
- public class Match : ValidaterAttribute
- {
- public Match(string regex, string message)
- {
- Regex = regex;
- Message = message;
- }
- public string Regex
- {
- get;
- set;
- }
- protected override bool OnValidating(object value, object source, Mappings.PropertyMapper pm, IConnectinContext cc)
- {
- if (value != null && !string.IsNullOrEmpty(value.ToString()))
- {
- string data = Convert.ToString(value);
- if (System.Text.RegularExpressions.Regex.Match(
- data, Regex, RegexOptions.IgnoreCase).Length == 0)
- {
- return false;
- }
- }
- return true;
- }
- }
- [AttributeUsage(AttributeTargets.Property)]
- public class EMail : Match
- {
- 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) { }
- }
- [AttributeUsage(AttributeTargets.Property)]
- public class CardID : Match
- {
- public CardID(string msg) : base(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/", msg) { }
- }
當(dāng)然還可以通過查詢數(shù)據(jù)庫進行驗證
- [AttributeUsage(AttributeTargets.Property)]
- public class Unique : ValidaterAttribute
- {
- public Unique(string err)
- {
- Message = err;
- }
- protected override bool OnValidating(object value, object source, Mappings.PropertyMapper pm, IConnectinContext cc)
- {
- if (value == null)
- return true;
- if (string.IsNullOrEmpty((string)value))
- return true;
- string sql = "select {0} from {1} where {0}=@p1";
- Command cmd = new Command(string.Format(sql, pm.ColumnName, pm.OM.Table));
- cmd.AddParameter("p1", value);
- object result = cc.ExecuteScalar(cmd);
- return result == null || result == DBNull.Value;
- }
- }
- 對于使用也很簡單
- ///
- /// 用戶名稱
- ///
- [Column]
- [NotNull("用戶名不能為空!")]
- [Length("5", "16", "用戶名長度必須5-16個字符!")]
- [Unique("該用戶名已經(jīng)給其他用戶使用!")]
- string UserName { get; set; }
鏈接:http://www.cnblogs.com/henryfan/archive/2009/09/15/1566951.html
分享題目:簡單驗證Smark.Data實體成員數(shù)據(jù)
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/ccicego.html


咨詢
建站咨詢
