DynProg.Validation now available on NuGet

Published on Dec 27, 2012

A bit of history

I wrote this library for the first time 6 or 7 years ago.

During all this time I keep improving it and enhancing it, sometimes even duplicating the effort on a different code base. For a few years now it has been sitting on my personal GitHub repository with a bunch of other small projects.

During the holidays, while working on a new side project, I was ready to add the library to the project the old fashion way, copy the DLL into a libs folder and add a reference.

It’s evident that this is not the way of doing things in .Net anymore and since we (.Net developers) finally have a package manager it was time to move this library into NuGet.

After a few hours of fiddling around with the code, cleaning up a few things and moving it into it’s own repository I published and now is available for everybody via NuGet.

You can install via NuGet or inside VS using the NuGet Package Manager.

Why another validation library?

DynProg.Validation is intended to replace the countless checks for null argument and argument formatting and provide a simple and easy to use interface.

It’s not a replacement for other popular libraries like FluentValidation or DataAnnotations.

It should replace code like this:

    public string setValue(string key, string value)
        if (String.IsNullOrEmpty(key) || key.length < 5 || key.length > 20)
            throw new ArgumentException("key");
        if (String.IsNullOrEmpty(value))
            throw new ArgumentException("value");
        // the actual code goes here

with something like this…

    public string setValue(string key, string value)
      var validator = new Validator();
      validator.CheckThat(() => key).IsNotNullOrEmpty()
          .LengthIsBetween(5, 20);
      validator.CheckThat(() => value).IsNotNullOrEmpty();
      //the actual code goes here

It doesn’t looks like much but let’s examine some of the niceties in the API and some of the facilities it provides.

General usage

We create a validator instance per method and you call the CheckThat method. This method takes a func that access and returns the parameter that we want to check.

This is a little weird but the reason is that we use this to internally get the name of the parameter that in turn we use to generate the Exception message. This save you of passing a string with the parameter name that can get out of sync at the first refactoring of your code.

The CheckThat method returns one of many CheckConditions based on the type of the parameter.

These CheckConditions expose a series of method suitable to check multiple aspects of the parameter.

You can chain multiple conditions together to check several attributes of the parameter.

Specific Exception per check

All methods of the CheckConditions have a “typeless” interface that will generate a generic Exception based on what condition is checking, parameter type and name.

They also have a “Generic” interface that take an Exception type. This interface takes the same parameters of the “typeless” one plus an array of object as the arguments that Exception type is expecting.

Managing exceptions.

Although (at the moment) the validation is run on runtime no Exceptions will be thrown or values returned unless you call one of the following methods.

  • List()
  • Throw()
  • Throw<T>()
  • ThrowFirst()

These methods can be called on an instance of the Validator object or in any of the CheckConditions.

The ThrowFirst method will throw the first exception in case one or more exceptions where found.

The Throw method will throw an ErrorsCollectionException if at least one error was found.

ErrorsCollectionException inherits from Exception and contains methods to access the list of exceptions generated during validation.

The Throw<T> method will throw an Exception of type T if at least one error was found.

Finally List will return the ErrorsCollectionException even if empty.

API documentation

I put together a quick site using gh-pages with the complete API.

More code examples coming soon, or just check the tests.