Thursday, April 16, 2009

Null Object Pattern using .NET

I was doing code reviews in one of my projects and I found a pattern of code that kept repeating itself. The code went something like this

if (Quote != null)


//do something accessing properties of Quote


The above code does not seem too bad does it? But when the object linkages increases to a couple of levels; i.e. Quote has an object Vehicle which in turn has an object engine and you want to find out the make and model of the engine imagine the number of If blocks that you will end up writing.

This was precisely the developer’s problem. Something told me that there has to be a better way of doing this and a quick google lead me to the Null Object Pattern. I am not going to go into the details of what it is because I am sure google can give you umpteen number of links. What I am going to show you though is one of my approaches to implement this Null Object Pattern in your code using .NET.

I am going to use a console application and the precise example I have quoted above. Let us consider the following scenario

There is a Quote class. The getVehicle of the quote class populates a vehicle. The vehicle object in turn has an engine object. The objective is to print out the cc and the horse power of the engine.

The definition of the classes are as shown below

EngineClass VehicleClass QuoteClass

And as a developer the following is the code I write to display the cc of the engine.


When you execute the above code, you will get an exception in the Console.WriteLine statement. If I were to handle this exception, the best way is to add an if block to first check if testvehicle and engine is null and then write cc only if they are not null. the other option is to catch a NullReference exception but catching exceptions is a costly affair. So what do we do so that the code executes even if the object being accessed is null?

The first step and this is optional is that I declare a INullable interface. This is just to ensure that we are consistent with .NET’s own nullable types. This INullable interface just has one method – isNull. The interface declaration is as shown below 


All objects that need to expose the null representation inherit this INullable interface. To ensure my developers do not have to write a lot of code I decided to create a generic NullObject class with the code shown below


Most of the code is self-explanatory.  In my returnNull method, I just instantiate a new type and return that type. I would just like to grab your attention to the fact that I have inherited the INullable interface and returned true. Once this class is written, all that the developer needs to do in his code is add a couple of lines. My new engine class is as below

NullEngineClass As you can see the difference now is just that I have added a static method call nullObject that will call the generic NullObject’s returnnull method. By now you have probably guessed what I am going to do in the vehicle class, so I am not going to withhold that any longer. 

NullVehicleClass Thats it!!! You run your application now and voila!!! The same line does not throw an error. It just says 0 which is perfectly acceptable. I hope you put this to the best use because personally I think this can relieve us of a lot of lines of code. Do let me know your thoughts on this. Happy Coding!!!!





Saturday, April 4, 2009

How to: Managing multiple blogs with Windows Live Writer

A couple of people have asked me how I manage and maintain my blog posts on both Windows Spaces and blogspot, so predictably, I thought I will blog that myself.

I use Windows Live Writer to write my blogs. The tool is quite useful. Once you install the software, you get the option to configure your blogs. If you do not set all the accounts the first time, don’t worry, you can always access it from Tools –> Accounts. You get the following dialog.


As you can see, I have both my accounts configured. To add an account click add and the rest of it pretty self explanatory. Once you have added the account, you should be able to see the accounts you have added under the Blogs menu.


The Live Writer tool also loads the themes you have configured which helps you see exactly how your blog will look. The tick needless to say indicates the current blog you are writing to. Once you are finished writing the blog, publish the blog, go to the blogs menu select the other blogs you want to publish it to and click publish again. As easy as that!!!

I have not tested the software in Windows 7 yet. I will do that pretty soon and post my results here!! So keep visiting. Happy Blogging.