学习微软企业库的心得-验证
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")]
使用默认的消息验证