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