ArgDefender 1.1.0
dotnet add package ArgDefender --version 1.1.0
NuGet\Install-Package ArgDefender -Version 1.1.0
<PackageReference Include="ArgDefender" Version="1.1.0" />
paket add ArgDefender --version 1.1.0
#r "nuget: ArgDefender, 1.1.0"
// Install ArgDefender as a Cake Addin
#addin nuget:?package=ArgDefender&version=1.1.0
// Install ArgDefender as a Cake Tool
#tool nuget:?package=ArgDefender&version=1.1.0
ArgDefender
ArgDefender is a fluent argument validation library that is intuitive, fast and extensible.
[!NOTE]
This project is a continuation of the archived Dawn.Guard
Installation
Using dotnet
dotnet add package ArgDefender
Using PowerShell
Install-Package ArgDefender
Introduction
Here is a sample constructor that validates its arguments without ArgDefender:
public Person(string name, int age)
{
if (name == null)
throw new ArgumentNullException(nameof(name), "Name cannot be null.");
if (name.Length == 0)
throw new ArgumentException("Name cannot be empty.", nameof(name));
if (age < 0)
throw new ArgumentOutOfRangeException(nameof(age), age, "Age cannot be less than zero.");
Name = name;
Age = age;
}
And this is how we write the same constructor with ArgDefender:
using ArgDefender;
public Person(string name, int age)
{
Name = Guard.Argument(name, nameof(name)).NotNull().NotEmpty();
Age = Guard.Argument(age, nameof(age)).Min(0);
}
If this looks like too much allocations to you, fear not. The arguments are read-only structs that are passed by reference. See the design decisions for details and an introduction to ArgDefender's more advanced features.
What's Wrong with Vanilla?
There is nothing wrong with writing your own checks but when you have lots of types you need to validate, the task gets very tedious, very quickly.
Let's analyze the string validation in the example without ArgDefender:
- We have an argument (name) that we need to be a non-null, non-empty string.
- We check if it's null and throw an
ArgumentNullException
if it is. - We then check if it's empty and throw an
ArgumentException
if it is. - We specify the same parameter name for each validation.
- We write an error message for each validation.
ArgumentNullException
accepts the parameter name as its first argument and error message as its second while it's the other way around for theArgumentException
. An inconsistency that many of us sometimes find it hard to remember.
In reality, all we need to express should be the first bullet, that we want our argument non-null and non-empty.
With ArgDefender, if you want to guard an argument against null, you just write NotNull
and that's it.
If the argument is passed null, you'll get an ArgumentNullException
thrown with the correct
parameter name and a clear error message out of the box. The standard validations
have fully documented, meaningful defaults that get out of your way and let you focus on your project.
Standard Validations
Click here for a list of the validations that are included in the library.
Design Decisions
Click here for the document that explains the motives behind the ArgDefender's API design and more advanced features.
Extensibility
Click here to see how to add custom validations to ArgDefender by writing simple extension methods.
Code Snippets
Code snippets can be found in the snippets folder. Currently, only the Visual Studio is supported.
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 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. |
.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 was computed. |
.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.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.