Any .NET Fluent Argument checking libraries out there? [closed]
Asked Answered
T

5

9

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 ?

:)

Toandfro answered 3/8, 2010 at 2:15 Comment(0)
T
6

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

Toandfro answered 2/9, 2010 at 0:8 Comment(3)
CuttingEdge.Conditions is the shizzle ;-)Napper
It's no longer maintained. May I suggest Guard? Disclaimer: I'm the author.Herl
I've also moved off CE.C looong ago (lol, my post was from a decade ago!). I ended up moving to Shouldly.Toandfro
S
2

Try FluentValidation

Or FluentValidation for .NET 2.0

Syllabub answered 1/9, 2010 at 16:1 Comment(2)
FluentValidation is made for validation of objects and not for argument validationMazonson
I use FluentValidation for validation of ASP.NET MVC Action Arguments, which are objects if the model is strongly typed. There will be many options. It is just one of the tools available so I just thought I'd put it out there in case it helps someone.Syllabub
I
1

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

Indehiscent answered 3/8, 2010 at 2:21 Comment(1)
@M.Babcock Try archive.org. My answer is over 4 years old. You can't seriously expect me to maintain all of my 1600+ answers here since 2008.Indehiscent
E
1

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

Enwomb answered 4/1, 2013 at 11:26 Comment(0)
T
1

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.

Twelvetone answered 17/11, 2014 at 21:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.