Byteology.GuardClauses 1.0.2

.NET 5.0
There is a newer version of this package available.
See the version list below for details.
NuGet\Install-Package Byteology.GuardClauses -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package Byteology.GuardClauses --version 1.0.2
<PackageReference Include="Byteology.GuardClauses" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Byteology.GuardClauses --version 1.0.2
#r "nuget: Byteology.GuardClauses, 1.0.2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Byteology.GuardClauses as a Cake Addin
#addin nuget:?package=Byteology.GuardClauses&version=1.0.2

// Install Byteology.GuardClauses as a Cake Tool
#tool nuget:?package=Byteology.GuardClauses&version=1.0.2

Nuget Code Coverage

Security Rating Reliability Rating Maintainability Rating

Byteology's Guard Clauses

A simple extensible package containing generic guard clause extensions.

A guard clause is a software pattern that simplifies complex functions by "failing fast", checking for invalid inputs up front and immediately failing if any are found.

Usage

public void DepositBook(Book book)
{
    Guard.Argument(book, nameof(book)).NotNull();

    // Deposit the book
}

// OR

public class Book
{
    public string Name { get; }
    public string Author { get; }
    public string[] Genres { get; }
    public int NumberOfPages { get; }
    public DateTime PublicationDate { get; }

    public Book(string name, string author, string[] genres, int numberOfPages, DateTime publicationDate)
    {
        Guard.Argument(name, nameof(name)).NotNullOrWhiteSpace();
        Guard.Argument(author, nameof(author)).NotNullOrWhiteSpace();
        Guard.Argument(genres, nameof(genres))
            .NotNull()
            .ElementsCount(c => c.GreaterThan(0));
        Guard.Argument(numberOfPages, nameof(numberOfPages)).GreaterThan(0);
        Guard.Argument(publicationDate, nameof(publicationDate)).NotDefault();

        // Initialize the book
    }
}

Guard Clause Chaining

In order to make the guard code as readable as possible you are able to chain as many guard clauses as you like:

    Guard.Argument(genres, nameof(genres))
        .NotNull()
        .ElementsCount(c => c.GreaterThan(0));

Generics

Guard clauses are generic so only applicable to the parameter type will show in the IntelliSense. Additionally guard clauses with parameters will have their types infered.

Supported Guard Clauses

The library supports the following guard clauses out of the box.

For all parameter types

  • NotNull() - throw if the argument is null.
  • NotDefault() - throws if the argument is equal to its default value.
  • EqualsTo(value) - throws if the argument does not equal to the specified value.
  • NotEqualsTo(value) - throws if the argument is equal to the specified value.
  • Satisfies(predicate) - throws if the argument does not satisfy the specified predicate.

For string parameters

  • NotNullOrEmpty() - throws if the argument is null or empty.
  • NotNullOrWhiteSpace() - throws if the argument is null or empty or consists of only white-space characters.
  • InFormat(regexPattern, options) - throws if the argument is not in the format specified by a regular expression.

For IComparable parameters

  • GreaterThan(value) - throws if the argument is not greater than the specified value.
  • GreaterThanOrEqualTo(value) - throws if the argument is not greater than or equal to the specified value.
  • LessThan(value) - throws if the argument is not less than the specified value.
  • LessThanOrEqualTo(value) - throws if the argument is not less than or equal to the specified value.
  • InRange(min, max) - throws if the argument is not within the specified range.
  • NotInRange(min, max) - throws if the argument is within the specified range.

For IEnumerable parameters

  • ElementsCount(guardClause) - throws if the number of elements in the argument does not pass the specified guard clause.
  • AllElements(guardClause) - throws if at least one element of the argument does not pass the specified guard clause.

Extending with your own Guard Clauses

To extend your own guards, you can do the following:

// Using the same namespace will make sure your code picks up your 
// extensions no matter where they are in your codebase.
namespace Byteology.GuardClauses
{
    public static class GuardClauseExtensions
    {
        public static IGuardClause<Book> NotBlacklisted<Book>(this IGuardClause<Book> clause)
        {
            if (clause.Argument.Author == "Blacklisted Author")
                throw new ArgumentException($"{clause.ArgumentName} is in the list of blacklisted books.");

            return clause;
        }
    }
}

// Usage
public void DepositBook(Book book)
{
    Guard.Argument(book, nameof(book))
        .NotNull()
        .NotBlacklisted();

    // Deposit the book
}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Byteology.GuardClauses:

Package Downloads
Byteology.TypedHttpClients The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Provides classes for creating typed http clients from an interface describing a service.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.1 274 10/26/2021
1.2.0 157 10/26/2021
1.1.0 287 10/19/2021
1.0.2 156 10/16/2021
1.0.1 233 10/15/2021
1.0.0 370 10/15/2021