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...

publicstringFoo(IEnumerable<string>sequence){Guard.RequiresNonNull(sequence,nameof(sequence));Guard.RequiresAll(sequence, item =>!string.IsNullOrEmpty(item));varresult=Bar(sequence);Guard.Ensures(()=>!string.IsNullOrEmpty(result),"Bar() returned an invalid string.");returnresult;}

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.

publicvoidFoo(Barbar){Guard.RequiresNonNull(bar,nameof(bar));//...}

Use the Requires method to display an error on a custom condition.

publicvoidFoo(intnumber){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.

publicvoidFoo(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.

publicvoidFoo(IEnumerable<int>numbers){Guard.RequiresAny(number =>number>0,"Receiving sequence containing negative numbers only.");//...}
publicvoidFoo(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.

publicBarFoo(){Barresult;// ...Guard.EnsuresNonNull(result,"Returned null.");returnresult;}

Use the Ensures method to display an error on a custom condition.

publicintFoo(){intresult;// ...Guard.Ensures(()=>result>0,"Returned negative number.");returnresult;}

Protecting against sequences

Use the EnsuresNoNullIn method to display an error when returning a sequence containing one or more null items.

publicIEnumerable<Bar>Foo(){intresult;// ...Guard.EnsuresNoNullIn(result,"Returned sequence containing null(s).");returnresult;}

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.

publicIEnumerable<int>Foo(){intresult;// ...Guard.EnsuresAny(number =>number>0,"Returned sequence containing negative numbers only.");returnresult;}
publicIEnumerable<int>Foo(){intresult;// ...Guard.EnsuresAll(number =>number>0,"Receiving sequence containing a negative number.");returnresult;}