Bajonczak.Guard
1.0.1
.NET Standard 2.0
.NET Framework 4.0
dotnet add package Bajonczak.Guard --version 1.0.1
NuGet\Install-Package Bajonczak.Guard -Version 1.0.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="Bajonczak.Guard" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Bajonczak.Guard --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Bajonczak.Guard, 1.0.1"
#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 Bajonczak.Guard as a Cake Addin
#addin nuget:?package=Bajonczak.Guard&version=1.0.1
// Install Bajonczak.Guard as a Cake Tool
#tool nuget:?package=Bajonczak.Guard&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Guard Clauses
A simple extensible package with 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.
Give a Star! ⭐
If you like or are using this project please give it a star. Thanks!
Usage
public void ProcessOrder(Order order)
{
Guard.Against.Null(order, nameof(order));
// process order here
}
// OR
public class Order
{
private string _name;
private int _quantity;
private long _max;
private decimal _unitPrice;
private DateTime _dateCreated;
public Order(string name, int quantity, long max, decimal unitPrice, DateTime dateCreated)
{
_name = Guard.Against.NullOrWhiteSpace(name, nameof(name));
_quantity = Guard.Against.NegativeOrZero(quantity, nameof(quantity));
_max = Guard.Against.Zero(max, nameof(max));
_unitPrice = Guard.Against.Negative(unitPrice, nameof(unitPrice));
_dateCreated = Guard.Against.OutOfSQLDateRange(dateCreated, nameof(dateCreated));
}
}
Supported Guard Clauses
- Guard.Against.Null (throws if input is null)
- Guard.Against.NullOrEmpty (throws if string, guid or array input is null or empty)
- Guard.Against.NullOrWhiteSpace (throws if string input is null, empty or whitespace)
- Guard.Against.OutOfRange (throws if integer/DateTime/enum input is outside a provided range)
- Guard.Against.OutOfSQLDateRange (throws if DateTime input is outside the valid range of SQL Server DateTime values)
- Guard.Against.Zero (throws if number input is zero)
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 Bajonczak.GuardClauses
{
public static class FooGuard
{
public static void Foo(this IGuardClause guardClause, string input, string parameterName)
{
if (input?.ToLower() == "foo")
throw new ArgumentException("Should not have been foo!", parameterName);
}
}
}
// Usage
public void SomeMethod(string something)
{
Guard.Against.Foo(something, nameof(something));
}
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 net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net40 net403 net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.0
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.1 | 725 | 10/25/2021 |
Added a new guard for NotFound; added support for optional custom error messages