zoukankan      html  css  js  c++  java
  • 学习微软企业库的心得-验证

    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(@"^ddd-dd-dddd$")]
            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(@"^ddd-dd-dddd$", 
                MessageTemplateResourceType = typeof(Resources), 
                MessageTemplateResourceName = "SSNMessage")] 
    

      使用默认的消息验证

  • 相关阅读:
    数据库知识扩展
    数据库遇到的问题——mysql在线修改表结构大数据表的风险与解决办法归纳
    数据库遇到的问题——事务操作时中断导致锁表
    java中常见的内存泄露的例子
    数据库设计中常见表结构分析
    索引失效的7种情况
    DWZ 自定义异常及后台校验2-a链接弹出框
    DWZ 自定义异常及后台校验
    Excl中的数据转换成SQL脚本(含日期写法)
    SpringAop (二)
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/3139885.html
Copyright © 2011-2022 走看看