GuardMe

Argument guard clauses with ReSharper annotations.


Keywords
annotations, arguments, clause, guard, argument-checks, csharp, dotnet, external-annotations, guard-clauses, netstandard
License
MIT
Install
Install-Package GuardMe -Version 1.1.0

Documentation

GuardMe

GuardMe is a C# guard-clause library annotated with ReSharper annotations.

public void Query(string query)
{
  Guard.NotNull (nameof(query), query);
  // query is guaranteed to be not null
}

internal void QueryInternal(string query)
{
  Bouncer.NotNull (nameof(query), query);
  // query is guaranteed to be not null ...
  // and the guard disappears when not debugging
}

Features

No library dependency: Instead of referencing the library you can include all the relevant source files directly in your project using the inline version.

Annotated with ReSharper annotations: Each guard clause is annotated with ReSharper annotations providing a richer coding experience.

Easily extended for specific use cases: While the library already provides the most basic use cases you can easily extend it with your own use cases by adding methods to the partial classes Guard and Bouncer.

Public/Internal API differentiation: The library differentiates between guard clauses exposed in the public API and guard clauses exposed in the internal API via the Guard and Bouncer classes.

Inline version

An addition to the standard library format GuardMe is also available as an inline version. The inline version includes all relevant source files directly in your project instead of adding a project reference. The following table gives a quick overview of the differences:

Feature Normal format Inline format
Project reference Yes No
ReSharper annotations via external annotations Yes Not needed
Reference to Jetbrains.Annotations No Yes
Extensible via partial classes No Yes
Ability to change the base guard clauses No Yes

Installation

Normal version: Install-Package GuardMe

Inline version (preferred): Install-Package GuardMe.Inline

Get it via Nuget (see above), from the Releases, or build it yourself.

API

GuardMe provides all its functionality via the Guard and the Bouncer classes. They both contain similar/equal guard clauses but should be used under different circumstances:

Guard should be used if the containing method is exposed via public API. Its guard clauses are permanent and will not be removed upon compilation, aiding a person that uses the public API surface.

static Guard
{
  void NotNull<T>(string parameterName, T value) where T : class
  void NotNull<T>(string parameterName, T? value) where T : struct
  
  void NotNullOrEmpty(string parameterName, string value)
  void NotNullOrWhitespace(string parameterName, string value)
}

Bouncer on the other hand should be used in methods which are exposed via public API. Its guard clauses are marked as conditional and therefore will only be compiled when debugging.

[Conditional("DEBUG")]
static Bouncer
{
  void NotNull<T>(string parameterName, T value) where T : class
  void NotNull<T>(string parameterName, T? value) where T : struct
  
  void NotNullOrEmpty(string parameterName, string value)
  void NotNullOrWhitespace(string parameterName, string value)
}

Annotations

All guard clauses are annotated with ReSharper annotations. Depending on which version you use the ReSharper annotations are deployed differently:

The annotations for the normal version are deployed via external annotations. This means that the library itself does not reference Jetbrains.Annotations, but instead has a .ExternalAnnotations.xml file that provides the information about the annotations. In order for the external annotations to work they must be placed besides the library.

The inline version references Jetbrains.Annotations directly.

Changelog

1.1: Added the Bouncer as well as guards for string. 1.0: Added null guards for reference and value types.

A Complete changelog can be found in CHANGELOG.md.