2012-01-05 95 views
9

要用於基於Web的mvc3 .net應用程序,您會推薦哪種驗證框架?應用程序遵循領域模型模式和領域模型POCO在單獨的類庫中?您對.NET項目推薦哪種驗證框架?

排序驗證,將被要求將... NOT NULL,基於正則表達式等

+0

您是否發現了不同框架的優缺點比較? – 2012-06-06 12:42:37

回答

18

我會FluentValidation去,這是一個真棒開源項目

https://github.com/JeremySkinner/FluentValidation

這是同樣適用於基本和更復雜的驗證

+0

spring.net怎麼樣? – InfoLearner 2012-01-05 22:54:54

+0

@KnowledgeSeeker對我來說似乎不必要的複雜..但我沒有用過真正的項目來給出一個有效的意見。我只能說FluentValidation看起來像是一個很好的解決方案,用於mvc項目 – 2012-01-05 23:02:58

+0

您是否有使用EntLib驗證應用程序塊的經驗?如果是,爲什麼你更喜歡FluentValidation? – 2012-06-28 21:32:35

3

如果您需要故障列表(而不是一次一個例外),那麼我喜歡Enterprise Library Validation塊。

查看PowerPoint演示文稿時: http://msdn.microsoft.com/en-us/library/ff650484.aspx

您可以連接最多最基本的驗證對你的POCO對象。 許多預生產規則可以在.config文件中設置。

你可以編寫自己的規則。

我的規則非常精細。他們一次執行1次驗證。

作爲一個簡單的例子:我會有兩個不同的規則來決定僱員是否可以僱傭(根據出生日期)。 一個規則可以確保員工的生日是特定的。
第二條規則將確保當前日期減去出生日期大於18年。 (或者任何規則)。

(現在讓我們假設我有一堆規則到位)。 因此,在驗證例程運行後,我得到列表中所有(無效)情況的列表。例如,如果我正在驗證僱員,我會得到一個殘疾人名單。

「沒有提供名字」

「沒有提供名字」

「沒有提供SSN」

,而不是 「一次一個」。 (這樣做「一次一個」可能需要多次才能最終弄清楚支票的有效性)。

下面是一些示例代碼。假設有人試圖購買ISBN「ABC123456」的書。

以下是一個自定義規則,用於檢查該書是否存在(例如在您的產品數據庫中)。我認爲你可以跟隨。它將與Book(.cs)poco對象連線。 (沒有顯示「接線」)。我只是試圖給你一個簡單的例子,說明如何創建一個簡單的規則(或不難)。

當找不到書(使用isbn)....然後您會看到validationResults.AddResult方法。這就是你如何得到多個殘疾人。稍後當您檢查驗證查詢時,您將可以訪問該集合。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 

using Microsoft.Practices.EnterpriseLibrary.Validation; 
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators; 


namespace MyCompany.Applications.MyApplication.BusinessLogic.Validation.MyType1Validations 
{ 
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] 
    public class BookExistsValidatorAttribute : ValidatorAttribute 
    { 
     protected override Validator DoCreateValidator(Type targetType) 
     { 
      return new BookExistsValidator("BookExistsValidatorTag"); 
     } 
    } 

    public class BookExistsValidator : Validator<string> 
    { 

     public BookExistsValidator(string tag) : base("BookExistsValidatorMessageTemplate", tag) { } 

     protected override string DefaultMessageTemplate 
     { 
      get { throw new NotImplementedException(); } 
     } 

     protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults) 
     { 

      bool bookExists = BookMatchExists(objectToValidate); 

      if (!bookExists) 
      { 
       string msg = string.Format("The Book does not exist. Your ISBN='{0}'", objectToValidate); 
       validationResults.AddResult(new ValidationResult(msg, currentTarget, key, 10001, this)); /* 10001 is just some number I made up */ 

      } 


     } 

     private bool BookMatchExists(string isbn) 
     { 
      bool returnValue = false; 

      IBookCollection coll = MyCompany.Applications.MyApplication.BusinessLogic.CachedControllers.BookController.FindAll(); /* Code not shown, but this would hit the db and return poco objects of books*/ 

      IBook foundBook = (from item in coll where item.ISBN.Equals(book, StringComparison.OrdinalIgnoreCase) select item).SingleOrDefault(); 

      if (null != foundBook) 
      { 
       returnValue = true; 
      } 
      return returnValue; 
     } 



    } 
} 
+0

你有沒有使用spring.net驗證框架? – InfoLearner 2012-01-05 22:42:20

+1

不,我沒有。我從EnterpriseLibrary開始,找出我可以在兩天內得到所需的東西。請參閱:http://stackoverflow.com/questions/3806447/spring-net-vs-enterprise-library和http://stackoverflow.com/questions/751700/which-validation-framework-to-choose-spring-validation-或者驗證申請,以獲得關於這兩者的更多評論。企業圖書館一直有很好的支持和文檔,所以多年來我一直堅持它。我已經在使用E.L.Data,所以使用驗證塊對我來說是一個簡單的過渡。 – granadaCoder 2012-01-06 16:45:14