Every time I wrote validation this code I think, I should write a library to do this for me. So I went ahead and wrote this simple class. The idea is to use the Introduce Assertion Refactoring in this cases and write this code in this way.
Instead of writing?
Now you can write
If the parameter is null an Exception of type NullReferenceException will be thrown. Of course, this is not the right Exception in this context, so you should do this.
Of course, I may want to check more than one thing at a time, and I may not want to throw an Exception but look at the errors and pass them all at once to a higher tier, maybe a UI so the user can fix the problem. In that case I can use it like this.
At this moment the Check class has methods to validate String, IEnumerable, IEnumerable<T>, object, int, decimal, double and DateTime. This is not complete by any stretch of the imagination but I like it so far.
You can download a Release of my base library on Google code.
You can also download the code from: http://code.google.com/p/latrompa/source/checkout