摘要:統一管理表單物件驗證的機制
在開發系統時常會有控制項需要驗證的需求,通常都會很直覺的寫成如下所示:
if (!String.IsNullOrEmpty(this.tbName.Text)){
....
}
else {
....
}
但是萬一頁面控制項一多了,那這樣瑣碎的Code就會穿插在方法裡面,所以筆者想出一個管理的機制並且去實作出一些類別來使用。內容如下所示:
//執行驗證動作的委派
public delegate bool VaildateHandler();
//驗證管理員
public class VaildateManager
{
private Dictionary _dictionary;
public VaildateManager()
{
_dictionary = new Dictionary();
}
//取得對應索引數值的驗證項目集合
public VaildateMemberCollection this[int index]
{
get
{
VaildateMemberCollection collection = null;
int i = 0;
foreach (string key in this._dictionary.Keys)
{
if (i == index && this._dictionary.TryGetValue(key, out collection))
return collection;
i++;
}
return collection;
}
}
//取得對應索引鍵的驗證項目集合
public VaildateMemberCollection this[string key]
{
get
{
VaildateMemberCollection collection;
if (!_dictionary.TryGetValue(key, out collection))
{
collection = new VaildateMemberCollection();
_dictionary.Add(key, collection);
}
return collection;
}
set
{
VaildateMemberCollection collection;
if (_dictionary.TryGetValue(key, out collection))
collection = value;
else
{
collection = new VaildateMemberCollection();
_dictionary.Add(key, collection);
}
}
}
//移除對應索引鍵的所有項目
public void Remove(string key)
{
if (this._dictionary.ContainsKey(key))
this._dictionary.Remove(key);
}
//對應索引鍵的所有項目執行驗證動作
public bool Vaild(string key)
{
VaildateMemberCollection collection = this[key];
if (collection != null)
{
string msg = collection.ToString();
if (!String.IsNullOrEmpty(msg))
{
MessageBox.Show(msg); //目前是直接顯示訊息,可以有很多做法例如加入事件,之後再利用事件去取得結果資訊
return false;
}
}
return true;
}
//對應索引數值的所有項目執行驗證動作
public bool Vaild(int index)
{
VaildateMemberCollection collection = this[index];
if (collection != null)
{
string msg = collection.ToString();
if (!String.IsNullOrEmpty(msg))
{
MessageBox.Show(msg);
return false;
}
}
return true;
}
}
//驗證項目集合
public class VaildateMemberCollection : List
{
//取得驗證未通過項目的數量
public int UnVaildCount
{
get
{
int result = 0;
this.ForEach(member =>
{
if (member.Vaildator != null && !member.Vaildator.Invoke())
result++;
});
return result;
}
}
//傳回驗證未通過項目訊息組合後的字串
public override string ToString()
{
StringBuilder sb = new StringBuilder();
this.ForEach(member =>
{
if (member.Vaildator != null && !member.Vaildator.Invoke())
sb.AppendLine(member.Message);
});
return sb.ToString();
}
}
//驗證的項目資訊
public class VaildateMember
{
public VaildateHandler Vaildator { get; set; }
public string Message { get; set; }
}
而大略的概念就是將要驗證的邏輯放進會傳回布林值的委派型別屬性(筆者雖然會寫但是不太會解釋原理@@,如果看不懂請見諒) 下面是使用範例:
1.初始化與配置物件
private VaildateManager _vmanager;
private void initVaildateManager()
{
//初始化物件
_vmanager = new VaildateManager();
//--加入索引鍵名稱為V1的成員
VaildateMember vm = new VaildateMember();
vm.Vaildator = new VaildateHandler(() => { return !String.IsNullOrEmpty(this.tbPNR.Text); });
vm.Message = "請輸入電腦代碼.";
_vmanager["V1"].Add(vm);
vm = new VaildateMember();
vm.Vaildator = new VaildateHandler(() => { return (!String.IsNullOrEmpty(this.tbFirstName.Text) && !String.IsNullOrEmpty(this.tbLastName.Text)); });
vm.Message = "請輸入完整姓名.";
_vmanager["V1"].Add(vm);
//----------------------------------
//--加入索引鍵名稱為V2的成員
vm = new VaildateMember();
vm.Vaildator = new VaildateHandler(() => { return !String.IsNullOrEmpty(this.tbCardId.Text); });
vm.Message = "請輸入會員卡號.";
_vmanager["V2"].Add(vm);
vm = new VaildateMember();
vm.Vaildator = new VaildateHandler(() => { return !String.IsNullOrEmpty(this.pbPassword.Password); });
vm.Message = "請輸入密碼.";
_vmanager["V2"].Add(vm);
//----------------------------------
}
2.執行驗證
if (mode == "V1" && _vmanager.Vaild("V1")) {
//通過驗證後執行動作
......
}
else {
//未通過驗證後執行動作
......
}
if (mode == "V2" && _vmanager.Vaild("V2")) {
......
}
else {
//未通過驗證後執行動作
......
}