views:

56

answers:

4

Hi folks,

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

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 ?

:)

A: 

If you're using .NET 4.0, Code Contracts are now a part of the CLR:

http://msdn.microsoft.com/en-us/magazine/ee236408.aspx

It's probably not quite as fluent (or concise) as what you're after though.

Bennor McCarthy
A: 

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

Mauricio Scheffer
A: 

Try Fluentalidation

Or FluentValidation for .NET 2.0

Daniel Dyson
+1  A: 

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

Pure.Krome
CuttingEdge.Conditions is the shizzle ;-)
Steven