« Back to home

The Ensures Library

Posted on

I have released a simple library called TheGarageLab.Ensures to NuGet, this is a set of static methods to verify method arguments, fields and other values.

As I mentioned in an earlier post I have started preparing some of my C# libraries for public consumption. I deliberately picked something simple to start with so I could concentrate on the packaging and publishing process without extra complexity.

Ensures is about as simple as it gets (you can find the source on GitHub) - it is a set of static methods that test for pre-conditions and throw an exception if they are not met. Usage looks like this:

public void MyMethod(string myParam)  
{
    Ensure.IsNotNullOrWhiteSpace(myParam);
}

It's not exactly a new idea or unique in anyway but it is a very useful pattern. It helps to avoid the general boilerplate code you normally have to write to check parameters and makes it a bit more clear as to what your intent is.

Each of the test functions has two versions, one that throws an ArgumentException and a generic version that throws an exception type of your choice:

public void MyMethod(object myParam)  
{
    Ensure.IsNotNull<ArgumentNullException>(myParam);
}

Simple, but effective.

To build the package I simply made a .nuspec file for the package and followed the instructions in this tutorial. This is about as simple a package as you can get - no other dependencies, no additional files required. The code is simple enough to put into a Portable Class Library so it should be usable on most .NET targets.

Feedback is of course welcome, use the comments here or raise an issue on the GitHub project.