OpenSource

Bytes2you.Validation – New Version Released (1.2.0.1)

With this post I want to share with you that a new version of Bytes2you.Validation library was just released.The release notes are as follows:

– Converted from ClassLibrary to PortableLibrary. Now it supports .NET Framework 4 and higher; Silverlight 4 and higher; Windows Phone 7 and higher; .NET for Windows Store apps; Xbox 360.
– Refactored the validation messages. Now every value is surrounded by <> for better readability.
– Introduced IsInstanceOfType() & IsNotInstanceOfType methods.
– Introduced IsNull() & IsNotNull() for Nullable<T> types.
– IntelliSense improvements. The members of IArgument<T> and IValidatableArgument<T> are now hidden from the editor.
– Added special float predicates – Infinity, NegativeInfinity, PositiveInfinity, NaN.
– Added special double predicates – Infinity, NegativeInfinity, PositiveInfinity, NaN.
– Better support for byte and short (no need to cast).

You can find the latest nuget here and the source code here.

Enjoy!

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, &quot;argument&quot;).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(&quot;Argument is null or empty string.&quot;, &quot;stringArgument&quot;);
   }

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

…we can use

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

Rules List

Guard.WhenArgument&lt;T&gt;(argument, &quot;argument&quot;)

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!