An extensible argument validation package by @JarrydVanHoy that I feel reads quite fluently.
$ dotnet add package ThrowIfArgumentThis is just another guard clause library for validating your arguments, but I enjoy the way using this API reads quite fluently. If you're like me, then you've been recreating a library of guard clauses from company to company. I finally got tired and uploaded my own version of a guard clause library.
This library is very similar to Guard Clauses, which I highly recommend checking out. It is well reviewed and has active support.
Install the ThrowIfArgument NuGet package:
dotnet add package ThrowIfArgument
public void HelloWorld(FooBar fooBar)
{
ThrowIf.Argument.IsNull(fooBar);
Console.WriteLine($"The narwhal bacons at {fooBar.BaconsAt}");
}
If you feel the package is missing a feature, you can extend with your own features:
public static ThrowIfArgumentExtensions
{
public static string StartsWithThunderclap
(
this IThrowIfArgumentBuilder,
string argument,
string? message = null,
[CallerArgumentExpression("argument")] string? argumentName = null
)
{
if (!argument.StartsWith("thunderclap", StringComparison.OrdinalIgnoreCase))
{
return argument;
}
throw new ArgumentException(
string.IsNullOrWhiteSpace(message)
? $"Cannot start with 'thunderclap'."
: message,
argumentName);
}
}
Ping me if you want any new features added to the library ❤️