Policeman Guard NuGet Package

This post is a simple article which shows how the use the Policeman.Guard NuGet package. The package can be used for simple checks in your methods to guard against null arguments, required conditions or whatever.

Code: https://github.com/damienbod/Policeman
NuGet Package: https://www.nuget.org/packages/Policeman.Guard/

The Requires guard method can be used to do any type of method checks. You can define the result exception if the guard fails. For example, the following example returns an ArgumentException exception.

Guard.Requires<ArgumentException>(
  "Expecting Exception of type ArgumentException", 
  (5 == 7)
);

Her’s another example showing the same as before, except I want to throw a PolicemanException exception if it fails.

private string HelloWorld(int importantParamWhichNeedsaGuard)
{
 // throws a PolicemanException exception if incorrect
 Guard.Requires<PolicemanException>(
  string.Format("Expecting Exception of type PolicemanException, arg= {0}", "importantParamWhichNeedsaGuard"),
  importantParamWhichNeedsaGuard == 7
 );

 // do your business logic.
 return "Your may continue your logic...";
}

Example of the ArgumentNotNull guard which throws an ArgumentNullException

TestClass _testClassNull;
Guard.ArgumentNotNull(_testClassNull, "Expecting Exception");

Example of the ArgumentNotNullOrEmpty guard which throws an ArgumentException

Guard.ArgumentNotNullOrEmpty("", "Expecting Exception");

Here’s a simple arithmetic check

Guard.Requires<ArithmeticException>(4 + 4 == 7);

Greater than or less than examples, both throw an ArgumentOutOfRangeException

Guard.ArgumentGreaterOrEqualThan(4,3,"noParam");
Guard.ArgumentLowerOrEqualThan(4, 4, "noParam");

Another example of a less than, greater than check.

private string HelloMinMax(int min, int max)
{
 Guard.ArgumentGreaterOrEqualThan(min, 3, "min");
 Guard.ArgumentLowerOrEqualThan(max, 4, "max");
			
 // Do you business logic
 return "Your may continue your logic...";
}

Future releases of this Guard
The hardest part is what should be included and what not. I don’t what to create a large Guard implementation which can guard against anything. For example maybe I could use reflection to get the parameter names instead of using strings, or supporting functions/actions might be a nice feature, chaining the guards maybe, or even range checks, class equals checks or whatever.

Of course there are other guard classes, libraries which work just as good, or better. The main thing is to have fun using them.

Links:

http://esmithy.net/2011/03/15/suppressing-ca1062/

https://github.com/3komma14/Guard/wiki

https://github.com/liteguard/liteguard/

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: