快速搜索
主页 > ag亚游vip|HOME > asp.netag怎么试玩|平台 >

.NET验证组件Fluent Validation使用指南

时间:2014-11-28 16:13来源:网络整理 作者:网络 点击:
分享到:
本文介绍了.NET验证组件Fluent Validation,并通过实例告诉我们这个验证组件的使用方法,以及与asp.net mvc验证库的整合方案,这里推荐给有相同需求的小伙伴

认识Fluent Vaidation.

  看到NopCommerce项目中用到这个组建是如此的简单,将数据验证从业务实体类中分离出来,真是一个天才的想法,后来才知道这个东西是一个开源的轻量级验证组建。
?
  Fluent Validation 翻译为:流畅验证
?
ag亚游vip|HOME   开源Codeplex其主页简介:该组件是一个轻量级的.NET类库,使用流畅的接口定义和lambda表达式为构建一个业务类的验证规则(A small validation library for .NET that uses a fluent interface and lambda expression for building validation rules for you business objects.)
?
  这个类库不仅仅可以使用的asp.net mvc项目中,普通的类库中也可以使用,当然在asp.net form项目中也支持。

怎么使用:  
  是不是好用,还要看使用时是否真的像其官网建议描述一样。我比较喜欢其官网上的例子,一眼就能看出用法上的感觉,绝对是如其名,流畅,这个也一种解释型语言常见的的一种用法,无限的对一个类型支持无限度个属性扩展。

业务实体类:
?

复制代码 代码如下:

?public class Person
?{
???? public string NameField;
???? public int Id { get; set; }
???? public string Surname { get; set; }
???? public string Forename { get; set; }
???? public List Children { get; set; }
???? public string[] NickNames { get; set; }
???? public DateTime DateOfBirth { get; set; }
???? public int? NullableInt { get; set; }
???? public Person()
???? {
???????? Children = new List();
???????? Orders = new List();
???? }
???? public int CalculateSalary()
???? {
???????? return 20;
???? }
???? public Address Address { get; set; }
???? public IList Orders { get; set; }
???? public string Email { get; set; }
???? public decimal Discount { get; set; }
???? public double Age { get; set; }
???? public int AnotherInt { get; set; }
???? public string CreditCard { get; set; }
???? public int? OtherNullableInt { get; set; }
?}
?public interface IAddress
?{
???? string Line1 { get; set; }
???? string Line2 { get; set; }
???? string Town { get; set; }
???? string County { get; set; }
???? string Postcode { get; set; }
???? Country Country { get; set; }
?}
?public class Address : IAddress
?{
???? public string Line1 { get; set; }
???? public string Line2 { get; set; }
???? public string Town { get; set; }
???? public string County { get; set; }
???? public string Postcode { get; set; }
???? public Country Country { get; set; }
???? public int Id { get; set; }
?}
?public class Country
?{
???? public string Name { get; set; }
?}
?public interface IOrder
?{
???? decimal Amount { get; }
?}
?public class Order : IOrder
?{
???? public string ProductName { get; set; }
???? public decimal Amount { get; set; }
?}

对Person的指定验证规则:  
?

复制代码 代码如下:

?using FluentValidation;
?public class CustomerValidator: AbstractValidator
?{
?? public CustomerValidator()
?? {
???? RuleFor(customer => customer.Surname).NotEmpty();
???? RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name");
???? RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount);
???? RuleFor(customer => customer.Address).Length(20, 250);
???? RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
?? }
?? private bool BeAValidPostcode(string postcode)
?? {
???? // custom postcode validating logic goes here
?? }
?}
?// 手动验证规则
?Customer customer = new Customer();
?CustomerValidator validator = new CustomerValidator();
?ValidationResult results = validator.Validate(customer);
?bool validationSucceeded = results.IsValid;
?IList failures = results.Errors;
精彩图集

赞助商链接