1:验证

添加DLL

Microsoft.Practices.EnterpriseLibrary.Validation

System.ComponentModel.DataAnnotations

添加命名空间:

using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Validation;

加验证

public class Customer{[StringLengthValidator(1, 25)]public string FirstName { get; set; }[StringLengthValidator(1, 25)]public string LastName { get; set; }[RegexValidator(@"^\d\d\d-\d\d-\d\d\d\d$")]public string SSN { get; set; }public Address Address { get; set; }}

  

  public class Address{[StringLengthValidator(1, 50)]public string StreetAddress { get; set; }[ValidatorComposition(CompositionType.And)][StringLengthValidator(1, 30)][ContainsCharactersValidator("sea", ContainsCharacters.All)]public string City { get; set; }[StringLengthValidator(2, 2)]public string State { get; set; }[RegexValidator(@"^\d{5}$")]public string ZipCode { get; set; }}

  

主项目加添DLL

Microsoft.Practices.ServiceLocation

Microsoft.Practices.EnterpriseLibrary.Common

Microsoft.Practices.EnterpriseLibrary.Validation

添加命名空间:

using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

代码:

private Validator<Customer> customerValidator;private void MainForm_Load(object sender, EventArgs e){ValidatorFactory valFactory = EnterpriseLibraryContainer.Current.GetInstance<ValidatorFactory>();customerValidator = valFactory.CreateValidator<Customer>();}private void acceptButton_Click(object sender, EventArgs e){Customer customer = new Customer{FirstName = firstNameTextBox.Text,LastName = lastNameTextBox.Text,SSN = ssnTextBox.Text,Address = new Address{StreetAddress = streetAddressTextBox.Text,City = cityTextBox.Text,State = stateComboBox.Text,ZipCode = zipCodeTextBox.Text}};       ValidationResults results = customerValidator.Validate(customer);if (!results.IsValid){MessageBox.Show(this,"Customer is not valid","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);return;}MessageBox.Show(this,"Processing customer '" + customer.FirstName + "'","Working",MessageBoxButtons.OK,MessageBoxIcon.Information);}

或
 if (!results.IsValid){StringBuilder builder = new StringBuilder();builder.AppendLine("Customer is not valid:");foreach (ValidationResult result in results){builder.AppendLine(string.Format("{0}: {1}", result.Key, result.Message));}MessageBox.Show(this,builder.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);return;}MessageBox.Show(this,"Processing customer '" + customer.FirstName + "'","Working",MessageBoxButtons.OK,MessageBoxIcon.Information);

  或用资源文件

  

 [StringLengthValidator(1, 25, MessageTemplateResourceType = typeof(Resources), MessageTemplateResourceName = "FirstNameMessage")] public string FirstName { get; set; } [StringLengthValidator(1, 25, MessageTemplateResourceType = typeof(Resources), MessageTemplateResourceName = "LastNameMessage")] public string LastName { get; set; } [RegexValidator(@"^\d\d\d-\d\d-\d\d\d\d$", MessageTemplateResourceType = typeof(Resources), MessageTemplateResourceName = "SSNMessage")]

  使用默认的消息验证

转载于:https://www.cnblogs.com/xiaofengfeng/p/3139885.html

学习微软企业库的心得-验证相关推荐

  1. [EntLib]微软企业库5.0 学习之路——第五步、介绍EntLib.Validation模块信息、验证器的实现层级及内置的各种验证器的使用方法——上篇...

    本文是为后面的学习之路做铺垫,简单介绍下企业库中的Validation模块的一些相关知识,包括Validation模块的简介.用途.使用方法.默认提供的多种验证器的介绍等. 一.简介及用途 在实际的项 ...

  2. [EntLib]微软企业库5.0 学习之路——第七步、Cryptographer加密模块简单分析、自定义加密接口及使用—上篇...

    在完成了后,今天开始介绍企业库中的新模块:Cryptographer(加密模块),这个模块在日常的大多数项目的作用非常重要,例如:网站会员密码.身份证号.网站配置等,通过对信息进行加密可以保证项目数据 ...

  3. (转)[EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART2——了解Unity的使用方法(1)...

    原文地址:http://www.cnblogs.com/kyo-yo/archive/2010/11/01/Learning-EntLib-Tenth-Decoupling-Your-System-U ...

  4. [EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART5——使用Unity自身的拦截器...

    在前一篇文章中,介绍了如何使用Unity来接管PIAB进行拦截操作,而Unity自身也提供了类似于ICallHandler的拦截处理程序--IInterceptionBehavior,今天这篇文章就是 ...

  5. 让微软企业库中的Email Trace Listener使用需要身份验证的SMTP服务器

    微软企业库中的日志记录模块中有个Email Trace Listener.对于将网站部署到异地的应用来讲,这是一个非常有效的功能.因为我们可以通过电子邮件查看日志,进而了解我们开发的程序错误出现在何处 ...

  6. 使用Microsoft EnterpriseLibrary(微软企业库)日志组件把系统日志写入数据库和xml文件...

    这里只是说明在项目中如何配置使用微软企业库的日志组件,对数据库方面的配置请参考其他资料. 1.在项目中添加Microsoft.Practices.EnterpriseLibrary.Data.dll. ...

  7. 基于微软企业库的AOP组件(含源码)

    软件开发,离不开对日志的操作.日志可以帮助我们查找和检测问题,比较传统的日志是在方法执行前或后,手动调用日志代码保存.但自从AOP出现后,我们就可以避免这种繁琐但又必须要实现的方式.本文是在微软企业库 ...

  8. 微软企业库mysql分页存储_使用微软企业库,非分页sql语句得到分页数据方法

    最近使用微软企业库,在做分页绑定的时候发现好象还没有多少通用的方法.为了灵活和换数据库,我不能使用存储过程.为了开发速度最快.我就是用datagrid绑定了. 以前长用的 DataAdapter.Fi ...

  9. 微软企业库(Microsoft Enterprise Library Data Access Block)

    1. Dynamic-link library Microsoft.Practices.ObjectBuilder.dll Microsoft.Practices.EnterpriseLibrary. ...

最新文章

  1. 谷歌社交平台新漏洞威胁千万用户隐私 将提早关闭
  2. 【OGG】OGG的单向复制配置-支持DDL(二)
  3. BLE-NRF51822-实现简单扫描器
  4. Bootstrap~多级导航(级联导航)的实现
  5. LeetCode MySQL 1045. 买下所有产品的客户
  6. JavaBean和Servlet
  7. Java至尊红颜_智能手机跨时代大作 摩托MPx220登场
  8. pandas—显示行索引与列索引(数组或者列表)
  9. C++餐厅订餐管理系统
  10. MySQL和SQLyog安装配置教程
  11. 日记1-时间序列协整进步
  12. 正则表达式过滤非数字字符串
  13. HyperLedger Fabric ChainCode开发——shim.ChaincodeStubInterface用法
  14. 基于单片机的RFID刷卡门禁电路设计(#0206)
  15. 推荐系统——Evaluation criteria(评估准则)
  16. 苹果系统安装虚拟机 Mac如何安装虚拟机教程
  17. Spring MVC 拦截器执行流程
  18. 关于lichong_87的外键级联更新-延迟检查和触发器的实现一点补充
  19. 王朝落日,读《万历十五年》
  20. 杰理AC692X 软开关机的实现

热门文章

  1. 使用Disentangling形式的损失函数回归2D和3D目标框
  2. CVPR2020 夜间目标检测挑战赛冠军方案解读
  3. 【项目合作】瓷砖表面打印缺陷识别
  4. 谷歌视频架构搜索:从 EvaNet 到 TinyVideoNet
  5. 初级java程序员怎样快速提升自己
  6. 无意中发现的一份清华大佬的刷题笔记!
  7. 2018年90后薪资报告出炉:你在哪个级别???
  8. 热传导/物质扩散算法应用于推荐
  9. 非局部均值(Nonlocal-Mean)
  10. oracle表与表之间更新,Oracle 两个表之间更新的实现