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

      使用默认的消息验证

  • 相关阅读:
    java类中为什么设置set和get方法操作属性
    Java常用排序算法+程序员必须掌握的8大排序算法+二分法查找法
    自学Zabbix之路15.3 Zabbix数据库表结构简单解析-Triggers表、Applications表、 Mapplings表
    自学Zabbix之路15.2 Zabbix数据库表结构简单解析-Items表
    自学Zabbix之路15.1 Zabbix数据库表结构简单解析-Hosts表、Hosts_groups表、Interface表
    21 Zabbix系统性能优化建议
    20 Zabbix 利用Scripts栏目对Hosts远程执行命令
    19 Zabbix web监控实例
    18 Zabbix 新增map中的icon图标
    自学Zabbix3.12.6-动作Action-Escalations配置
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/3139885.html
Copyright © 2011-2022 走看看