Just released Light.GuardClauses v1.3

2017-04-14 at 07:42 0 comments

Last year in May, I published Light.GuardClauses, a .NET Portable Class Library that lets you perform easy precondition checks at the beginning of your methods. Haven’t heard of it yet? Then check out this introduction video:

Last Wednesday, I released v1.3 which brings the following new features to you:

New assertion methods for URIs

v1.3 of Light.GuardClauses introduced two new assertion methods for the Uri class:

public async Task<string> GetWebContentAsync(Uri targetUrl)

    // Implementation omitted to keep the example small

The MustBeAbsoluteUri method ensures that the passed in Uri object is not a relative URI, the MustHaveScheme method checks that the URI actually is an HTTPS URL in this case. Both of them raise an ArgumentException if the precondition is violated. Of course, these methods also perform null checks raising an ArgumentNullException if necessary – thus your precondition check section at the beginning of your methods becomes as small as possible.

New assertion methods for DateTime

There are also new assertion methods for the the DateTime struct:

public void UpdateTimestamp(Datetime pointInTime)

    // Implementation omitted to keep the example small

In the example above, the MustBeUtc assertion method ensures that pointInTime has a DateTimeKind of UTC. I desperately needed this assertion method in my daily work, but I also included the MustBeLocal and MustBeUnspecified methods in the v1.3 release of Light.GuardClauses, just for the sake of completion.

Assertion methods that don’t throw exceptions

The most important improvement of v1.3 is the addition of assertion methods that actually do not throw exceptions. In my daily work, I sometimes find myself in situations where I want to perform a relative complex check that is actually already included in Light.GuardClauses, but I do not want to throw an exception during this check. This often happens when I create Anticorruption Layers or parse serialized values.

Therefore, v1.3 of Light.GuardClauses includes the following assertion method:

  • IsValidEnumValue,
  • IsEmpty (for GUIDs),
  • IsIn and IsNotIn (ranges),
  • IsNullOrEmpty (for collections and strings)
  • IsNullOrWhiteSpace (for strings)
  • IsSameAs (for object references)
  • ContainsOnlyLetters and ContainsOnlyLettersAndDigits (for strings)

Keep in mind that these methods cannot be conditionally compiled as all of them return a Boolean value. In the end, I plan to vastly extend them in future versions so that there is an exception throwing variant and a Boolean variant for all complex assertions.

Go get it

Light.GuardClauses is available on NuGet and GitHub, 100% production ready, so check it out and give it like. You have an idea for your own extension method? Then create an issue or implement it yourself and send me a Pull Request on GitHub.


Currently there are no comments for this post. Write the first one!

Leave a Reply

Your email address will not be published. Required fields are marked *