The primary way to use Ensure.That is through the Ensure.That() extension method pattern. This approach is highly readable and supports method chaining.
Basic Usage
You can validate a value directly or include the parameter name for better error reporting:
Ensure.That(myString).IsNotNullOrWhiteSpace();
Ensure.That(myString, nameof(myString)).IsNotNullOrWhiteSpace();
Chaining Validations
Methods are chainable, allowing you to perform multiple checks on the same argument:
Ensure
.That(myString)
.IsNotNullOrWhiteSpace()
.IsGuid();
Extending with Custom Validations
You can extend the validation capabilities by creating extension methods for the Param<T> type (the type returned by Ensure.That):
public static class StringArgExtensions
{
public static StringParam IsNotFishy(this StringParam param)
=> param.Value != "fishy"
? param
: throw Ensure.ExceptionFactory.ArgumentException("Something is fishy!", param.Name);
}
Ensure.That(myString, nameof(myString)).IsNotFishy();
Performance Note: If you are concerned about the performance overhead of the public readonly struct Param<T> created by Ensure.That(), consider using Ensure.Context or EnsureArg instead.
Ensure.That(myString).IsNotNullOrWhiteSpace();
Ensure.That(myString, nameof(myString)).IsNotNullOrWhiteSpace();
Ensure
.That(myString)
.IsNotNullOrWhiteSpace()
.IsGuid();