-
Notifications
You must be signed in to change notification settings - Fork 4
/
2009-01-04-using-extension-methods-on-null-objects.html
12 lines (11 loc) · 4 KB
/
2009-01-04-using-extension-methods-on-null-objects.html
1
2
3
4
5
6
7
8
9
10
11
12
---
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">    <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">        <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">        {</p> <p style="margin: 0px">            <span style="color: #00bfff">if</span> (logger <span style="color: #a6a0e0">==</span> <span style="color: #00bfff">null</span>)</p> <p style="margin: 0px">                <span style="color: #00bfff">return</span> <span style="color: #00d2d2">NullLogger</span><span style="color: #a6a0e0">.</span>Instance;</p> <p style="margin: 0px">            <span style="color: #00bfff">else</span></p> <p style="margin: 0px">                <span style="color: #00bfff">return</span> logger;</p> <p style="margin: 0px">        }</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">        <span style="color: #00bfff">public</span> <span style="color: #00bfff">void</span> DoSomething()</p> <p style="margin: 0px">        {</p> <p style="margin: 0px">            Log<span style="color: #a6a0e0">.</span>Safe()<span style="color: #a6a0e0">.</span>WriteDebug(<span style="color: #fa8072">"I did something!"</span>);</p> <p style="margin: 0px">        }</p> </div> <p>Voila!</p>