LightTraveller.Guards
1.0.2
Prefix Reserved
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
<PackageReference Include="LightTraveller.Guards" Version="1.0.2" />
paket add LightTraveller.Guards --version 1.0.2
#r "nuget: LightTraveller.Guards, 1.0.2"
// 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 theGuard
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
- Add this to the
using
statements in your code:using LightTraveller.Guards;
- Simply call
static
methods of theGuard
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
Guard.Null
throws if the generic input is null.Guard.Empty
throws if thestring
input is null, an empty string or consists only of white-space characters.Gurad.NullOrEmpty
throws if theIEnumerable<T>
input is null, or an empty collection.Guard.Zero
throws if the input is zero.Guard.Negative
throws if the input is negative.Guard.ZeroOrNegative
throws if the input is zero or negative.Guard.Positive
throws if the input is positive.Guard.ZeroOrPositive
throws if the input is zero or positive.Guard.OutOfRange
throws if the genericIComparable<T>
input is out of the specified range.Guard.InvalidEnumValue
throws if the input cannot be cast to a member of the specified enumeration.
Product | Versions 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. |
-
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.