Friday, September 16, 2011

Seterlund.CodeGuard is now on NuGet

My small guard library is now available on NuGet.

The package name is Seterlund.CodeGuard and is available under the New BDS License.

The Guard.That(...) will throw an exception, when some condition is not met

public void SomeMethod(int arg1, int arg2)
{
    // This line will throw an exception when the arg1 is less or equal to arg2
    Guard.That(() => arg1).IsGreaterThan(arg2);

    // This will check that arg1 is not null and that is in some range 1..100
    Guard.That(arg2).IsNotNull().IsInRange(1,100);

    // Several checks can be added.
    Guard.That(arg1)
      .IsInRange(100,1000)
      .IsEven()
      .IsTrue(x => x > 50, "Must be over 500");

    // Do stuff
}


The Validate.That(...) method makes it possible to return a list of all error conditions

public void OtherMethod(int arg1)
{
    // Get a list of errors
    List<string> errors = Validate.That(() => arg1).IsNotNull().GetResult();
}
---
Share:

0 kommentarer:

Post a Comment