有没有任何.NET流畅的参数检查库?

9

在查看Shrinkr的源代码时(我们都会审查其他项目的源代码来学习,对吧???:)),我注意到了以下很酷的代码...(下面是我缩写的内容)

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

注�他们检查�数的�畅方�,很好�。 alt text
(��:cherrythian.com) 所以...检查代�时,他们有一些自定义类���这个...
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 ?

:)

5个回答

6
我最终使用了在Codeplex上找到的CuttingEdge Conditions
例如。
// 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 是很棒的东西 ;-) - Steven
1
它已经不再维护了。我可以建议使用Guard吗?免责声明:我是作者。 - Şafak Gür
1
我很久以前就不再使用CE.C了(哈哈,我的帖子是十年前的!)。我最终转向了Shouldly。 - Pure.Krome

2

FluentValidation是用于对象验证而不是参数验证的。 - BoeseB
2
我使用FluentValidation来验证ASP.NET MVC Action参数,如果模型是强类型的,则这些参数将是对象。有许多选项可供选择。它只是可用工具之一,所以我想提供这个信息,以便帮助其他人。 - Daniel Dyson

1
这是我一段时间前写的一个简单类,只有几行代码(来自这里:http://code.google.com/p/hotwire-queue/wiki/QuickAssert),它做了类似于流畅验证的事情,使用了我发现更容易阅读的略微不同的风格(可能因人而异)。不需要任何第三方库,如果验证失败,您将获得一个带有失败代码的简单错误消息。
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());
        }
    }
}

在我编写Ensure时,Visual Studio 2010不支持代码合同,但现在已经支持了,请参见http://msdn.microsoft.com/en-us/magazine/hh148151.aspx


1
你可以尝试 Bytes2you.Validation项目)。它是一个快速,可扩展,直观且易于使用的C#库,提供流畅的API来进行参数验证。提供了在.NET应用程序中实现防御性编程所需的一切。

1

这里有一个使用表达式的例子。由于它相当简单,每个人似乎都有自己的实现方法...


4
尝试使用archive.org。我的回答已经超过4年了,你不能指望我在这里维护我自2008年以来所回答的1600多个问题。 - Mauricio Scheffer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接