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

      使用默认的消息验证

  • 相关阅读:
    ASP.NET安全问题-- 创建安全的Web应用程序
    浅谈ASP.NET内部机制(八)
    ASP.NET 配置文件纵横谈(一)
    项目开发-让设计模式成为一种心智
    浅谈ASP.NET内部机制(七)
    ASP.NET 配置文件纵横谈(二)
    GridView的分页是否真的是鸡肋呢?
    SQL开发中容易忽视的一些小地方(四)
    SQL开发中容易忽视的一些小地方( 三)
    怎样才能充分利用SQL索引
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/3139885.html
Copyright © 2011-2022 走看看