自己写的ValidatorBlock,参考Enterprise Library的ValidatorBlock

因为项目的原因,不能使用3.0的validator block,感觉很是不爽,于是决定参考官方实现,自己写一个。
其实思路还是比较简单,一个attribute类,一个提供验证的方法类,就搞定。不废话,贴代码先!

attribute类是这么写的
  [AttributeUsage(AttributeTargets.Property)]
    public sealed class RequiredValueAttribute : System.Attribute
    {
        public RequiredValueAttribute()
        { }

        public RequiredValueAttribute(string alertMessage)
        {
            this.alertMessage = alertMessage;
        }

        public RequiredValueAttribute(string alertMessage, bool isRequired)
        {
            this.alertMessage = alertMessage;
            this.isRequired = isRequired;
        }

        private string alertMessage;
        public string AlertMessage
        {
            get { return alertMessage; }
            set { alertMessage = value; }
        }

        private bool isRequired;
        public bool IsRequired
        {
            get { return isRequired; }
            set { isRequired = value; }
        }

        public ValidateResult validate(object value, string alertMessage, string requiredValueName)
        {
            ValidateResult result = new ValidateResult();
            if (value == null || value.ToString() == "")
            {
                result.Result = false;
                result.MessageResult = alertMessage;
                result.RequiredValueName = requiredValueName;
            }
            else
                result.Result = true;
            
            return result;
        }
    }


方法类是这么写的
public class Validator<T>
    {
        public Validator()
        {
        }

        public List<ValidateResult> DoValidate(T objectToValidate)
        {
            List<ValidateResult> resultList = new List<ValidateResult>();

            Type objType = objectToValidate.GetType();

            PropertyInfo[] propInfoList = objType.GetProperties();//refelection all the prop

            foreach (PropertyInfo prop in propInfoList)
            {
                RequiredValueAttribute attribute = Attribute.GetCustomAttribute(prop, typeof(RequiredValueAttribute)) as RequiredValueAttribute;
                if (attribute != null)
                {
                    object value = prop.GetValue(objectToValidate, null);
                    ValidateResult result = attribute.validate(value, attribute.AlertMessage, prop.Name);
                    resultList.Add(result);
                }
            }
            return resultList;
        }
    }

还有一个返回结果的类
public class ValidateResult
    {
        
private bool result;
        
public bool Result
        {
            
get { return result; }
            
set { result = value; }
        }

        
private string requiredValueName;
        
public string RequiredValueName
        {
            
get { return requiredValueName; }
            
set { requiredValueName = value; }
        }

        
private string messageResult;
        
public string MessageResult
        {
            
get { return messageResult; }
            
set { messageResult = value; }
        }
    }

假设我们有这个一个属性类
public class User
    {
        
private string userId;
        
        
public string UserId
        {
            
get { return userId; }
            
set { userId = value; }
        }

        
private string userName;
        [RequiredValueAttribute(
"用户名称必须填写")] //add the attribute
        
public string UserName
        {
            
get { return userName; }
            
set { userName = value; }
        }

        
private string password;
        [RequiredValueAttribute(
"密码必须填写")] //add the attribute
        public string Password
        {
            
get { return password; }
            
set { password = value; }
        }

        
private string email;
        [RequiredValueAttribute(
"邮件必须填写")] //add the attribute
        public string Email
        {
            
get { return email; }
            
set { email = value; }
        }
    }

那么我们在需要验证的时候就可以这么操作
private void button1_Click(object sender, EventArgs e)
        {
            User user 
= new User();

            user.UserId 
= Guid.NewGuid().ToString();
            user.UserName 
= this.mUserNameTextBox.Text;
            user.Password 
= this.mPasswordTextBox.Text;
            user.Email 
= this.mEmailTextBox.Text;

            List
<ValidateResult> resultList = new Validator<User>().DoValidate(user);

            
foreach (ValidateResult result in resultList)
            {
                
if (!result.Result)
                {
                    MessageBox.Show(result.RequiredValueName 
+ ":" + result.MessageResult);
                    
return;
                }
            }
        }

ok,一切搞定,如果保存的时候发现有requiredvalue标签的属性为空的话,就会messagebox报错!
本文仅针对实体中的属性的校验,而且仅支持attribute一种写法!


posted on 2007-06-18 21:26  xiao_p  阅读(345)  评论(1编辑  收藏

导航