Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
14 lines (11 sloc) 3.58 KB
---
layout: post
title: An extension method, just because I can.
date: '2008-04-20T12:43:00.001+01:00'
tags: [patterns, csharp]
modified_time: '2008-04-20T12:43:34.660+01:00'
blogger_id: tag:blogger.com,1999:blog-4015568221071268916.post-5524975972244453378
comments: true
blogger_orig_url: http://serialseb.blogspot.com/2008/04/extension-method-just-because-i-can.html
---
<p>Going to the dentist is an awkward experience. You know you have to use their service or your teeth will rot, but the experience is just not enjoyable.</p> <p>That's how I feel about string.Format. I use it all over the place, but find it incredibly out of place. Thanks to extension methods though, I can propose a nicer syntax. Here's a snippet from a site I'm building for one of my clients, using my rasta REST framework and linq2sql.</p> <div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new"> <div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new"> <p style="margin: 0px">[<span style="color: #2b91af">HttpOperation</span>(<span style="color: #2b91af">HttpMethod</span>.GET, ContentType=<span style="color: blue">new</span> <span style="color: #2b91af">HttpContentType</span>(<span style="color: #a31515">&quot;image/*&quot;</span>))]</p> <p style="margin: 0px"><span style="color: blue">public</span> <span style="color: #2b91af">ResourceOperationResult</span> GetMovie(<span style="color: blue">string</span> filmName)</p> <p style="margin: 0px">{</p> <p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">var</span> movie = _movies.FindOne(movie =&gt; movie.UrlTitle == filmName);</p> <p style="margin: 0px">&#160;</p> <p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">if</span> (movie == <span style="color: blue">null</span>)</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ResourceNotFoundException</span> </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; Message = <span style="color: #a31515">&quot;The movie {0} wasn't found in the database.&quot;</span>.With(filmName) </p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</p> <p style="margin: 0px">&#160;</p> <p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">ResourceMovedTemporarilyResult</span> { NewResourceUrl=<span style="color: #a31515">&quot;/library/tempfilm.jpg&quot;</span> };</p> <p style="margin: 0px">}</p> </div> </div> <p>Notice the extension method there? I just find it sweet. Here's the very simple extension method.</p> <div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new"> <p style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">string</span> With(<span style="color: blue">this</span> <span style="color: blue">string</span> text, <span style="color: blue">params</span> <span style="color: blue">object</span>[] values)</p> <p style="margin: 0px">{</p> <p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">string</span>.Format(text, values);</p> <p style="margin: 0px">}</p> </div> <p>There you are, a shamefully simple extension method, just because I can!</p>