<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Haugerns Development Escapades &#187; c#</title>
	<atom:link href="http://www.haugern.net/blog/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.haugern.net/blog</link>
	<description>Morten Haug on development and other curiosities</description>
	<lastBuildDate>Sun, 31 May 2009 21:56:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Beware of the IEnumerables</title>
		<link>http://www.haugern.net/blog/beware-of-the-ienumerables/</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>
]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/beware-of-the-ienumerables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Norwegian Developer Conference 2008 Review</title>
		<link>http://www.haugern.net/blog/norwegian-developer-conference-2008-review/</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[agile]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[conferences]]></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>
]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/norwegian-developer-conference-2008-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the using-statement and pattern in C#</title>
		<link>http://www.haugern.net/blog/using-the-using-statement-and-pattern-in-c/</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[c#]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[pattern]]></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>
]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/using-the-using-statement-and-pattern-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
