Question

while looking at Shrinkr's source code (we all review other project's source code to learn, right??? :) ) I noticed the following kewl code .. (abbreviated by me, below)

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

Notice the fluent way they check for arguments? Nice :)

alt text
(source: cherrythian.com)

So .. checking the code, they have some custom class that does this...

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 ....
}

Are there any common frameworks out there?

gem install netFluentCheck ?

:)

Was it helpful?

Solution

I ended up using CuttingEdge Conditions, found on Codeplex.

eg.

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

nice :)

OTHER TIPS

Here's a simple class only a few lines long that I wrote a while ago ( from here : http://code.google.com/p/hotwire-queue/wiki/QuickAssert) that does something similar to fluent validation, uses a slightly different style that I find a bit easier to read (ymmv). Doesn't require any third party libraries, and if the validation fails, you get a simple error message with the exact code that failed.

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);

to this:

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

Here's the class, hope it's helpful as a starting point for someone ;-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());
        }
    }
}

At the time I wrote Ensure, code contracts were not supported in Visual studio 2010, but are now, see http://msdn.microsoft.com/en-us/magazine/hh148151.aspx

You can try Bytes2you.Validation (Project). It is fast, extensible, intuitive and easy-to-use C# library providing fluent APIs for argument validation. Gives everything you need to implement defensive programming in your .NET application.

Here's one that uses Expressions. Since it's pretty trivial, everyone seems to have their own implementation of this...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top