Easy to write, easy to read guard clauses for your .NET methods. Available as a NuGet package.
Guard against nulls.
Ensure.Arg(nameof(myArg), myArg).IsNotNull();
Guard against empty strings or whitespace.
Ensure.Arg(nameof(myArg), myArg).IsNotEmpty();
Ensure.Arg(nameof(myOtherArg), myOtherArg).IsNotWhitespace();
Guard against anything.
// Ensure myArg contains a number.
Ensure.Arg(nameof(myArg), myArg).Is(a => a.Any(char.IsDigit));
Chain any of them together.
Ensure.Arg(nameof(myArg), myArg).IsNotNull().And.IsNotWhitespace();
Creating your own guard clauses is easy to do.
public static class ArgExtensions
{
public static Andable<Arg<int>> IsGreaterThanZero(this Arg<int> arg)
{
Ensure.Arg(nameof(arg), arg).IsNotNull();
return Ensure.Arg(arg.Name, arg.Value).Is(a => a > 0);
}
}
Using them is too.
Ensure.Arg(nameof(myArg), myArg).IsGreaterThanZero();
ArgEnsure contains methods to throw any of the three built-in argument exceptions in .NET. The ability to throw custom exceptions is coming soon.
Exception | Methods | Extension Methods |
---|---|---|
System.ArgumentNullException |
IsNotNull() |
|
System.ArgumentException |
Is() |
IsNot() IsNotEmpty() IsNotWhitespace() |
System.ArgumentOutOfRangeException |
IsInRange() |
IsNotInRange() |
Building the source from scratch can be done a number of ways.
The easiest method is by using Docker. If you have Bash, Git and Docker installed you can use the included scripts to kick off a build immediately. No other tools are required.
cd build
./build.sh restore
./build.sh test
./build.sh clean # or
./build.sh clean-repo
If you have sufficient build tools installed, the standard dotnet
CLI commands will also work.
- Mono,
mono-devel
- .NET Core SDK 2.0 or above