-
Notifications
You must be signed in to change notification settings - Fork 0
SolidStack.Core.Guards
SolidStack.Core.Guards is an extremely simple, unambiguous and lightweight guard clause library that allow you to write pre-conditions and post-conditions for your methods in a readable way.
Guard clauses...
public string Foo(IEnumerable<string> sequence)
{
Guard.RequiresNonNull(sequence, nameof(sequence));
Guard.RequiresAll(sequence, item => !string.IsNullOrEmpty(item));
var result = Bar(sequence);
Guard.Ensures(() => !string.IsNullOrEmpty(result), "Bar() returned an invalid string.");
return result;
}First, install NuGet. Then, install SolidStack.Core.Guards from the package manager console:
PM> Install-Package SolidStack.Core.Guards
The Guard class provides a set of guard clauses to help you protect against the pre-conditions and the post-conditions of your methods. To read more about guard clauses click here.
Every methods of the Guard class starting by "Requires" are used to validate method pre-conditions.
Use the RequiresNonNull method to display an error when receiving nulls.
public void Foo(Bar bar)
{
Guard.RequiresNonNull(bar, nameof(bar));
//...
}Use the Requires method to display an error on a custom condition.
public void Foo(int number)
{
Guard.Requires(() => number >= 0, "Receiving negative number.");
//...
}Use the RequiresNoNullIn method to display an error when receiving a sequence containing one or more null items.
public void Foo(IEnumerable<Bar> barSequence)
{
Guard.RequiresNoNullIn(barSequence, nameof(barSequence));
//...
}Use the RequiresAny and RequiresAll methods to display an error when all elements in a sequence do not meet a custom condition or any of the elements does not meet the condition.
public void Foo(IEnumerable<int> numbers)
{
Guard.RequiresAny(number => number > 0, "Receiving sequence containing negative numbers only.");
//...
}public void Foo(IEnumerable<int> numbers)
{
Guard.RequiresAll(number => number > 0, "Receiving sequence containing a negative number.");
//...
}Every methods of the Guard class starting by "Ensures" are used to validate method post-conditions.
Use the EnsuresNonNull method to display an error when returning nulls.
public Bar Foo()
{
Bar result;
// ...
Guard.EnsuresNonNull(result, "Returned null.");
return result;
}Use the Ensures method to display an error on a custom condition.
public int Foo()
{
int result;
// ...
Guard.Ensures(() => result > 0, "Returned negative number.");
return result;
}Use the EnsuresNoNullIn method to display an error when returning a sequence containing one or more null items.
public IEnumerable<Bar> Foo()
{
int result;
// ...
Guard.EnsuresNoNullIn(result, "Returned sequence containing null(s).");
return result;
}Use the EnsuresAny and EnsuresAll methods to display an error when all elements in a sequence do not meet a custom condition or any of the elements does not meet the condition.
public IEnumerable<int> Foo()
{
int result;
// ...
Guard.EnsuresAny(number => number > 0, "Returned sequence containing negative numbers only.");
return result;
}public IEnumerable<int> Foo()
{
int result;
// ...
Guard.EnsuresAll(number => number > 0, "Receiving sequence containing a negative number.");
return result;
}