A simple and succinct way to code defensively without clogging up your classes with dozens of lines of boring boilerplate.
using static Aspinall.ArgumentChecker.FluentChecker;
public class MyClass
{
private string myString;
public MyClass(string someString)
{
myString = CheckThat(someString, nameof(someString)).IsNotNullOrWhitespace().Value;
}
...
}
nuget install Aspinall.ArgumentChecker
The recommended way to use the library is through the fluent API demonstrated above.
- Add
using static Aspinall.ArgumentChecker.FluentChecker;
to your class. - For each argument you want to check:
- Create an
ArgumentChecker
instance usingCheckThat(value, nameof(value))
. - Chain the checking method calls to it e.g.
CheckThat(value, nameof(value)).IsNotNull().IsNotEmpty()
. Each check method returns theArgumentChecker
again. If a check fails then an approriate exception will be thrown. - If you want to assign the original value after it has been checked, it is available in the
Value
property of theArgumentChecker
e.g.var local = CheckThat(argument, nameof(argument)).IsNotNull().Value;
- Create an
The following checks are currently available:
IsEqualTo(expectedValue)
IsNotDefaultValue()
IsNotEmpty()
- assumes that the value implementsSystem.Collections.ICollection
IsNotNull()
IsNotNullOrWhitespace()
- assumes that the value is astring
You can check the documentation comments and the unit tests for a detailed description of each check and example usage.
This project targets .NET Standard 1.4 and was created using Visual Studio 2017. As such it uses the new .csproj
file format and requires the VS2017 build tools. Other than that it is a very simple project.
If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.
The code in this project is licensed under MIT license.