Thursday, October 25, 2007

Wednesday, October 24, 2007

Design by contract

Something that was bothering me for a while now was writing the following piece of code over and over again:

public void DoSomething(Request request) { if(null == request) { throw new ArgumentNullException("request"); } }

Last week, I decided to do something about it. In a previous post, I mentioned the concept of a fluent interface. So, I tried using it while finding a solution for writing this kind of boilerplate code. While I was half way through, I found this post from Sean Chambers (via DotNetKicks) that describes (almost) the same solution as I was developing using fluent interfaces. Although he beat me to it, his post confirmed that I was on the right track. Here goes ...  

Instead of writing the code above, I can now write this:

public void DoSomething(Request request) { // Throws ArgumentNullException Check.Argument(request, "request").IsNotNull(); }

I also created a couple of other checks:

Strings

public void DoSomethingElse(String request) { // Throws ArgumentException Check.Argument(emptyString, "request") .IsNotNullAndNotEmpty(); }

or

public void DoSomethingElse(String request) { // Throws ArgumentException Check.Argument(emptyString, "request") .IsNotNullAndNotEmptyAndNotWhiteSpace(); }

Collections

public void DoSomethingElse(ICollection requests) { // Throws ArgumentException Check.Argument(requests as ICollection, "requests") .IsNotNullAndNotEmpty(); }

or

public void DoSomethingElse(ICollection<String> requests) { // Throws ArgumentException Check.Argument(requests as ICollection<String>, "requests") .IsNotNullAndNotEmpty(); }

Enums

public void DoSomethingMore(DayOfWeek dayOfWeek) { // Throws InvalidEnumArgumentException Check.Argument(dayOfWeek as Enum, "dayOfWeek") .IsValidEnumValue(); }

Value types

public void DoSomethingMeaningfull(Int32 someNumber) { // Throws ArgumentOutOfRangeException Check.Argument(someNumber, "someNumber").IsNotNegative(); }

or

public void DoSomethingMeaningfull(Int32 someNumber) { // Throws ArgumentOutOfRangeException Check.Argument(someNumber, "someNumber").IsNotPositive(); }

or

public void DoSomethingMeaningfull(Int32 someNumber) { // Throws ArgumentOutOfRangeException Check.Argument(someNumber, "someNumber").IsNotZero(); }

You can download the code here. Please let me know what you think. I would love to hear your feedback. It's the only way I will ever learn how to become good programmer.

Saturday, October 06, 2007

ASP.NET MVC Framework at ALT.NET Conf

Scott Guthrie announced the new ASP.NET MVC framework at the Alt.Net conference today. I blogged about this earlier on. Jeffrey Palermo who is attending the conference has put some more details on his blog.

To me, this is the first great thing coming from Redmond in the last couple of years, besides generics and the .NET Framework itself. I hope they will put up some bits really soon. According to Jeffrey, Microsoft will release it as an add-on to Visual Studio 2008 sometime next year. Until now, I saw no reason for upgrading to Visual Studio 2008. With the ASP.NET MVC framework coming, it is almost a necessity for me.

Almost all the pains I had with WebForms are taken away. Pluggability and testability all around (yes, there is even an IHttpContext)!

I want to say thanks to the guys who created or contributed to MonoRail. They embodied the concerns that the .NET community had about ASP.NET and provided an innovative alternative. Although I like it a lot, I know that I could never use it for my professional projects (this because most Microsoft shops only like bits coming from Redmond, including the one I work at). Thanks to Scott Guthrie and his team, my fighting days with WebForms are soon coming to an end. Can't wait.

PS: Keep an eye on the CodeBetter blogs the next couple of days. I guess that they will be posting some great stuff regarding the Alt.Net conference.

Update 09/10/2007: Two videos of the ASP.NET MVC Framework at the ALT.NET Conf can be watched at the old Hanselman place.

Wednesday, October 03, 2007

Utilities.NET 1.0

The first release of Utilities.NET contains no less than 117 classes to make your coding efforts a little bit easier. After having a quick peek at the source code, it seems properly documented and nicely written. Very nice stuff!

This is huge

I was reading ScottGu's blog and the following post appeared:

Releasing the Source Code for the .NET Framework Libraries

After slapping myself in the face a few times, the post was still there. Wow! This is awesome! Also listen to this podcast for more information.