Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
13 lines (11 sloc) 4 KB
---
layout: post
title: Using extension methods on null objects
date: '2009-01-04T13:54:00.001Z'
tags: [patterns, csharp]
modified_time: '2009-01-04T13:54:06.698Z'
blogger_id: tag:blogger.com,1999:blog-4015568221071268916.post-5153502591890145655
comments: true
blogger_orig_url: http://serialseb.blogspot.com/2009/01/using-extension-methods-on-null-objects.html
---
<p>One feature that is often disregarded in extension methods is that they get called for null objects. While instrumenting parts of OpenRasta, I faced the problem of having some classes not always created through the container. As such, sometimes their Log property would be null, and each call to a logging method would have to check for that condition. After doing a few back-and-forth, I settled on using an extension method, coupled with a <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">null object</a>.</p> <p>Take a simple class, and add a Log property.</p> <div style="padding-right: 10px; padding-left: 10px; font-size: 10pt; background: #252525; padding-bottom: 10px; color: #e0e0e0; padding-top: 10px; font-family: consolas, courier new"> <p style="margin: 0px"><span style="color: #00bfff">public</span> <span style="color: #00bfff">class</span> <span style="color: #00d2d2">LoggingClass</span></p> <p style="margin: 0px">{</p> <p style="margin: 0px">&#160;&#160;&#160; <span style="color: #00bfff">public</span> <span style="color: #92e4d6">ILogger</span> Log { <span style="color: #00bfff">get</span>; <span style="color: #00bfff">set</span>; }</p> <p style="margin: 0px">}</p> </div> <p>Then, create a simple extension method on the ILogger, and return a null object</p> <p></p> <div style="padding-right: 10px; padding-left: 10px; font-size: 10pt; background: #252525; padding-bottom: 10px; color: #e0e0e0; padding-top: 10px; font-family: consolas, courier new"> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #00bfff">public</span> <span style="color: #00bfff">static</span> <span style="color: #92e4d6">ILogger</span> Safe(<span style="color: #00bfff">this</span> <span style="color: #92e4d6">ILogger</span> logger)</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #00bfff">if</span> (logger <span style="color: #a6a0e0">==</span> <span style="color: #00bfff">null</span>)</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #00bfff">return</span> <span style="color: #00d2d2">NullLogger</span><span style="color: #a6a0e0">.</span>Instance;</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #00bfff">else</span></p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #00bfff">return</span> logger;</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p> </div> <p>Now all the logging code can simply call the Safe() method to ensure that, in the absence of a logger, a null object will be used instead. The code using it looks like the following.</p> <div style="padding-right: 10px; padding-left: 10px; font-size: 10pt; background: #252525; padding-bottom: 10px; color: #e0e0e0; padding-top: 10px; font-family: consolas, courier new"> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #00bfff">public</span> <span style="color: #00bfff">void</span> DoSomething()</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Log<span style="color: #a6a0e0">.</span>Safe()<span style="color: #a6a0e0">.</span>WriteDebug(<span style="color: #fa8072">&quot;I did something!&quot;</span>);</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p> </div> <p>Voila!</p>