Skip to content

SolidStack.Core.Guards

Maxime Gélinas edited this page Jun 2, 2018 · 4 revisions

Overview

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.

Table of contents

Getting Started

What does it look like?

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;
}

Where can I get it?

First, install NuGet. Then, install SolidStack.Core.Guards from the package manager console:

PM> Install-Package SolidStack.Core.Guards

Protecting your methods with the Guard class

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.

Protecting against pre-conditions

Every methods of the Guard class starting by "Requires" are used to validate method pre-conditions.

Protecting against single values

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.");
	//...
}

Protecting against sequences

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.");
	//...
}

Protecting against post-conditions

Every methods of the Guard class starting by "Ensures" are used to validate method post-conditions.

Protecting against single values

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;
}

Protecting against sequences

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;
}