日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

使用FluentValidation来进行数据有效性验证

發(fā)布時間:2025/3/20 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用FluentValidation来进行数据有效性验证 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

之前我介紹過了使用系統(tǒng)自帶的Data Annotations來進行數(shù)據(jù)有效性驗證,今天在CodePlex上逛的時候,發(fā)現(xiàn)了一個非常簡潔好用的庫:FluentValidation

由于非常簡潔,就直接拿官網(wǎng)的例子演示了:?

using FluentValidation;public class CustomerValidator : AbstractValidator<Customer>{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<ValidationFailure> failures = results.Errors;

它還可以非常方便的與Asp.Net集成,用起來非常方便。官網(wǎng)的幫助文檔也非常詳盡,有數(shù)據(jù)有效性檢驗的朋友趕緊用起來把。

?

總結

以上是生活随笔為你收集整理的使用FluentValidation来进行数据有效性验证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。