<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          好用的驗證框架FluentValidation(上)

          共 3419字,需瀏覽 7分鐘

           ·

          2021-04-11 22:57


          把數(shù)據(jù)錯誤扼殺在早期,那就是在數(shù)據(jù)的入口處,一般數(shù)據(jù)都是打包成一個實體的方式進(jìn)傳遞,F(xiàn)luentValidation就以實體類為單位進(jìn)行屬性驗證的集合。

          Install-Package FluentValidation

          下面看一個例子吧。

          實體類:

          public class Person{    public int Id { get; set; }    public DateTime Birthday { get; set; }    public string IDCard { get; set; }    public string Name { get; set; }    public string Email { get; set; }    public PersonAddress Address { get; set; }    public string Tel { get; set; }}public class PersonAddress{    public string Country { get; set; }    public string Province { get; set; }    public string City { get; set; }    public string County { get; set; }    public string Address { get; set; }    public string Postcode { get; set; }}

          驗證實體類:

          /// <summary>/// Person驗證/// </summary>public class PersonValidator : AbstractValidator<Person>{    public PersonValidator()    {        RuleFor(p => p.Name).NotNull();        RuleFor(p => p.Email).NotNull().EmailAddress();        RuleFor(p => p.Birthday).NotNull();        RuleFor(p => p.IDCard)            .NotNull()            .When(p => (DateTime.Now > p.Birthday.AddYears(1)))            .WithMessage(p => $"出生日期為{p.Birthday},現(xiàn)在時間為{DateTime.Now},大于一歲,CardID值必填!");        RuleFor(p => p.Tel).NotNull().Matches(@"^(\d{3,4}-)?\d{6,8}$|^[1]+[3,4,5,8]+\d{9}$");        RuleFor(p => p.Address).NotNull();        RuleFor(p => p.Address).SetValidator(new PersonAddressValidator());    }}/// <summary>/// Person Address驗證/// </summary>public class PersonAddressValidator : AbstractValidator<PersonAddress>{    public PersonAddressValidator()    {        RuleFor(a => a.Country).NotNull();        RuleFor(a => a.Province).NotNull();        RuleFor(a => a.City).NotNull();        RuleFor(a => a.County).NotNull();        RuleFor(a => a.Address).NotNull();        RuleFor(a => a.Postcode).NotNull().Length(6);    }}

          使用場景:

          class Program{    static void Main(string[] args)    {        var person = new Person()        {            //少一位            Tel = "1345346711",            Name = "桂素偉",            //格式錯誤            Email = "axzxs2001#163.com",            //設(shè)置生日,沒有身份證            Birthday = DateTime.Parse("2020-03-28 00:00:00"),
          Address = new PersonAddress() { //郵編位數(shù)不對 Postcode = "12345" }, }; var validator = new PersonValidator(); var results = validator.Validate(person); if (!results.IsValid) { foreach (var failure in results.Errors) { Console.WriteLine("屬性 " + failure.PropertyName + " 驗證失?。? + failure.ErrorMessage); } } Console.WriteLine("--------------------------------------------------------------------"); Console.WriteLine(results.ToString("\r\n")); }}

          FluentValidation有一個很贊的功能,就是驗證某一屬性時,可以用別的屬性的值作為條件,組合實現(xiàn)驗證,這樣就能適應(yīng)更多的業(yè)務(wù)邏輯驗證場景。比如上例中的,只有大于一歲(Birthday)的人,身份證(IDCard)是必填項。當(dāng)然FluentValidation不只這些功能,比如嵌套實體驗證,組合驗證規(guī)則等,都是很貼心的功能,期待大家嘗試。


          往期精彩回顧




          【推薦】.NET Core開發(fā)實戰(zhàn)視頻課程 ★★★

          .NET Core實戰(zhàn)項目之CMS 第一章 入門篇-開篇及總體規(guī)劃

          【.NET Core微服務(wù)實戰(zhàn)-統(tǒng)一身份認(rèn)證】開篇及目錄索引

          Redis基本使用及百億數(shù)據(jù)量中的使用技巧分享(附視頻地址及觀看指南)

          .NET Core中的一個接口多種實現(xiàn)的依賴注入與動態(tài)選擇看這篇就夠了

          10個小技巧助您寫出高性能的ASP.NET Core代碼

          用abp vNext快速開發(fā)Quartz.NET定時任務(wù)管理界面

          在ASP.NET Core中創(chuàng)建基于Quartz.NET托管服務(wù)輕松實現(xiàn)作業(yè)調(diào)度

          現(xiàn)身說法:實際業(yè)務(wù)出發(fā)分析百億數(shù)據(jù)量下的多表查詢優(yōu)化

          關(guān)于C#異步編程你應(yīng)該了解的幾點建議

          C#異步編程看這篇就夠了


          瀏覽 60
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  韩国二区18 | 成人免费电影MV | 澳门操逼视频 | 2017亚洲天堂 | 欧美黄色成人影片下载大全 |