Recently I released Ensure.That v2.0.0, which is a simple guard clause project that simplifies and unifies your validation of arguments, for .Net4+, Silverlight, Windows Phone, WinRT.

It’s easy to get started. It’s distributed using NuGet and exists either as a dll or as a source package.

install-package ensure.that

or for the source distribution:

install-package ensure.that.source

What does it bring?

Simplified and unified argument validation.

public void Something(string myString) {
    Ensure.That(myString).IsNotNullOrEmpty();
    ...
}

If you want the name of the argument as the value for ArgumentException.ParamName, you pass it:

public void Something(string myString) {
    Ensure.That(myString, "myString").IsNotNullOrEmpty();
    ...
}

You can chain:

public void Something(string myString) {
    Ensure.That(myInt).IsGte(1).And().IsLte(10);

    //same as
    Ensure.That(myInt).IsGt(1).IsLt(10);

    //But could be simplified
    Ensure.That(myInt).IsInRange(1, 10);

    //You can assign the value to e.g. a property
    MyProperty = Ensure.That(myInt).Is(2).Value;
    ...
}

Wait, a magic string for the parameter name?

Yes, indeed. First. There’s an alternative so that you don’t need a magic string. Second. If you are, like me, a happer Visual Studio user cause of the daily use of ReSharper, I can tell you, it picks up those strings when you do e.g. name refactorings.

To get rid of the need of passing a string and instead use lambdas, you need to add an extenstion method as described here: https://github.com/danielwertheim/ensure.that/wiki/support-for-lambda-expressions

After that you can do:

public void Something(string myString) {
    Ensure.That(() => myString).IsNotNullOrEmpty();
    ...
}

The reason to why this is not included, is that: the lambda expression-tree has negative effects on performance, as it needs to compile the expression to extract the value. May not sound like a big deal, but I want you to be aware of it, hence why you need to include it manually.

Help!

There are probably a bunch of stuff you can add that is missing. Please do. Follow this guide, and I’ll accept your pull request.

Ps! A logo would be nice as well.

Thanks,

//Daniel

Category:
Development
Tags:
, ,

Join the conversation! 1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: