LightTraveller.Guards 1.1.1

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package LightTraveller.Guards --version 1.1.1                
NuGet\Install-Package LightTraveller.Guards -Version 1.1.1                
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.
<PackageReference Include="LightTraveller.Guards" Version="1.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LightTraveller.Guards --version 1.1.1                
#r "nuget: LightTraveller.Guards, 1.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LightTraveller.Guards as a Cake Addin
#addin nuget:?package=LightTraveller.Guards&version=1.1.1

// Install LightTraveller.Guards as a Cake Tool
#tool nuget:?package=LightTraveller.Guards&version=1.1.1                

LightTraveller.Guards

Using this throw helper (guard) library, you can skip typing boilerplate code to guard against unacceptable values of arguments passed to a method. This helps to produce a cleaner and more readable code


Example: Checking for null

The following method

using LightTraveller.Guards;

public class TicketService
{
    private readonly TicketRepository _repository;
    private readonly UserService _userService;
    private readonly TicketValidator _validator;
    

    public TicketService(TicketRepository repository, UserService userService, TicketValidator validator)
    {
        if (repository is null)
            throw new ArgumentNullException(nameof(repository));

        if (userService is null)
            throw new ArgumentNullException(nameof(userService));

        if (validator is null)
            throw new ArgumentNullException(nameof(validator));
        
        _repository = repository;
        _userService = userService;
        _validator = validator; 
    }
}

can be rewritten as

using LightTraveller.Guards;

public class TicketService
{
    private readonly TicketRepository _repository;
    private readonly UserService _userService;
    private readonly TicketValidator _validator;
    

    public TicketService(TicketRepository repository, UserService userService, TicketValidator validator)
    {
        _repository = Guard.Null(repository);        
        _userService = Guard.Null(userService);

        // An overload accepting a custom exception message
        _validator = Guard.Null(validator, "Make sure the passed argument is not null.");
    }
}

Notice

As you can see in the example above, when using Guards library, you do not need to pass the name of the arguments, e.g., nameof(repository) or "repository", to the guard methods to be used for throwing exceptions. This is taken care of by using [CallerArgumentExpression] attribute in the the methods of the Guard class.


Installation

You can add the Nuget package of the LightTraveller.Guards by running the following command in the .NET CLI

dotnet add package LightTraveller.Guards --version <VERSION NUMBER>

For more information please go to the nuget.org page of this library.

How to Use

  1. Add this to the using statements in your code: using LightTraveller.Guards;
  2. Simply call static methods of the Guard class by passing the argument to be checked to that method. Each method returns the instance passed to it if it passes the check; otherwise, throws an exception.

For a code sample, please look at the above example.

Available Methods

  1. Guard.Null throws if the generic input is null.
  2. Guard.Empty throws if the string input is null, an empty string or consists only of white-space characters.
  3. Gurad.NullOrEmpty throws if the IEnumerable<T> input is null, or an empty collection.
  4. Guard.Zero throws if the input is zero.
  5. Guard.Negative throws if the input is negative.
  6. Guard.ZeroOrNegative throws if the input is zero or negative.
  7. Guard.Positive throws if the input is positive.
  8. Guard.ZeroOrPositive throws if the input is zero or positive.
  9. Guard.OutOfRange throws if the generic IComparable<T> input is out of the specified range.
  10. Guard.InvalidEnumValue throws if the input cannot be cast to a member of the specified enumeration.
  11. Guard.ZeroPointer throws if the input is IntPtr.Zero.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on LightTraveller.Guards:

Package Downloads
LightTraveller.Helpers

Helpers for common coding tasks in .NET

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.4 472 2/27/2023
1.1.3 248 2/9/2023
1.1.2 258 2/3/2023
1.1.1 721 1/31/2023
1.1.0 324 11/30/2022
1.0.2 340 11/7/2022
1.0.1 351 11/4/2022
1.0.0 349 11/3/2022