Input Validation the Easy Way
I just stumbled across what looks like a really great library for performing input validation called CutingEdge.Conditions.
I don’t know about you, but I find myself putting "if (variable==null)" or something similar at the beginning of almost every method I write. Eventually I extracted the commonly-used validation codes out into static methods of a helper class, which helped. CuttingEdge.Conditions takes things a step further (ok, a few steps further) by providing a fluent interface so you can write code like this:
// Check all preconditions:
id.Requires("id")
.IsNotNull() // throws ArgumentNullException on failure
.IsInRange(1, 999) // ArgumentOutOfRangeException on failure
.IsNotEqualTo(128); // throws ArgumentException on failure
Very cool! Here’s the scoop for CuttingEdge.Conditions:
- Home on Codeplex with download link
- .NET Junkie’s blog posts about it here and here
- A CodeProject article about it
Looks sweet. I just downloaded it to give it a test drive.
Note: For those that (are forced to) worry about IP and licensing, it’s LGPL.
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply