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")] 
    

      使用默认的消息验证

  • 相关阅读:
    P2604 [ZJOI2010]网络扩容
    P2053 [SCOI2007]修车
    P2045 方格取数加强版
    P4134 [BJOI2012]连连看
    P2153 [SDOI2009]晨跑
    P3381 【模板】最小费用最大流
    P3376 【模板】网络最大流
    P1326 足球
    2020牛客多校第八场I题 Interesting Computer Game(并查集+判环)
    Codeforces 1375D Replace by MEX(思维题)
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/3139885.html
Copyright © 2011-2022 走看看