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

在萬(wàn)源等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、成都網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需求定制設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,全網(wǎng)營(yíng)銷推廣,成都外貿(mào)網(wǎng)站建設(shè),萬(wàn)源網(wǎng)站建設(shè)費(fèi)用合理。
Smark.Data支持通過Atteribute的方式來描述一個(gè)實(shí)體對(duì)象在數(shù)據(jù)保存前進(jìn)行數(shù)據(jù)的有效驗(yàn)證,使用人員也可以通過擴(kuò)展自己的Attribute實(shí)現(xiàn)新的驗(yàn)證方式。
在Smark.Data中所有實(shí)體必須繼承DataObject,這個(gè)對(duì)象主要包裝了一些簡(jiǎn)單的實(shí)體操作(詳細(xì)代碼可以到: 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)屬性存在驗(yàn)證描述的情況,就執(zhí)行相關(guān)驗(yàn)證;實(shí)際情況下有可能一個(gè)屬性配置多個(gè)驗(yàn)證的,如:不能為空,范圍值等等。
首先定義一個(gè)驗(yà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;
- }
- }
既然明確了要干什么,那下面就好擴(kuò)展了。
不為空
- [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());
- }
- }
長(zhǎng)度限制
- [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ù)庫(kù)進(jìn)行驗(yàn)證
- [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;
- }
- }
- 對(duì)于使用也很簡(jiǎn)單
- ///
- /// 用戶名稱
- ///
- [Column]
- [NotNull("用戶名不能為空!")]
- [Length("5", "16", "用戶名長(zhǎng)度必須5-16個(gè)字符!")]
- [Unique("該用戶名已經(jīng)給其他用戶使用!")]
- string UserName { get; set; }
鏈接:http://www.cnblogs.com/henryfan/archive/2009/09/15/1566951.html
網(wǎng)頁(yè)標(biāo)題:簡(jiǎn)單驗(yàn)證Smark.Data實(shí)體成員數(shù)據(jù)
分享路徑:http://m.fisionsoft.com.cn/article/ccicego.html


咨詢
建站咨詢
