<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><!-- generator="wordpress/2.2.1" --><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Haugerns Development Escapades</title>
	<link>http://www.haugern.net/blog</link>
	<description>Morten Haug on development and other curiosities</description>
	<pubDate>Wed, 19 Nov 2008 20:50:23 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/DevelopmentEscapades" type="application/rss+xml" /><item>
		<title>Mystery Code I</title>
		<link>http://feeds.feedburner.com/~r/DevelopmentEscapades/~3/458803346/</link>
		<comments>http://www.haugern.net/blog/mystery-code-i/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 20:50:23 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
		
		<category><![CDATA[mystery code]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/mystery-code-i/</guid>
		<description><![CDATA[There are a lot of ways to make sure your .NET Windows application just has one instance running, and we picked the one described here. It was working flawlessly until someone tried running it on a terminal where it crashed mysteriously.
What happened? Can you spot the error?
]]></description>
			<content:encoded><![CDATA[<p>There are a lot of ways to make sure your .NET Windows application just has one instance running, and we picked the <a href="http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx" target="_blank">one described here</a>. It was working flawlessly until someone tried running it on a terminal where it crashed mysteriously.</p>
<p>What happened? Can you spot the error?</p>
<img src="http://feeds.feedburner.com/~r/DevelopmentEscapades/~4/458803346" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/mystery-code-i/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.haugern.net/blog/mystery-code-i/</feedburner:origLink></item>
		<item>
		<title>ReSharper messes up your Visual Studio shortcuts</title>
		<link>http://feeds.feedburner.com/~r/DevelopmentEscapades/~3/433511907/</link>
		<comments>http://www.haugern.net/blog/resharper-messes-up-your-visual-studio-shortcuts/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 12:17:26 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
		
		<category><![CDATA[resharper]]></category>

		<category><![CDATA[visual studio]]></category>

		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/resharper-messes-up-your-visual-studio-shortcuts/</guid>
		<description><![CDATA[I must start stating I’m a huge fan of JetBrains’ ReSharper for Visual Studio, it certainly makes me more productive in my day-to-day coding tasks.
None the less, today I was just a little bit frustrated when upgrading from 4.0 to 4.1. I have a couple of custom shortcuts which I don’t mind retaining. But ReSharper [...]]]></description>
			<content:encoded><![CDATA[<p>I must start stating I’m a huge fan of <a href="http://www.jetbrains.com/resharper/index.html" target="_blank">JetBrains’ ReSharper for Visual Studio</a>, it certainly makes me more productive in my day-to-day coding tasks.</p>
<p>None the less, today I was just a little bit frustrated when upgrading from 4.0 to 4.1. I have a couple of custom shortcuts which I don’t mind retaining. But ReSharper happily overwrites it all with default settings, which was quite annoying. The least I’d expect would be a message telling me it would (or didn’t I read the setup correctly?) do it, and I’d happily save my settings and restore them after the upgrade.</p>
<p>I see that others have complained as well, and you will find <a href="http://intellij.net/forums/message.jspa?messageID=5221710" target="_blank">JetBrains official statement here</a>. MS &amp; JetBrains should really figure this out.</p>
<p>So if you’re upgrading ReSharper these days, please back up your VS-settings first!</p>
<img src="http://feeds.feedburner.com/~r/DevelopmentEscapades/~4/433511907" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/resharper-messes-up-your-visual-studio-shortcuts/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.haugern.net/blog/resharper-messes-up-your-visual-studio-shortcuts/</feedburner:origLink></item>
		<item>
		<title>Beware of the IEnumerables</title>
		<link>http://feeds.feedburner.com/~r/DevelopmentEscapades/~3/423527636/</link>
		<comments>http://www.haugern.net/blog/beware-of-the-ienumerables/#comments</comments>
		<pubDate>Fri, 17 Oct 2008 08:58:00 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
		
		<category><![CDATA[c#]]></category>

		<category><![CDATA[dotnet]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/beware-of-the-ienumerables/</guid>
		<description><![CDATA[Some time ago I wrote about the difference between List and Collection, and how you should use them. Usually you want to keep the details away from the users of your class, and an even tighter way to encapsulate a list of something is to expose IEnumerable&#60;T&#62;. Note that both IList&#60;T&#62; and ICollection&#60;T&#62; inherits this [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I wrote about <a href="http://www.haugern.net/blog/net-collectiont-vs-listt/">the difference between List and Collection</a>, and how you should use them. Usually you want to keep the details away from the users of your class, and an even tighter way to encapsulate a list of something is to expose <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>. Note that both IList&lt;T&gt; and ICollection&lt;T&gt; inherits this interface, so IEnumerable&lt;T&gt; exposes even less details to the user. Another benefit is now you can use <a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx" target="_blank">yield</a> behind the scenes to create your “list” on the fly.</p>
<p>But this also its pitfall. What do you think will be the output of this code?</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red255\green255\blue255;\red0\green0\blue0;\red255\green128\blue0;\red128\green128\blue64;\red43\green145\blue175;\red0\green255\blue0;}??\fs24 \cf1\cb2\highlight2   \cf3 static\cf1  \cf3 void\cf1  \cf4 Main\cf1 (\cf3 string\cf1 [] \cf4 args\cf1 )\par ??  \{\par ??      \cf3 var\cf1  \cf4 ola\cf1  = \cf3 new\cf1  \cf5 Pupil\cf1 ()\{\cf4 Name\cf1 =\cf6 "Ola"\cf1 , \cf4 IsMate\cf1  = \cf3 false\cf1 \};\par ??      \cf3 var\cf1  \cf4 trond\cf1  = \cf3 new\cf1  \cf5 Pupil\cf1 () \{\cf4 Name\cf1  = \cf6 "Trond"\cf1 , \cf4 IsMate\cf1  = \cf3 true\cf1 \};\par ??      \cf3 var\cf1  \cf4 pupils\cf1  = \cf3 new\cf1  \cf5 List\cf1 &lt;\cf5 Pupil\cf1 &gt;()\{\cf4 ola\cf1 ,\cf4 trond\cf1 \};\par ??\par ??      \cf5 IEnumerable\cf1 &lt;\cf3 string\cf1 &gt; \cf4 names\cf1  = \cf4 ClassMateNames\cf1 (\cf4 pupils\cf1 );\par ??\par ??      \cf3 foreach\cf1  (\cf3 string\cf1  \cf4 name\cf1  \cf3 in\cf1  \cf4 names\cf1 )\par ??      \{\par ??          \cf5 Console\cf1 .\cf4 WriteLine\cf1 (\cf4 name\cf1 );\par ??      \}\par ??  \}\par ??\par ??  \cf3 private\cf1  \cf3 static\cf1  \cf5 IEnumerable\cf1 &lt;\cf3 string\cf1 &gt; \cf4 ClassMateNames\cf1 (\cf5 IEnumerable\cf1 &lt;\cf5 Pupil\cf1 &gt; \cf4 pupils\cf1 )\par ??  \{\par ??      \cf3 foreach\cf1  (\cf5 Pupil\cf1  \cf4 pupil\cf1  \cf3 in\cf1  \cf4 pupils\cf1 )\par ??      \{\par ??          \cf3 if\cf1  (\cf4 pupil\cf1 .\cf4 IsMate\cf1 )\par ??              \cf3 yield\cf1  \cf3 return\cf1  \cf4 pupil\cf1 .\cf4 Name\cf1 ;\par ??      \}\par ??  \}\par ??}<br />
--></p>
<div style="font-size: 8pt; background: black; color: white; font-family: consolas">
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 10</span>&#160;&#160; <span style="color: #ff8000">static</span> <span style="color: #ff8000">void</span> <span style="color: #808040">Main</span>(<span style="color: #ff8000">string</span>[] <span style="color: #808040">args</span>)</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 11</span>&#160;&#160; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ff8000">var</span> <span style="color: #808040">ola</span> = <span style="color: #ff8000">new</span> <span style="color: #2b91af">Pupil</span>(){<span style="color: #808040">Name</span>=<span style="color: lime">&quot;Ola&quot;</span>, <span style="color: #808040">IsMate</span> = <span style="color: #ff8000">false</span>};</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ff8000">var</span> <span style="color: #808040">trond</span> = <span style="color: #ff8000">new</span> <span style="color: #2b91af">Pupil</span>() {<span style="color: #808040">Name</span> = <span style="color: lime">&quot;Trond&quot;</span>, <span style="color: #808040">IsMate</span> = <span style="color: #ff8000">true</span>};</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ff8000">var</span> <span style="color: #808040">pupils</span> = <span style="color: #ff8000">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Pupil</span>&gt;(){<span style="color: #808040">ola</span>,<span style="color: #808040">trond</span>};</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 15</span>&#160;</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #ff8000">string</span>&gt; <span style="color: #808040">names</span> = <span style="color: #808040">ClassMateNames</span>(<span style="color: #808040">pupils</span>);</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 17</span>&#160;</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ff8000">foreach</span> (<span style="color: #ff8000">string</span> <span style="color: #808040">name</span> <span style="color: #ff8000">in</span> <span style="color: #808040">names</span>)</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.<span style="color: #808040">WriteLine</span>(<span style="color: #808040">name</span>);</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 22</span>&#160;&#160; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 23</span>&#160;</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 24</span>&#160;&#160; <span style="color: #ff8000">private</span> <span style="color: #ff8000">static</span> <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #ff8000">string</span>&gt; <span style="color: #808040">ClassMateNames</span>(<span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Pupil</span>&gt; <span style="color: #808040">pupils</span>)</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 25</span>&#160;&#160; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ff8000">foreach</span> (<span style="color: #2b91af">Pupil</span> <span style="color: #808040">pupil</span> <span style="color: #ff8000">in</span> <span style="color: #808040">pupils</span>)</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ff8000">if</span> (<span style="color: #808040">pupil</span>.<span style="color: #808040">IsMate</span>)</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ff8000">yield</span> <span style="color: #ff8000">return</span> <span style="color: #808040">pupil</span>.<span style="color: #808040">Name</span>;</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 31</span>&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Yes, you’re right, the output is my only friend “Trond”:</p>
<p><a href="http://www.haugern.net/blog/wp-content/image.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="118" alt="image" src="http://www.haugern.net/blog/wp-content/image-thumb.png" width="244" border="0" /></a> </p>
</p>
<p>But when are we really finding out who my friends are? The most natural thing is that I find them when I call <font face="Consolas">ClassMateNames</font>, as it is called first. That’s what I’d think anyway. But what if got in a fight with “Trond” between when I sort out my friends and list them to the console? </p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red255\green255\blue255;\red0\green0\blue0;\red255\green128\blue0;\red128\green128\blue64;\red43\green145\blue175;}??\fs24 \cf1\cb2\highlight2   \cf3 private\cf1  \cf3 static\cf1  \cf3 void\cf1  \cf4 FightWith\cf1 (\cf5 Pupil\cf1  \cf4 pupil\cf1 )\par ??  \{\par ??      \cf4 pupil\cf1 .\cf4 IsMate\cf1  = \cf3 false\cf1 ;\par ??  \}\par ??}<br />
--></p>
<div style="font-size: 8pt; background: black; color: white; font-family: consolas">
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 35</span>&#160;&#160; <span style="color: #ff8000">private</span> <span style="color: #ff8000">static</span> <span style="color: #ff8000">void</span> <span style="color: #808040">FightWith</span>(<span style="color: #2b91af">Pupil</span> <span style="color: #808040">pupil</span>)</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 36</span>&#160;&#160; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #808040">pupil</span>.<span style="color: #808040">IsMate</span> = <span style="color: #ff8000">false</span>;</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 38</span>&#160;&#160; }</p>
</p></div>
</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red255\green128\blue0;\red128\green128\blue64;}??\fs24 \cf1\cb2\highlight2   \cf3 IEnumerable\cf1 &lt;\cf4 string\cf1 &gt; \cf5 names\cf1  = \cf5 ClassMateNames\cf1 (\cf5 pupils\cf1 );\par ??\par ??  \cf5 FightWith\cf1 (\cf5 trond\cf1 );\par ??\par ??  \cf4 foreach\cf1  (\cf4 string\cf1  \cf5 name\cf1  \cf4 in\cf1  \cf5 names\cf1 )\par ??  \{\par ??      \cf3 Console\cf1 .\cf5 WriteLine\cf1 (\cf5 name\cf1 );\par ??  \}\par ??}<br />
--></p>
<div style="font-size: 8pt; background: black; color: white; font-family: consolas">
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 16</span>&#160;&#160; <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #ff8000">string</span>&gt; <span style="color: #808040">names</span> = <span style="color: #808040">ClassMateNames</span>(<span style="color: #808040">pupils</span>);</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 17</span>&#160;</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 18</span>&#160;&#160; <span style="color: #808040">FightWith</span>(<span style="color: #808040">trond</span>);</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 19</span>&#160;</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 20</span>&#160;&#160; <span style="color: #ff8000">foreach</span> (<span style="color: #ff8000">string</span> <span style="color: #808040">name</span> <span style="color: #ff8000">in</span> <span style="color: #808040">names</span>)</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 21</span>&#160;&#160; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.<span style="color: #808040">WriteLine</span>(<span style="color: #808040">name</span>);</p>
<p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 23</span>&#160;&#160; }</p>
</p></div>
</p>
<p>This gives me these friends:</p>
<p><a href="http://www.haugern.net/blog/wp-content/image1.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="126" alt="image" src="http://www.haugern.net/blog/wp-content/image-thumb1.png" width="244" border="0" /></a> </p>
</p>
<p>How about that! The information that I no longer is friends with “Trond” has changed the list I thought I retrieved with the call to <font face="Consolas">ClassMateNames</font>. What is happening is called <a href="http://www.google.no/search?hl=no&amp;q=deferred+execution&amp;meta=" target="_blank">deferred execution</a>, which delays the actual execution of the <a href="http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx" target="_blank">internals</a> in the <font face="Consolas">ClassMateNames</font> to when I iterate it. This is exactly how LINQ to [insert TLA here] operate as well, so nothing is actually fetched until you need it.</p>
<p>In my case this was an advantage, because at the time I was listing my friends, I didn’t actually have any!</p>
<h3>And the problem is?</h3>
<p>The problem starts when you before and/or after the call to <font face="Consolas">ClassMateNames</font> set up and tear down a session, connection, or anything that the inside of the iterator rely on. Any fetching of IEnumerables <a href="http://www.haugern.net/blog/using-the-using-statement-and-pattern-in-c/" target="_blank">inside using-blocks</a> should also be considered smells in this regard. If you should clear the list of pupils right after you have sent it to <font face="Consolas">ClassMateNames</font> (you don’t need it anymore right?), you have introduced a very subtle bug as the list is empty at execution.</p>
<p>The advice are thus: IEnumerables and yield is definitively great tools, but be aware of the inherent difficulties and quirks of deferred execution and use them with caution both as producer and consumer!</p>
<img src="http://feeds.feedburner.com/~r/DevelopmentEscapades/~4/423527636" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/beware-of-the-ienumerables/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.haugern.net/blog/beware-of-the-ienumerables/</feedburner:origLink></item>
		<item>
		<title>MSDN Live with Steve Ballmer keynote</title>
		<link>http://feeds.feedburner.com/~r/DevelopmentEscapades/~3/409528453/</link>
		<comments>http://www.haugern.net/blog/msdn-live-with-steve-ballmer-keynote/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 19:05:04 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
		
		<category><![CDATA[oo principles]]></category>

		<category><![CDATA[conferences]]></category>

		<category><![CDATA[dotnet]]></category>

		<category><![CDATA[learning]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/msdn-live-with-steve-ballmer-keynote/</guid>
		<description><![CDATA[MSDN Live has toured Norway this fall, with a handful of presentations for .NET developers. I attended the last one in Oslo on Tuesday, where Steve Ballmer made the keynote!
Most developers have seen the developers, developers, developers talk, and we had high expectations for this keynote. He was quite enthusiastic in Oslo as well, telling [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.haugern.net/blog/wp-content/windowslivewritermsdnlivewithsteveballmerkeynote-115e2ballmer-2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="254" alt="ballmer" src="http://www.haugern.net/blog/wp-content/windowslivewritermsdnlivewithsteveballmerkeynote-115e2ballmer-thumb.jpg" width="263" align="left" border="0" /></a>MSDN Live has toured Norway this fall, with a handful of presentations for .NET developers. I attended the last one in Oslo on Tuesday, where Steve Ballmer made the keynote!</p>
<p>Most developers have seen the <a href="http://www.youtube.com/watch?v=KMU0tzLwhbE">developers, developers, developers</a> talk, and we had high expectations for this keynote. He was quite enthusiastic in Oslo as well, telling us about things he thinks will influence our industry in the foreseeable future. <a href="http://www.youtube.com/results?search_query=ballmer+oslo&amp;search_type=&amp;aq=f">This search at Youtube seems to find the whole show</a>.</p>
<p>The rest of the day I attended the main track, starting out with a Silverlight 2 presentation from <a href="http://blog.goeran.no/">G&#248;ran Hansen</a>. While Silverlight 1 was mostly about animations and media, I didn&#8217;t give much attention. But the second version looks promising, with the CLR and a subset of the .NET libraries present. What I miss though, is maybe some background about the fact that running .NET WinForms applications in IE in object-tags has been possible a long time (without the automatic deployment of the framework). Props however to MS for finally supporting more than Windows &amp; IE only! G&#248;ran did a great job presenting, I really enjoyed his presence on stage.</p>
<p>Next up was another presentation from G&#248;ran, which gave the audience an introduction to <a href="http://msdn.microsoft.com/en-us/library/cc488545.aspx">ASP.NET Dynamic Data</a>. For those of us who attended <a href="http://www.haugern.net/blog/norwegian-developer-conference-2008-review/">NDC2008</a> and <a href="http://www.hanselman.com/blog/">Scott Hanselmans</a> keynote there, it was plain repetition. The only difference might be that this was showing the final bits released with SP1 of the framework, and Scott showing us some earlier version. It seems like a great technology to get you up and running very quick, but I wonder how many pages you end up with in the CustomPage directory after a while. Even though the topic didn&#8217;t tickle my fancy, G&#248;ran did a great job on this one as well.</p>
<p>Then it was time for lunch, rather late according to my stomach. But it tasted good, and there seemed to be enough to fill up all us starving geeks. As always during the breaks, old colleagues and friends sneak up on you and says hello. Which I really do appreciate!</p>
<p>After the break, <a href="http://andersnoras.com/blogs/anoras/default.aspx">Anders Nor&#229;s</a> gave a talk about SOA. More specifically about how we should start thinking along the lines of <a href="http://en.wikipedia.org/wiki/Enterprise_Service_Bus">enterprise service buses</a> and leave our old crusty webservices in the dust. I really do concur with a lot his ideas, and I&#8217;m always happy to see code with binsor-love. I also liked his onion-architecture, which resembles my standard layered architecture (no, not the &quot;standard standard&quot;). Hmmm, looks like another blog post (again).</p>
<p>&quot;Debugging your debugging habits&quot; was up next with Rune Zakariassen from Microsoft. He showed us some tips &amp; tricks for your debugging needs in Visual Studio, and presented a step-by-step recipe on how you best can find bugs in your code. I thought it was a nice presentation, even though my only real takeaway was the tracepoint functionality in VS. I&#8217;ve been a sucker for Debug.Writeline when I don&#8217;t want to impact the run of things stepping through, but this gives me an even less intrusive way keeping track of how the program executes.</p>
<p>Finally, G&#248;ran entered the stage for the last time this day. He showed us how WPF databinding can be used with a variant of Model-View-Presenter called Presentation Model. The emphasis was really on this pattern, and how it helps attain <a href="http://en.wikipedia.org/wiki/Separation_of_concerns">SoC</a>. He also mentioned <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">DIP</a> with the short version &quot;Program to an interface, not an implementation&quot;. A good thing can&#8217;t be said enough so here is the original:</p>
<blockquote><p>High level modules should not depend upon low level modules. Both should depend upon abstractions.</p>
<p>Abstractions should not depend upon details. Details should depend upon abstractions.</p>
</blockquote>
<p>All things considered, yet another great talk from G&#248;ran, keep up the good work and preach on about those design patterns and OO principles!</p>
<p>Yet again, great work by <a href="http://blogs.msdn.com/grothaug/default.aspx">Rune @ Microsoft</a> arranging this event. Until next time, I&#8217;m on twitter at <a href="http://twitter.com/haugern">http://twitter.com/haugern</a>.</p>
<img src="http://feeds.feedburner.com/~r/DevelopmentEscapades/~4/409528453" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/msdn-live-with-steve-ballmer-keynote/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.haugern.net/blog/msdn-live-with-steve-ballmer-keynote/</feedburner:origLink></item>
		<item>
		<title>Late to the Twitter-game</title>
		<link>http://feeds.feedburner.com/~r/DevelopmentEscapades/~3/407519384/</link>
		<comments>http://www.haugern.net/blog/late-to-the-twitter-game/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 18:47:37 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
		
		<category><![CDATA[blog]]></category>

		<category><![CDATA[internet]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/late-to-the-twitter-game/</guid>
		<description><![CDATA[I&#8217;ve finally caved in and created a Twitter-account today, you can follow me at http://twitter.com/haugern!

]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve finally caved in and created a Twitter-account today, you can follow me at <a href="http://twitter.com/haugern">http://twitter.com/haugern</a>!</p>
<p><a href="http://twitter.com"><img src="https://assets1.twitter.com/images/twitter_logo_s.png" /></a></p>
<img src="http://feeds.feedburner.com/~r/DevelopmentEscapades/~4/407519384" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/late-to-the-twitter-game/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.haugern.net/blog/late-to-the-twitter-game/</feedburner:origLink></item>
		<item>
		<title>Introducing my Bookshelf</title>
		<link>http://feeds.feedburner.com/~r/DevelopmentEscapades/~3/372232157/</link>
		<comments>http://www.haugern.net/blog/introducing-my-bookshelf/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 22:12:51 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
		
		<category><![CDATA[books]]></category>

		<category><![CDATA[learning]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/introducing-my-bookshelf/</guid>
		<description><![CDATA[I have for a long time been very up front with everything I was aware of I didn&#8217;t know much about. As a countermeasure and a late follow up to Sources of Software Development learning (About Me), I&#8217;ve listed some books I&#8217;ve got in my bookshelf in my home office.
Let it be your starting point [...]]]></description>
			<content:encoded><![CDATA[<p>I have for a long time been very up front with everything I was aware of <a href="http://www.haugern.net/blog/list-of-my-known-unknowns/">I didn&#8217;t know much about</a>. As a countermeasure and a late follow up to <a href="http://www.haugern.net/blog/sources-for-software-development-resources-about-me/">Sources of Software Development learning (About Me)</a>, I&#8217;ve listed some books I&#8217;ve got in my <a href="http://www.haugern.net/blog/bookshelf/">bookshelf</a> in my home office.</p>
<p>Let it be your starting point into the great resource of reading books. Blogs are great, but books really are in a different league when it comes to polish, hard facts, and references. I really find it a lot easier to get immersed in a topic when reading from a book than from my screen, and of course you have the added bonus of being offline while reading.</p>
<p>Amazon is your allied (especially now when the $ is really cheap for us Norwegians); go make your book order today!</p>
<img src="http://feeds.feedburner.com/~r/DevelopmentEscapades/~4/372232157" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/introducing-my-bookshelf/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.haugern.net/blog/introducing-my-bookshelf/</feedburner:origLink></item>
		<item>
		<title>Copy Source As Html for Visual Studio 2008</title>
		<link>http://feeds.feedburner.com/~r/DevelopmentEscapades/~3/342897231/</link>
		<comments>http://www.haugern.net/blog/copy-source-as-html-for-visual-studio-2008/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 20:51:19 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
		
		<category><![CDATA[visual studio]]></category>

		<category><![CDATA[tools]]></category>

		<category><![CDATA[blog]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/copy-source-as-html-for-visual-studio-2008/</guid>
		<description><![CDATA[I have been &#34;stuck&#34; in Visual Studio 2005 for a while, but when we now jumped to Visual Studio 2008 recently, I missed a tool I&#8217;ve been using to blog my code. As the heading states, this is Copy Source As HTML. They have not released an updated version, but Guy Burstein comes to the [...]]]></description>
			<content:encoded><![CDATA[<p>I have been &quot;stuck&quot; in Visual Studio 2005 for a while, but when we now jumped to Visual Studio 2008 recently, I missed a tool I&#8217;ve been using to blog my code. As the heading states, this is <a href="http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/" target="_blank">Copy Source As HTML</a>. They have not released an updated version, but <a href="http://blogs.microsoft.co.il/blogs/bursteg/" target="_blank">Guy Burstein</a> comes to the rescue with <a href="http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/11/21/copy-source-as-html-copysourceashtml-for-visual-studio-2008-rtm.aspx" target="_blank">an updated addin</a>.</p>
<p>Reading the comments it can seem to be some different &quot;opinions&quot; about where to put it, but I just made an Addin directory under Visual Studio 2008 in My Documents in Windows XP and extracted it there.</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green128\blue0;\red0\green0\blue0;}??\fs24 \cf1\cb2\highlight2 //And it works like a charm!}<br />
--></p>
<div style="font-size: 10pt; background: black; color: white; font-family: consolas">
<p style="margin: 0px"><span style="color: green">//And it works like a charm!</span></p>
</p></div>
<img src="http://feeds.feedburner.com/~r/DevelopmentEscapades/~4/342897231" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/copy-source-as-html-for-visual-studio-2008/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.haugern.net/blog/copy-source-as-html-for-visual-studio-2008/</feedburner:origLink></item>
		<item>
		<title>Norwegian Developer Conference 2008 Review</title>
		<link>http://feeds.feedburner.com/~r/DevelopmentEscapades/~3/319121933/</link>
		<comments>http://www.haugern.net/blog/norwegian-developer-conference-2008-review/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 19:44:07 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
		
		<category><![CDATA[c#]]></category>

		<category><![CDATA[conferences]]></category>

		<category><![CDATA[agile]]></category>

		<category><![CDATA[dotnet]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/norwegian-developer-conference-2008-review/</guid>
		<description><![CDATA[
I arrived Tuesday at NDC2008 full of anticipation and excitement; there were a lot of great talks scheduled as I could see it, and I had trouble choosing which ones to attend. I almost immediately found some old colleagues and class mates, which I hadn&#8217;t talked to in several years. That was really an added [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.haugern.net/blog/wp-content/ndc2008logo-thumb2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="72" alt="ndc2008Logo_thumb" src="http://www.haugern.net/blog/wp-content/ndc2008logo-thumb-thumb1.jpg" width="240" align="right" border="0" /></a></p>
<p>I arrived Tuesday at <a href="http://www.ndc2008.no/en/home/" target="_blank">NDC2008</a> full of anticipation and excitement; there were a lot of great talks scheduled as I could see it, and I had trouble choosing which ones to attend. I almost immediately found some old colleagues and class mates, which I hadn&#8217;t talked to in several years. That was really an added bonus, and I really appreciated the little &quot;reunions&quot;.</p>
<h3>Day 1</h3>
<p><a href="http://www.hanselman.com/blog/" target="_blank">Scott Hanselman</a> started the show with a keynote, showing us a little LINQ and the new <a href="http://weblogs.asp.net/scottgu/archive/2007/12/14/new-asp-net-dynamic-data-support.aspx" target="_blank">Dynamic Data</a>-bits. Hanselman was witty, and was a <a href="http://www.hanselman.com/blog/11TopTipsForASuccessfulTechnicalPresentation.aspx" target="_blank">great presenter</a>. There might have been a couple of things that did go to fast if you hadn&#8217;t seen a lot of .NET 3.5 before, but I guess most got at least a glimpse of what it can do.</p>
<p>After the keynote I was considering several sessions, but I decided to attend <a href="http://www.poppendieck.com/" target="_blank">Mary Poppendiecks</a> first session titled <a href="http://en.wikipedia.org/wiki/Thrash_%28computer_science%29" target="_blank">Thrashing</a>. She went through the reasons for them, and what can be done to remedy it. As a reader of the <a href="http://en.wikipedia.org/wiki/The_Mythical_Man-Month" target="_blank">Mythical Man Month,</a> <a href="http://www.amazon.com/Slack-Getting-Burnout-Busywork-Efficiency/dp/0767907698" target="_blank">Slack</a>, <a href="http://www.amazon.com/Peopleware-Productive-Projects-Teams-Second/dp/0932633439" target="_blank">Peopleware</a>, and others, I found she conveyed a lot of the same information found there, and I really share their views. A new aspect I hadn&#8217;t thought of before was <a href="http://en.wikipedia.org/wiki/Queueing_theory" target="_blank">queuing theory</a>, which we apply consciously to hardware and related problems, but seldom to team and people dynamics. I will make a follow up post on the matter.</p>
<p>I&#8217;ve lately dabbled with some reflection, so next I attended <a href="http://weblogs.asp.net/rosherove/" target="_blank">Roy Osheroves</a> talk Deep Reflection, hoping it would be as deep as promised (level 400 session). It certainly was, and I&#8217;m glad I&#8217;ve recently been looking at both Reflection.Emit and CodeDom-programming. It also helps to extensively take advantage of the vanilla reflection utilities regularly. This was a prerequisite, but it seemed like a lot of eyes glazed over when it was presented. He ended the session with a song, and I think he did his presentation on this heavy topic in a great way.</p>
<p>Supposed to be doing a talk about agility in Typemock (the firm), I gave Roys next session a chance. But the agenda had changed and we were introduced to Designing for Testability. I had this part mostly under control, so I was a bit disappointed that the original talk was exchanged. It was an introduction to IoC, DI, and IoC-containers, as well as our options when designing for testability with mocks or subclassing. This session ended in a song as well, and the lyrics was funny as always.</p>
<p>There was unfortunately another change in the agenda, Roy had originally a Threading-talk I&#8217;d like to see, but it was changed to a Testing your data access layer session. With this change, I attended Mary Poppendiecks talk on The Role of Leadership in <a href="http://en.wikipedia.org/wiki/Lean_software_development" target="_blank">Lean Software Development</a>. Contrary to popular belief in most Agile circles, she thinks there is a place for leaders, not only self-organizing teams. I must admit that this is something I&#8217;ve personally experienced as well; when everyone is responsible, no one takes responsibility. I won&#8217;t go into more detail here, but I think it was a great talk, and she definitively hit home many points with me.</p>
<h3>Day 2</h3>
<p>I start out attending an Agile Panel discussion hosted by Scott Hanselman, featuring Mary Poppendieck, Roy Osherove, <a href="http://en.wikipedia.org/wiki/Ken_Schwaber" target="_blank">Ken Schwaber</a>, <a href="http://www.hendricksonxp.com/" target="_blank">Chet Hendrickson</a>, and <a href="http://en.wikipedia.org/wiki/Ron_Jeffries" target="_blank">Ron Jeffries</a>. An example topic was what are the first steps to become agile. It wasn&#8217;t that much of a discussion really, as all the panelists believe in the Agile values.</p>
<p>The next two sessions I followed the Agile crowd in general, and Jeffries &amp; Hendrickson in particular in their first two talks about Natural Laws of Agile Software Development. They presented the same material I saw from <a href="http://smidig.no/smidig2007/" target="_blank">Smidig (Agile) 2007</a> on the economics of releasing early. I think it shows the potential payoff of releasing early, but it misses some aspects of going to early into maintenance mode with the software. I think this has to be explored some more. After showing these teasers, they went more into how early and frequent releases can be done baking quality into the process through the means of TDD and Acceptance Tests.</p>
<p>While I was humming along with Ron &amp; Chet, it seemed like Roy got quite a following. It was almost impossible to get a seat on his Advanced Unit Testing session. It really seems my fellow Norwegians are good &amp; ready for some <a href="http://altdotnet.org/" target="_blank">ALT.NET</a> techniques &amp; practices, especially unit testing. I eventually got a seat on the session, but I must admin I personally was a little bit disappointed as I&#8217;ve already been down most of the roads before. Hopefully it was another teaser for all those who are thinking of getting into the whole unit testing business.</p>
<p>Next up, I attended <a href="http://blogs.msdn.com/madst/" target="_blank">Mads Torgersens</a> Microsoft LINQ Under the Covers: An In-Depth Look at LINQ. And under the covers it was indeed. He gave us a great peek into how a LINQ-expression was disassembled, and showed us the output through Reflector. I must admit it was hard to follow everything, but I was at least familiar with all the constructs. All in all a mindblowing experience, and Mads gets credit for his enthusiasm during the session.</p>
<p>Finally, I attended Mary Poppendiecks session on The Discipline of Going Fast. We got new insights into the <a href="http://en.wikipedia.org/wiki/The_Toyota_Way" target="_blank">Toyota Way</a>, a little bit of history, and specifically the Stop-the-Line practice. I definitively will continue this flirt the Lean methodologies.</p>
<h3>Conclusion</h3>
<p>I&#8217;m very pleased, and I was exhausted after two days packed with great content. The only thing I have a complaint about is that a couple of Roys talks should have been moved to accommodate the massive interest his topics achieved.</p>
<p>For another review from a <a href="http://www.iridescence.no/Posts/Summary-of-the-Norwegian-Developer-Conference-2008.aspx" target="_blank">fellow Norwegian blogger look at Fredriks post</a>. You may also see <a href="http://blogs.msdn.com/grothaug/archive/2008/06/20/bilder-fra-norwegian-developers-conference-2008.aspx" target="_blank">more pictures from Rune Grothaug</a>, who did an amazing job arranging this as well.</p>
<p>I must thank the hosts for a great event, and I will come back next year!</p>
<img src="http://feeds.feedburner.com/~r/DevelopmentEscapades/~4/319121933" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/norwegian-developer-conference-2008-review/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.haugern.net/blog/norwegian-developer-conference-2008-review/</feedburner:origLink></item>
		<item>
		<title>Using the using-statement and pattern in C#</title>
		<link>http://feeds.feedburner.com/~r/DevelopmentEscapades/~3/311385682/</link>
		<comments>http://www.haugern.net/blog/using-the-using-statement-and-pattern-in-c/#comments</comments>
		<pubDate>Fri, 13 Jun 2008 20:31:09 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
		
		<category><![CDATA[pattern]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[dotnet]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/using-the-using-statement-and-pattern-in-c/</guid>
		<description><![CDATA[How often do find yourself writing code like this to do some things in batch:
 


&#160;&#160;&#160; BatchCalculator calc = new BatchCalculator();
&#160;&#160;&#160; calc.Suspend();
&#160;&#160;&#160; calc.CalculateSomething(something);
&#160;&#160;&#160; calc.CalculateSomethingElse(something);
&#160;&#160;&#160; calc.Resume();

Well, I do and I&#8217;m not really happy about sprinkling those Suspends and Resumes around everywhere I need to start and stop something. I see at least two common pitfalls with [...]]]></description>
			<content:encoded><![CDATA[<p>How often do find yourself writing code like this to do some things in batch:</p>
<p> <!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red128\green128\blue64;\red255\green128\blue0;}??\fs24 \cf1\cb2\highlight2     \cf3 BatchCalculator\cf1  \cf4 calc\cf1  = \cf5 new\cf1  \cf3 BatchCalculator\cf1 ();\par ??    \cf4 calc\cf1 .\cf4 Suspend\cf1 ();\par ??    \cf4 calc\cf1 .\cf4 CalculateSomething\cf1 (\cf4 something\cf1 );\par ??    \cf4 calc\cf1 .\cf4 CalculateSomethingElse\cf1 (\cf4 something\cf1 );\par ??    \cf4 calc\cf1 .\cf4 Resume\cf1 ();\par ??}<br />
-->
</p>
<div style="font-size: 10pt; background: black; color: white; font-family: consolas">
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #2b91af">BatchCalculator</span> <span style="color: #808040">calc</span> = <span style="color: #ff8000">new</span> <span style="color: #2b91af">BatchCalculator</span>();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #808040">calc</span>.<span style="color: #808040">Suspend</span>();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #808040">calc</span>.<span style="color: #808040">CalculateSomething</span>(<span style="color: #808040">something</span>);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #808040">calc</span>.<span style="color: #808040">CalculateSomethingElse</span>(<span style="color: #808040">something</span>);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #808040">calc</span>.<span style="color: #808040">Resume</span>();</pre>
</div>
<p>Well, I do and I&#8217;m not really happy about sprinkling those Suspends and Resumes around everywhere I need to start and stop something. I see at least two common pitfalls with this<a href="http://www.haugern.net/blog/wp-content/slum.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="379" alt="http://www.flickr.com/photos/stuckincustoms/894698437/" src="http://www.haugern.net/blog/wp-content/slum-thumb.jpg" width="287" align="right" border="0" /></a> solution and a minor hiccup:</p>
<ol>
<li>Somewhere down the pipe I&#8217;m bound to forget the call to Resume explicitly and I&#8217;ll have a bug on my hands. </li>
<li>Somewhere in between the Suspend and Resume calls an Exception is thrown, Resume is never reached, and leaving the object in an unwanted state. </li>
<li>It could be better looking!&#160; -&gt; </li>
</ol>
<h3>The short using-introduction</h3>
<p>With the introduction of .NET and it&#8217;s managed environment and non-deterministic garbage collector, there were several figureheads in the industry that raised an eyebrow or two. There were also people raising more than their eyebrows as well, and according to legend and several <a href="http://www.dotnetrocks.com" target="_blank">.NET Rocks</a> shows, <a href="http://www.sellsbrothers.com/" target="_blank">Chris Sells</a> (now a blue badge) was one. They allegedly made MS include an IDisposable interface with a simple method Dispose() to fill their garbage collection needs. And if that wasn&#8217;t enough, they included the <a href="http://msdn.microsoft.com/en-us/library/yh598w02.aspx" target="_blank">using</a>-statement which is a <a href="http://www.codeproject.com/KB/cs/CSharp_using.aspx" target="_blank">try-finally</a> in disguise where the finally automatically calls IDisposable.Dispose()!</p>
<p>A couple of regulars in my world in that department are the <a href="http://msdn.microsoft.com/en-us/library/system.data.idbconnection.aspx" target="_blank">IDbConnection interface</a> and later (from 2.0 and onwards) the <a href="http://msdn.microsoft.com/en-us/library/ms172152.aspx" target="_blank">TransactionScope</a> class, but it has also been recommended practice for any implementers of the IDisposable interface.</p>
<h3>Yeah yeah, but what can <u>I</u> do with it?</h3>
<p>With the aforementioned background in place we can exploit it to create a better and more fluent API for our batch-oriented processes. Let us simply dive into the code, and I introduce without further ado; the changed BatchCalculator:</p>
</p>
<div style="font-size: 10pt; background: black; color: white; font-family: consolas">
<pre style="margin: 0px"><span style="color: #ff8000">public</span> <span style="color: #ff8000">class</span> <span style="color: #2b91af">BatchCalculator</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #2b91af">IDisposable</span> <span style="color: #808040">Suspend</span>(){}</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #ff8000">void</span> <span style="color: #808040">Resume</span>(){}</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #ff8000">void</span> <span style="color: #808040">CalculateSomethingElse</span>(<span style="color: #ff8000">object</span> <span style="color: #808040">something</span>){}</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #ff8000">void</span> <span style="color: #808040">CalculateSomething</span>(<span style="color: #ff8000">object</span> <span style="color: #808040">something</span>){}</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>Suspend now returns an IDisposable and we can replace our calling code to this:</p>
</p>
<div style="font-size: 10pt; background: black; color: white; font-family: consolas">
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #2b91af">BatchCalculator</span> <span style="color: #808040">calc</span> = <span style="color: #ff8000">new</span> <span style="color: #2b91af">BatchCalculator</span>();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">using</span>(<span style="color: #808040">calc</span>.<span style="color: #808040">Suspend</span>())</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #808040">calc</span>.<span style="color: #808040">CalculateSomething</span>(<span style="color: #808040">something</span>);</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #808040">calc</span>.<span style="color: #808040">CalculateSomethingElse</span>(<span style="color: #808040">something</span>);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
</div>
<p>Yes! That&#8217;s more like it. I definitively like to looks of that.</p>
<h3>But how.. do I ensure a call to Resume?</h3>
<p>This is where the &quot;magic&quot; happens. Let us make a class which implements IDisposable that gets returned from our Suspend method:</p>
</p>
<div style="font-size: 10pt; background: black; color: white; font-family: consolas">
<pre style="margin: 0px"><span style="color: #ff8000">public</span> <span style="color: #ff8000">class</span> <span style="color: #2b91af">Suspender</span> : <span style="color: #2b91af">IDisposable</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">private</span> <span style="color: #ff8000">readonly</span> <span style="color: #2b91af">BatchCalculator</span> <span style="color: #808040">m_calculator</span>;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #808040">Suspender</span>(<span style="color: #2b91af">BatchCalculator</span> <span style="color: #808040">calculator</span>)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #808040">m_calculator</span> = <span style="color: #808040">calculator</span>;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #ff8000">void</span> <span style="color: #808040">Dispose</span>()</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #808040">m_calculator</span>.<span style="color: #808040">Resume</span>();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>And our revised Suspend method:</p>
</p>
<div style="font-size: 10pt; background: black; color: white; font-family: consolas">
<pre style="margin: 0px"><span style="color: #ff8000">public</span> <span style="color: #2b91af">IDisposable</span> <span style="color: #808040">Suspend</span>()</pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">return</span> <span style="color: #ff8000">new</span> <span style="color: #2b91af">Suspender</span>(<span style="color: #ff8000">this</span>);</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>Now go look at the implemented Dispose-method in our Suspender-class. It just calls our Resume method on our BatchCalculator! So when the using-block is exited, the Dispose-method is called and hooray, mission accomplished.</p>
<h3>Finishing touch</h3>
<p>To increase the applicability of the Suspender-class I introduce the <a href="http://www.martinfowler.com/bliki/RoleInterface.html" target="_blank">role interface</a> IResumable:</p>
</p>
<div style="font-size: 10pt; background: black; color: white; font-family: consolas">
<pre style="margin: 0px"><span style="color: #ff8000">public</span> <span style="color: #ff8000">interface</span> <span style="color: #2b91af">IResumable</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">void</span> <span style="color: #808040">Resume</span>();</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>And implement it in BatchCalculculator:</p>
</p>
<div style="font-size: 10pt; background: black; color: white; font-family: consolas">
<pre style="margin: 0px"><span style="color: #ff8000">public</span> <span style="color: #ff8000">class</span> <span style="color: #2b91af">BatchCalculator</span> : <span style="color: #2b91af">IResumable</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #2b91af">IDisposable</span> <span style="color: #808040">Suspend</span>()</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ff8000">return</span> <span style="color: #ff8000">new</span> <span style="color: #2b91af">Suspender</span>(<span style="color: #ff8000">this</span>);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #ff8000">void</span> <span style="color: #808040">Resume</span>(){}</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #ff8000">void</span> <span style="color: #808040">CalculateSomethingElse</span>(<span style="color: #ff8000">object</span> <span style="color: #808040">something</span>){}</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #ff8000">void</span> <span style="color: #808040">CalculateSomething</span>(<span style="color: #ff8000">object</span> <span style="color: #808040">something</span>){}</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>Now the Suspender class can just wrap our new interface:</p>
</p>
<div style="font-size: 10pt; background: black; color: white; font-family: consolas">
<pre style="margin: 0px"><span style="color: #ff8000">public</span> <span style="color: #ff8000">class</span> <span style="color: #2b91af">Suspender</span> : <span style="color: #2b91af">IDisposable</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">private</span> <span style="color: #ff8000">readonly</span> <span style="color: #2b91af">IResumable</span> <span style="color: #808040">m_resumable</span>;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #808040">Suspender</span>(<span style="color: #2b91af">IResumable</span> <span style="color: #808040">resumable</span>)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #808040">m_resumable</span> = <span style="color: #808040">resumable</span>;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff8000">public</span> <span style="color: #ff8000">void</span> <span style="color: #808040">Dispose</span>()</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #808040">m_resumable</span>.<span style="color: #808040">Resume</span>();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
</div>
<h3>Final note</h3>
<p>If we revisit our weak spots, have we solved them all? A definitive yes; using using guarantees that the Dispose()-method is called which in turn calls our wrapped method. I must also add I really like the syntactic sugar using represents.</p>
<p>This pattern is obviously at a tangent for what using and IDisposable was supposed to be used for. The MSDN library has this to say about <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx" target="_blank">IDisposable</a>: </p>
<blockquote>
<p>The primary use of this interface is to release unmanaged resources.</p>
</blockquote>
<p>But why not leverage what we have available. After all, coding is done once. Reading it is another matter completely.</p>
<img src="http://feeds.feedburner.com/~r/DevelopmentEscapades/~4/311385682" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/using-the-using-statement-and-pattern-in-c/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.haugern.net/blog/using-the-using-statement-and-pattern-in-c/</feedburner:origLink></item>
		<item>
		<title>Looking forward to Norwegian Developer Conference 2008</title>
		<link>http://feeds.feedburner.com/~r/DevelopmentEscapades/~3/302050401/</link>
		<comments>http://www.haugern.net/blog/looking-forward-to-norwegian-developer-conference-2008/#comments</comments>
		<pubDate>Sat, 31 May 2008 21:55:59 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
		
		<category><![CDATA[conferences]]></category>

		<category><![CDATA[learning]]></category>

		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/looking-forward-to-norwegian-developer-conference-2008/</guid>
		<description><![CDATA[ I&#8217;m happy to announce I&#8217;m going to NDC2008 in Oslo. It seems like it&#8217;s going to be two days packed full of everything a developer could want, I wish I could do Haugern.Clone() a couple of times.
I haven&#8217;t quite decided yet what I will observe first-hand, but whatever I choose, I&#8217;m sure it&#8217;ll be [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.ndc2008.no/en/home/"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="72" alt="ndc2008Logo_thumb" src="http://www.haugern.net/blog/wp-content/ndc2008logo-thumb1.jpg" width="240" align="left" border="0" /></a> I&#8217;m happy to announce I&#8217;m going to <a href="http://www.ndc2008.no/en/home/">NDC2008</a> in Oslo. It seems like it&#8217;s going to be two days packed full of everything a developer could want, I wish I could do Haugern.Clone() a couple of times.</p>
<p>I haven&#8217;t quite decided yet what I will observe first-hand, but whatever I choose, I&#8217;m sure it&#8217;ll be interesting!</p>
<img src="http://feeds.feedburner.com/~r/DevelopmentEscapades/~4/302050401" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/looking-forward-to-norwegian-developer-conference-2008/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.haugern.net/blog/looking-forward-to-norwegian-developer-conference-2008/</feedburner:origLink></item>
	</channel>
</rss>
