MVC中的统一验证机制~终极了(自己的改良版)
本版本延续MVC中的统一验证机制~续的这篇文章,本篇主要是对验证基类的扩展和改善
1 namespace Web.Mvc.Extensions 2 { 3 4 #region 验证基类 5 /// <summary> 6 /// 通用验证基类 7 /// </summary> 8 public abstract class EntityValidationAttribute : ValidationAttribute 9 { 10 #region Constructors 11 public EntityValidationAttribute(MessageType messageId, params object[] args) : 12 base(() => MessageManager.Current.GetMessage(messageId, args)) { } 13 #endregion 14 15 #region Protected Properties 16 protected virtual Regex rLetters { get { return new Regex("[a-zA-Z]{1,}"); } } 17 /// <summary> 18 /// 验证数字 19 /// 子类可以根据自己的逻辑去重写 20 /// </summary> 21 protected virtual Regex rDigit { get { return new Regex("[0-9]{1,}"); } } 22 /// <summary> 23 /// 验证邮编 24 /// 子类可以根据自己的逻辑去重写 25 /// </summary> 26 protected virtual Regex rPostNumber { get { return new Regex("^[0-9]{3,14}$"); } } 27 /// <summary> 28 /// 验证手机 29 /// 子类可以根据自己的逻辑去重写 30 /// </summary> 31 protected virtual Regex rMobile { get { return new Regex(@"^1[3|4|5|8][0-9]\d{8}$"); } } 32 /// <summary> 33 /// 验证电话 34 /// 子类可以根据自己的逻辑去重写 35 /// </summary> 36 protected virtual Regex rTelePhone { get { return new Regex(@"^[0-9]{2,4}-\d{6,8}$"); } } 37 /// <summary> 38 /// 验证传真 39 /// 子类可以根据自己的逻辑去重写 40 /// </summary> 41 protected virtual Regex rFex { get { return new Regex(@"/^[0-9]{2,4}-\d{6,8}$"); } } 42 /// <summary> 43 /// 验证Email 44 /// 子类可以根据自己的逻辑去重写 45 /// </summary> 46 protected virtual Regex rEmail { get { return new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } } 47 #endregion 48 49 } 50 #endregion 51 52 #region 具体验证模块 53 /// <summary> 54 /// 为空验证 55 /// </summary> 56 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] 57 public class RequiredAttribute : EntityValidationAttribute 58 { 59 public bool AllowEmptyStrings { get; set; } 60 public RequiredAttribute(MessageType messageType, params object[] args) : 61 base(messageType, args) 62 { } 63 public override bool IsValid(object value) 64 { 65 return new System.ComponentModel.DataAnnotations.RequiredAttribute { AllowEmptyStrings = this.AllowEmptyStrings }.IsValid(value); 66 } 67 } 68 /// <summary> 69 /// 范围验证 70 /// </summary> 71 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] 72 public class RangeAttribute : EntityValidationAttribute 73 { 74 private System.ComponentModel.DataAnnotations.RangeAttribute innerRangeAttribute; 75 76 public RangeAttribute(double minimum, double maximum, MessageType messageType, params object[] args) : 77 base(messageType, args) 78 { 79 innerRangeAttribute = new System.ComponentModel.DataAnnotations.RangeAttribute(minimum, maximum); 80 } 81 82 public RangeAttribute(int minimum, int maximum, MessageType messageType, params object[] args) : 83 base(messageType, args) 84 { 85 innerRangeAttribute = new System.ComponentModel.DataAnnotations.RangeAttribute(minimum, maximum); 86 } 87 88 public RangeAttribute(Type type, string minimum, string maximum, MessageType messageType, params object[] args) : 89 base(messageType, args) 90 { 91 innerRangeAttribute = new System.ComponentModel.DataAnnotations.RangeAttribute(type, minimum, maximum); 92 } 93 94 public override bool IsValid(object value) 95 { 96 return innerRangeAttribute.IsValid(value); 97 } 98 } 99 100 /// <summary> 101 /// Email验证 102 /// </summary> 103 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] 104 public class EmailAttribute : EntityValidationAttribute 105 { 106 public EmailAttribute(MessageType messageType, params object[] args) : 107 base(messageType, args) { } 108 public override bool IsValid(object value) 109 { 110 if (value == null) 111 return false; 112 else 113 return rEmail.IsMatch(value.ToString()); 114 } 115 } 116 117 /// <summary> 118 /// 数值验证 119 /// </summary> 120 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] 121 public class DigitAttribute : EntityValidationAttribute 122 { 123 public DigitAttribute(MessageType messageType, params object[] args) : 124 base(messageType, args) { } 125 public override bool IsValid(object value) 126 { 127 if (value == null) 128 return false; 129 else 130 return rDigit.IsMatch(value.ToString()); 131 } 132 133 } 134 135 /// <summary> 136 /// 邮编验证 137 /// </summary> 138 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] 139 public class PostNumberAttribute : EntityValidationAttribute 140 { 141 public PostNumberAttribute(MessageType messageType, params object[] args) : 142 base(messageType, args) { } 143 public override bool IsValid(object value) 144 { 145 if (value == null) 146 return false; 147 else 148 return rPostNumber.IsMatch(value.ToString()); 149 } 150 151 } 152 153 /// <summary> 154 /// 手机验证 155 /// </summary> 156 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] 157 public class MobileAttribute : EntityValidationAttribute 158 { 159 public MobileAttribute(MessageType messageType, params object[] args) : 160 base(messageType, args) { } 161 public override bool IsValid(object value) 162 { 163 if (value == null) 164 return false; 165 else 166 return rMobile.IsMatch(value.ToString()); 167 } 168 } 169 170 /// <summary> 171 /// 电话验证 172 /// </summary> 173 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] 174 public class TelePhoneAttribute : EntityValidationAttribute 175 { 176 public TelePhoneAttribute(MessageType messageType, params object[] args) : 177 base(messageType, args) { } 178 public override bool IsValid(object value) 179 { 180 if (value == null) 181 return false; 182 else 183 return rTelePhone.IsMatch(value.ToString()); 184 } 185 } 186 187 /// <summary> 188 /// 传真验证 189 /// </summary> 190 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] 191 public class FexAttribute : EntityValidationAttribute 192 { 193 public FexAttribute(MessageType messageType, params object[] args) : 194 base(messageType, args) { } 195 public override bool IsValid(object value) 196 { 197 if (value == null) 198 return false; 199 else 200 return rFex.IsMatch(value.ToString()); 201 } 202 } 203 #endregion 204 205 #region 验证消息返回类 206 /// <summary> 207 /// 消息类 208 /// </summary> 209 public class MessageManager 210 { 211 static Dictionary<MessageType, string> messages = new Dictionary<MessageType, string>(); 212 static MessageManager() 213 { 214 messages.Add(MessageType.RequiredField, "这个 \"{0}\"是必填的!"); 215 messages.Add(MessageType.GreaterThan, "这个 \"{0}\" 的值必须大于 \"{1}\"!"); 216 messages.Add(MessageType.LessThan, "这个 \"{0}\" 的值必须小于 \"{1}\"!"); 217 messages.Add(MessageType.EmailField, "这个 \"{0}\" 不是有效的Email地址!"); 218 messages.Add(MessageType.DigitField, "这个 \"{0}\" 不是有效的数字!"); 219 messages.Add(MessageType.PostNumberField, "这个 \"{0}\" 不是有效的邮编!"); 220 messages.Add(MessageType.MobileField, "这个 \"{0}\" 不是有效的手机号码!"); 221 messages.Add(MessageType.TelePhoneField, "这个 \"{0}\" 不是有效的电话号码!"); 222 messages.Add(MessageType.FexField, "这个 \"{0}\" 不是有效的传真!"); 223 } 224 /// <summary> 225 /// 得到验证异常的消息集合 226 /// 对外公开 227 /// </summary> 228 /// <param name="messageType">异常消息ID</param> 229 /// <param name="args">消息参数集合</param> 230 /// <returns></returns> 231 public string GetMessage(MessageType messageType, params object[] args) 232 { 233 return string.Format(CultureInfo.CurrentCulture, messages[messageType], args); 234 } 235 /// <summary> 236 /// 本类的实例对象 237 /// </summary> 238 public static MessageManager Current = new MessageManager(); 239 } 240 241 242 243 #endregion 244 245 #region 验证类型枚举 246 /// <summary> 247 /// 验证消息类型 248 /// </summary> 249 public enum MessageType 250 { 251 /// <summary> 252 /// 为空验证 253 /// </summary> 254 RequiredField, 255 /// <summary> 256 /// 大于验证 257 /// </summary> 258 GreaterThan, 259 /// <summary> 260 /// 小于验证 261 /// </summary> 262 LessThan, 263 /// <summary> 264 /// 邮箱验证 265 /// </summary> 266 EmailField, 267 /// <summary> 268 /// 数字验证 269 /// </summary> 270 DigitField, 271 /// <summary> 272 /// 邮编验证 273 /// </summary> 274 PostNumberField, 275 /// <summary> 276 /// 手机验证 277 /// </summary> 278 MobileField, 279 /// <summary> 280 /// 电话验证 281 /// </summary> 282 TelePhoneField, 283 /// <summary> 284 /// 传真验证 285 /// </summary> 286 FexField, 287 } 288 #endregion 289 290 } 291 292 完整的实体为: 293 294 /// <summary> 295 /// 人类实体 296 /// </summary> 297 public class Person 298 { 299 /// <summary> 300 /// 姓名 301 /// </summary> 302 [DisplayName("姓名"), Required(MessageType.RequiredField, "Name")] 303 public string Name { get; set; } 304 305 /// <summary> 306 /// 年纪 307 /// </summary> 308 [DisplayName("年纪"), Range(18, int.MaxValue, MessageType.GreaterThan, "Age", 18)] 309 public int Age { get; set; } 310 311 /// <summary> 312 /// 体重 313 /// </summary> 314 [DisplayName("体重"), Range(int.MinValue, 160, MessageType.LessThan, "Weight", 160)] 315 public double Weight { get; set; } 316 317 /// <summary> 318 /// 电子邮件 319 /// </summary> 320 [DisplayName("电子邮件"), Email(MessageType.EmailField, "电子邮件")] 321 public string Email { get; set; } 322 323 /// <summary> 324 /// 手机 325 /// </summary> 326 [DisplayName("手机"), Mobile(MessageType.MobileField, "Mobile")] 327 public string Mobile { get; set; } 328 329 /// <summary> 330 /// 电话 331 /// </summary> 332 [DisplayName("电话"), TelePhone(MessageType.TelePhoneField, "TelePhone")] 333 public string TelePhone { get; set; } 334 335 /// <summary> 336 /// 邮编 337 /// </summary> 338 [DisplayName("邮编"), PostNumber(MessageType.PostNumberField, "PostNumber")] 339 public string PostNumber { get; set; } 340 341 /// <summary> 342 /// 传真 343 /// </summary> 344 [DisplayName("传真"), Fex(MessageType.FexField, "Fex")] 345 public string Fex { get; set; } 346 }
这就是面向对象的程序设计中对验证的统一,也算是一种抽象了。