Ardalis.GuardClauses
5.0.0
dotnet add package Ardalis.GuardClauses --version 5.0.0
NuGet\Install-Package Ardalis.GuardClauses -Version 5.0.0
<PackageReference Include="Ardalis.GuardClauses" Version="5.0.0" />
paket add Ardalis.GuardClauses --version 5.0.0
#r "nuget: Ardalis.GuardClauses, 5.0.0"
// Install Ardalis.GuardClauses as a Cake Addin #addin nuget:?package=Ardalis.GuardClauses&version=5.0.0 // Install Ardalis.GuardClauses as a Cake Tool #tool nuget:?package=Ardalis.GuardClauses&version=5.0.0
<h1 align=center> <img src="media/logotype 1024.svg" width=50%> </h1>
<a href="https://twitter.com/intent/follow?screen_name=ardalis"> <img src="https://img.shields.io/twitter/follow/ardalis.svg?label=Follow%20@ardalis" alt="Follow @ardalis" /> </a> <a href="https://twitter.com/intent/follow?screen_name=nimblepros"> <img src="https://img.shields.io/twitter/follow/nimblepros.svg?label=Follow%20@nimblepros" alt="Follow @nimblepros" /> </a>
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);
// 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);
_quantity = Guard.Against.NegativeOrZero(quantity);
_max = Guard.Against.Zero(max);
_unitPrice = Guard.Against.Negative(unitPrice);
_dateCreated = Guard.Against.OutOfSQLDateRange(dateCreated, 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.EnumOutOfRange (throws if an enum value is outside a provided Enum 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)
- Guard.Against.Expression (use any expression you define)
- Guard.Against.InvalidFormat (define allowed format with a regular expression or func)
- Guard.Against.NotFound (similar to Null but for use with an id/key lookup; throws a
NotFoundException
)
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 Ardalis.GuardClauses
{
public static class FooGuard
{
public static void Foo(this IGuardClause guardClause,
string input,
[CallerArgumentExpression("input")] string? parameterName = null)
{
if (input?.ToLower() == "foo")
throw new ArgumentException("Should not have been foo!", parameterName);
}
}
}
// Usage
public void SomeMethod(string something)
{
Guard.Against.Foo(something);
Guard.Against.Foo(something, nameof(something)); // optional - provide parameter name
}
YouTube Overview
Breaking Changes in v4
- OutOfRange for Enums now uses
EnumOutOfRange
- Custom error messages now work more consistently, which may break some unit tests
Nice Visualization of Refactoring to use Guard Clauses
https://user-images.githubusercontent.com/782127/234028498-96e206b0-9a70-4aa0-9c36-a62477ea0aa9.mp4
via Nicolas Carlo
References
- Getting Started with Guard Clauses
- How to write clean validation clauses in .NET (Nick Chapsas, YouTube, 9 minutes)
- Guard Clauses (podcast: 7 minutes)
- Guard Clause
Commercial Support
If you require commercial support to include this library in your applications, contact NimblePros
Build Notes (for maintainers)
- Remember to update the PackageVersion in the csproj file and then a build on main should automatically publish the new package to nuget.org.
- Add a release with form
1.3.2
to GitHub Releases in order for the package to actually be published to Nuget. Otherwise it will claim to have been successful but is lying to you.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 is compatible. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages (191)
Showing the top 5 NuGet packages that depend on Ardalis.GuardClauses:
Package | Downloads |
---|---|
Een.Common
Nuget package for common helpers and enums |
|
Serilog.UI
Simple web UI for several Serilog sinks. |
|
Ardalis.SharedKernel
An example shared kernel project/package for use with Ardalis.CleanArchitecture template. You should probably replace this with your own package! |
|
Serilog.UI.MsSqlServerProvider
Microsoft SQL Server data provider for Serilog UI. |
|
mmx.utils
Package Description |
GitHub repositories (22)
Showing the top 5 popular GitHub repositories that depend on Ardalis.GuardClauses:
Repository | Stars |
---|---|
jasontaylordev/CleanArchitecture
Clean Architecture Solution Template for ASP.NET Core
|
|
ardalis/CleanArchitecture
Clean Architecture Solution Template: A starting point for Clean Architecture with ASP.NET Core
|
|
dotnet-architecture/eShopOnWeb
Sample ASP.NET Core 8.0 reference application, powered by Microsoft, demonstrating a layered application architecture with monolithic deployment model. Download the eBook PDF from docs folder.
|
|
rnwood/smtp4dev
smtp4dev - the fake smtp email server for development and testing
|
|
ardalis/SmartEnum
A base class for quickly and easily creating strongly typed enum replacements in C#.
|
Version | Downloads | Last updated |
---|---|---|
5.0.0 | 675,587 | 9/30/2024 |
4.6.0 | 1,139,201 | 7/8/2024 |
4.5.0 | 2,290,381 | 2/26/2024 |
4.4.0 | 626,308 | 1/23/2024 |
4.3.0 | 532,186 | 12/23/2023 |
4.2.0 | 761,762 | 11/9/2023 |
4.1.2 | 629 | 11/9/2023 |
4.1.1 | 1,237,464 | 8/1/2023 |
4.1.0 | 1,868 | 8/1/2023 |
4.0.1 | 9,426,558 | 3/28/2022 |
4.0.0 | 884,646 | 1/18/2022 |
3.3.0 | 1,274,953 | 10/29/2021 |
3.2.0 | 1,539,994 | 5/14/2021 |
3.1.0 | 602,996 | 2/13/2021 |
3.0.1 | 682,936 | 10/9/2020 |
3.0.0 | 206,698 | 8/20/2020 |
2.0.0 | 97,703 | 7/22/2020 |
1.5.0 | 1,045,839 | 4/29/2020 |
1.4.2 | 127,710 | 3/16/2020 |
1.4.1 | 99,288 | 2/13/2020 |
1.4.0 | 5,622 | 2/13/2020 |
1.3.3 | 73,575 | 1/18/2020 |
1.3.2 | 10,184 | 1/3/2020 |
1.3.1 | 72,286 | 11/15/2019 |
1.2.9 | 234,460 | 7/11/2019 |
1.2.8 | 95,125 | 2/11/2019 |
1.2.7 | 9,728 | 2/4/2019 |
1.2.6 | 3,446 | 1/24/2019 |
1.2.5 | 6,462 | 1/23/2019 |
1.2.4 | 492,617 | 11/26/2018 |
1.2.3 | 239,712 | 12/4/2017 |
1.2.2 | 2,864 | 12/1/2017 |
1.2.1 | 2,373 | 11/13/2017 |
1.2.0 | 2,024 | 11/13/2017 |
1.1.1 | 17,299 | 9/27/2017 |
1.1.0 | 1,652 | 9/26/2017 |
1.0.3 | 1,916 | 9/22/2017 |
1.0.2 | 2,005 | 9/22/2017 |
1.0.1 | 2,009 | 9/22/2017 |
1.0.0 | 38,436 | 9/12/2017 |
* Updating to 5.0 because 4.6 had breaking logic (see #354)
* Updated Obsolete message for Guard.Against.Expression to clarify reversal of logic (#457)