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" />
<PackageVersion Include="LightTraveller.Guards" Version="1.0.2" />
<PackageReference Include="LightTraveller.Guards" />
paket add LightTraveller.Guards --version 1.0.2
#r "nuget: LightTraveller.Guards, 1.0.2"
#:package LightTraveller.Guards@1.0.2
#addin nuget:?package=LightTraveller.Guards&version=1.0.2
#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 theGuardclass.
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
usingstatements in your code:using LightTraveller.Guards; - Simply call
staticmethods of theGuardclass 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.Nullthrows if the generic input is null.Guard.Emptythrows if thestringinput is null, an empty string or consists only of white-space characters.Gurad.NullOrEmptythrows if theIEnumerable<T>input is null, or an empty collection.Guard.Zerothrows if the input is zero.Guard.Negativethrows if the input is negative.Guard.ZeroOrNegativethrows if the input is zero or negative.Guard.Positivethrows if the input is positive.Guard.ZeroOrPositivethrows if the input is zero or positive.Guard.OutOfRangethrows if the genericIComparable<T>input is out of the specified range.Guard.InvalidEnumValuethrows 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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.