2010-08-03 43 views
9

一邊看着Shrinkr的源代碼(我們所有的評論其他項目的源代碼來學習吧??? :))我注意到以下KEWL代碼..(由我略,下同)任何.NET Fluent參數在那裏檢查庫?

public virtual Foo Foo 
{ 
    get; 
    set 
    { 
     Check.Argument.IsNotNull(value, "value"); 
     // then do something. 
    } 
} 

注意流利的他們檢查參數的方式?尼斯:)

alt text http://cherrythian.com/images/borat.jpg

所以..檢查代碼,他們有一些自定義類,這是否......

public static class Check 
{ 
    public static class Argument 
    { 
     public static void IsNotNull(object parameter, 
            string parameterName) 
     { ... } 

     public static void IsNotNullOrEmpty(string parameter, 
              string parameterName) 
     { ... } 

.... etc .... 
} 

是否有任何共同的框架在那裏?

gem install netFluentCheck

:)

回答

6

我結束了使用CuttingEdge Conditions,Codeplex上找到。

例如。

// Check all preconditions: 
Condition.Requires(id, "id") 
    .IsNotNull()   // throws ArgumentNullException on failure 
    .IsInRange(1, 999) // ArgumentOutOfRangeException on failure 
    .IsNotEqualTo(128); // throws ArgumentException on failure 

好:)

+3

CuttingEdge.Conditions是shizzle ;-) – Steven 2010-09-19 16:28:39

1

Here's one使用表達式。因爲它是非常容易的,每個人似乎都有自己的執行本...

+0

您的鏈接已死亡。 – 2015-01-24 00:53:49

+4

@ M.Babcock嘗試archive.org。我的答案是4歲以上。你不能認真地期望我自2008年以來保持所有1600+以上的答案。 – 2015-01-24 07:57:51

1
+0

FluentValidation用於驗證對象而不用於參數驗證 – BoeseB 2015-03-03 09:14:12

+2

我使用FluentValidation驗證ASP.NET MVC Action Arguments,它是對象if該模型是強類型的。會有很多選擇。它只是其中一種可用的工具,所以我只是認爲我會把它放在那裏以防萬一有人幫助。 – 2015-03-03 19:03:42

1

這裏有一個簡單的類只需要幾行代碼,我寫了一段時間前(從這裏開始:http://code.google.com/p/hotwire-queue/wiki/QuickAssert),做類似的流暢驗證的東西,使用了稍微不同的風格,我發現有點容易閱讀(ymmv)。不需要任何第三方庫,如果驗證失敗,您將得到一個簡單的錯誤消息,其中包含失敗的確切代碼。

config.Active.Should().BeTrue(); 
config.RootServiceName.Should().Be("test-animals"); 
config.MethodValidation.Should().Be(MethodValidation.afterUriValidation); 
var endpoints = config.Endpoints; 
endpoints.Should().NotBeNull().And.HaveCount(2); 

這樣:

config.Ensure(c => c.Active, 
       c => c.RootServiceName == "test-animals", 
       c => c.MethodValidation == MethodValidation.afterUriValidation, 
       c => c.Endpoints != null && c.Endpoints.Count() == 2); 

這裏的類,希望這是一個起點,有人;-D

using System; 
using System.Linq.Expressions; 
using NUnit.Framework; 

namespace Icodeon.Hotwire.Tests.Framework 
{ 
    public static class QuickAssert 
    { 
     public static void Ensure<TSource>(this TSource source, params Expression<Func<TSource, bool>>[] actions) 
     { 
      foreach (var expression in actions) 
      { 
       Ensure(source,expression); 
      } 
     } 

     public static void Ensure<TSource>(this TSource source, Expression<Func<TSource, bool>> action) 
     { 
      var propertyCaller = action.Compile(); 
      bool result = propertyCaller(source); 
      if (result) return; 
      Assert.Fail("Property check failed -> " + action.ToString()); 
     } 
    } 
} 

在我寫確保時間很有幫助,代碼合約均不支持Visual Studio 2010,但現在,請參閱http://msdn.microsoft.com/en-us/magazine/hh148151.aspx

1

您可以嘗試Bytes2you.ValidationProject)。它是快速,可擴展,直觀和易於使用的C#庫,爲參數驗證提供了流暢的API。提供在.NET應用程序中實施防禦性編程所需的一切。