Bytes2you.Validation – Defensive Programming Toolset

Intro

In my previous post I’ve talked about the defensive programming technique. As I’ve promised I’m sharing with you the required toolset for implementing defensive programming in .NET easily in your codebase in clear and testable way.

Bytes2you.Validation

Bytes2you.Validatino is fast, extensible, intuitive and easy-to-use C# library providing fluent APIs for argument validation. It gives everything you need to implement defensive programming in your .NET applications.

Installation

The library is available in the NuGet Gallery here. Just type: PM> Install-Package Bytes2you.Validation

The API

Guard.WhenArgument(argument, "argument").Rule1().Rule2()...RuleN().Throw();

Based on the argument type there are different things that we can validate that make sense for that type (such as IsEmpty validation for string/guid/collection, etc.).

Examples: Instead of…

public void SomeMethod(string stringArgument)
{
   if (string.IsNullOrEmpty(stringArgument))
   {
      throw new ArgumentException("Argument is null or empty string.", "stringArgument");
   }

   if (stringArgument == "xxx")
   {
      throw new ArgumentException("Argument is equal to \"xxx\"", "stringArgument");
   }
}

…we can use

public void SomeMethod(string stringArgument)
{
   Guard.WhenArgument(stringArgument, "stringArgument").IsNullOrEmpty().IsEqual("xxx").Throw();
   // Which means - when stringArgument is null or empty OR is equal to "xxx" we should throw exception.
}

Rules List

Guard.WhenArgument<T>(argument, "argument")

For all T:

.IsEqual(value);
.IsNotEqual(value);

When T is class:

.IsNull();
.IsNotNull();

When T is bool:

.IsTrue();
.IsFalse();

When T is IComparable:

.IsLessThan(bound);
.IsGreaterThan(bound);
.IsLessThanOrEqual(bound);
.IsGreaterThanOrEqual(bound);

When T is IEnumerable:

.IsNullOrEmpty();
.IsNotNullOrEmpty();

When T is Guid:

.IsEmptyGuid();
.IsNotEmptyGuid();

When T is string:

.IsNullOrEmpty();
.IsNotNullOrEmpty();

Fully Tested

The codebase is fully covered with unit tests (100% code coverage).

Excellent Performance

Performance tests are also presented ensuring that the library will not affect the performance of your applications.

Extensible

One can easily add new rules (see ValidationRules namespace) and expose them through fluent API extensions (see FluentExtensions namespace).

Request…

I’ll be more than happy to hear your feedback about the aforementioned library. If you think of a way of improving it, or you believe that a new features will bring more value to it, you can just drop me a line.

Thank you!

One comment

  1. This really works well, It will be nice if you extended adding the posibility to throw a custom exception (or any other besides the hardcoded ones) thank you

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