LightTraveller.Guards 1.0.2

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. 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.0.2
NuGet\Install-Package LightTraveller.Guards -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.
<PackageReference Include="LightTraveller.Guards" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LightTraveller.Guards --version 1.0.2
#r "nuget: LightTraveller.Guards, 1.0.2"
#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.0.2

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

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 void CalculatePayableAmount(Order order, decimal lastPaidAmount, long storeId)
{
    if (order is null)
    {
        throw new ArgumentNullException(nameof(order));
    }

    if (lastPaidAmount < 0)
    {
        throw new ArgumentException("The decimal parameter cannot be negative.",
                                    nameof(prevPaymentAmount));
    }

    if (storeId <= 0)
    {
        throw new ArgumentException("The long parameter cannot be negative.",
                                    nameof(storeId));
    }

    //...
}

can be rewritten as

using LightTraveller.Guards;

public void CalculatePayableAmount(Order order, decimal lastPaidAmount, long storeId)
{
    order = Guard.Null(order);
    lastPaidAmount = Guard.Negative(lastPaidAmount);
    storeId = Guard.ZeroOrNegative(storeId);

    //...
}

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(order) or "order", 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.
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 The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

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 430 2/27/2023
1.1.3 222 2/9/2023
1.1.2 236 2/3/2023
1.1.1 652 1/31/2023
1.1.0 304 11/30/2022
1.0.2 320 11/7/2022
1.0.1 326 11/4/2022
1.0.0 325 11/3/2022