<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><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: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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Tore Vestues blogs</title>
	
	<link>http://tore.vestues.no</link>
	<description>On a quest for the silver bullet..</description>
	<lastBuildDate>Mon, 26 Jul 2010 12:22:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ToreVestues" /><feedburner:info uri="torevestues" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>The Microsoft MVP Award</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/F4hXuAGJr_M/</link>
		<comments>http://tore.vestues.no/2010/07/26/the-microsoft-mvp-award/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 12:20:35 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Talks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=113</guid>
		<description><![CDATA[I am proud to announce that I have received the Microsoft MVP Award in C#, for my &#8220;exceptional contributions and commitment to technical communities worldwide&#8221;.
That means I am a C# MVP, and I am very proud of it!
I feel it is important for me to contribute to, and learn from, the technical communities around the [...]]]></description>
			<content:encoded><![CDATA[<p>I am proud to announce that I have received the Microsoft MVP Award in C#, for my &#8220;exceptional contributions and commitment to technical communities worldwide&#8221;.</p>
<p>That means I am a C# MVP, and I am very proud of it!</p>
<p>I feel it is important for me to contribute to, and learn from, the technical communities around the world, and therefore I&#8217;m honored by receiving this award for my contributions. I will do my best to continue to contribute to the communities in the time to come, and I know I will learn a lot from them.</p>
<p>- Tore Vestues</p>
<p><img src="http://vestues.no/tore/wp-content/MVP_Horizontal_FullColor_resized_500x.png" alt="MVP" title="MVP" width="500" height="202" class="size-full wp-image-114" /></p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/F4hXuAGJr_M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/07/26/the-microsoft-mvp-award/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2010/07/26/the-microsoft-mvp-award/</feedburner:origLink></item>
		<item>
		<title>Reflection and performance</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/16nOSCbRdHU/</link>
		<comments>http://tore.vestues.no/2010/07/01/reflection-and-performance/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 11:07:45 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=85</guid>
		<description><![CDATA[The open source framework Glue that I&#8217;ve created, use a lot of reflection to invoke members on objects. This has sparked my interest in exploring the use of reflection in respect to performance. A colleague of mine, Åsmund Eldhuset, and I set aside an evening for a geeknight to get to the bottom of this, [...]]]></description>
			<content:encoded><![CDATA[<p>The open source framework <a href="http://glue.codeplex.com">Glue</a> that I&#8217;ve created, use a lot of reflection to invoke members on objects. This has sparked my interest in exploring the use of reflection in respect to performance. A colleague of mine, <a href="http://no.linkedin.com/in/aasmundeldhuset">Åsmund Eldhuset</a>, and I set aside an evening for a geeknight to get to the bottom of this, and this is what we found out.</p>
<p>There are many ways to use reflection, and the performance on them vary a lot. What we did was to try out every method and timing them to measure performance. These are the methods we tested: Normal property access, normal reflection, compiling and invoking a compiled expression, using Emit to create, compile and run the statement.</p>
<p><strong>Normal property access</strong></p>
<p>To compare the other methods, we measured the performance on accessing a property the normal way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Run<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Normal property access&quot;</span>, <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#123;</span> var city <span style="color: #008000;">=</span> address.<span style="color: #0000FF;">City</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>It was no surprise that this was the fastest method.</p>
<p><strong>Normal reflection (250 times slower than normal property access)</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">var cityPropertyInfo <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>Address<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetProperty</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;City&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Run<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;With reflection&quot;</span>, <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> cityPropertyInfo.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>address, <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>This is what most of us think about when talking about reflection. This is also the by far most common method of reflection used in Glue. Although about 250 times slower than normal property access, this method has very strong sides compared to the other methods.</p>
<p><strong>Invoking compiled expressions (twice as slow as normal property access)</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>Address, <span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;&gt;</span> expression <span style="color: #008000;">=</span> x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">City</span><span style="color: #008000;">;</span>
var func <span style="color: #008000;">=</span> expression.<span style="color: #0000FF;">Compile</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Run<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Invoke compiled expression&quot;</span>, <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> func.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span>address<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Using compiled expressions is another way of dynamically invoking members on objects. Due to the syntax exposed in the api in Glue, most mapping specifications enter Glue as (uncompiled) expressions. The fact that it is only about twice as slow than normal property access, is very promising. But there is a catch, a big one. In order to invoke this way, the expression must be compiled. It is not compiled when it enters Glue, so we have to include the time it takes to compile the expression as well.</p>
<p><strong>Compile Expression and invoke it (51700 times slower than normal property access)</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">RunLong<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Compile Expression and invoke&quot;</span>, <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
             <span style="color: #000000;">&#123;</span>
                 var compiledFunction <span style="color: #008000;">=</span> expression.<span style="color: #0000FF;">Compile</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                 compiledFunction.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span>address<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
             <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Yes, if you have to compile an expression before invoking it, it takes about 200 times longer to do the operation than with normal reflection. Leaving this a unattractive method to use with Glue.</p>
<p><strong>Using Emit, invoking a compiled expression (4 times slower than normal property access)</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> Func<span style="color: #008000;">&lt;</span>Address, <span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> GetEmitDelegate<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    var dm <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DynamicMethod<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;TestMethod&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span><span style="color: #000000;">&#41;</span>, <span style="color: #008000;">new</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Address<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Address<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Module</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    MethodInfo getCity <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Address<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetProperty</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;City&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetGetMethod</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    ILGenerator il <span style="color: #008000;">=</span> dm.<span style="color: #0000FF;">GetILGenerator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    il.<span style="color: #0000FF;">Emit</span><span style="color: #000000;">&#40;</span>OpCodes.<span style="color: #0000FF;">Ldarg_0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    il.<span style="color: #0000FF;">EmitCall</span><span style="color: #000000;">&#40;</span>OpCodes.<span style="color: #0000FF;">Call</span>, getCity, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    il.<span style="color: #0000FF;">Emit</span><span style="color: #000000;">&#40;</span>OpCodes.<span style="color: #0000FF;">Ret</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>Address, <span style="color: #FF0000;">String</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#41;</span>dm.<span style="color: #0000FF;">CreateDelegate</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>Address, <span style="color: #FF0000;">String</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
var emitDelegate <span style="color: #008000;">=</span> GetEmitDelegate<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Run<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Emit: run compiled statement&quot;</span>, <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">=&gt;</span>emitDelegate<span style="color: #000000;">&#40;</span>address<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>My guess is that using Emit is not something you will be doing a lot. But, we had to try. Again, executing a compiled expression was fast. But as with expressions, the problem comes when you have to compile before running.</p>
<p><strong>Using Emit, create statement, compile it and run (39000 times slower than normal property access)</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">RunLong<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Emit: Create statement, compile, run&quot;</span>,<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">=&gt;</span>Emit<span style="color: #000000;">&#40;</span>address<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>As we can see, this is also horribly slow.</p>
<p><strong>Conclusion</strong></p>
<p>Unless you can create an expression/emit statement and compile it and then run the compiled version more than 200 times, in the app&#8217;s lifetime, using normal reflection is by far the preferred way to go when you have to dynamically invoke members on objects.</p>
<p>- Tore Vestues</p>
<p>Thanks to <a href="http://no.linkedin.com/in/aasmundeldhuset">Åsmund Eldhuset</a>.</p>
<p><a href='http://tore.vestues.no/2010/07/01/reflection-and-performance/performancerunner/' rel='attachment wp-att-104'>Here&#8217;s the source</a></p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/16nOSCbRdHU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/07/01/reflection-and-performance/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2010/07/01/reflection-and-performance/</feedburner:origLink></item>
		<item>
		<title>My NDC2010 talk</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/8sgcVy_WQfk/</link>
		<comments>http://tore.vestues.no/2010/06/25/my-ndc2010-talk/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 18:07:33 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=99</guid>
		<description><![CDATA[I had a great time at NDC this year. Good talks, great people!
My talk titled &#8220;A Style of Programming&#8221;, was filmed and is already online.
You can find it here.
Most of the other talks from the conference can be found here.
- Tore Vestues
]]></description>
			<content:encoded><![CDATA[<p>I had a great time at NDC this year. Good talks, great people!</p>
<p>My talk titled &#8220;A Style of Programming&#8221;, was filmed and is already online.</p>
<p>You can find it <a href="http://streaming.ndc2010.no/tcs/?id=C21C13ED-8657-4D10-9A21-213BA1031F90">here</a>.</p>
<p>Most of the other talks from the conference can be found <a href="http://streaming.ndc2010.no/tcs/#page:conferenceList&#038;status:recorded&#038;pageNumber:4">here</a>.</p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/8sgcVy_WQfk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/06/25/my-ndc2010-talk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2010/06/25/my-ndc2010-talk/</feedburner:origLink></item>
		<item>
		<title>My Xp2010 lightning talk</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/Se9da9sotDc/</link>
		<comments>http://tore.vestues.no/2010/06/08/my-xp2010-lightning-talk/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 21:10:23 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Talks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=96</guid>
		<description><![CDATA[I recently did a lightning talk on the Xp2010 conference, titled &#8220;Agile fails without craftsmanship&#8221;. It was filmed, and you can see it here. 
Xp2010 was a great event by the way, and you can find other talks here.
- Tore Vestues
]]></description>
			<content:encoded><![CDATA[<p>I recently did a lightning talk on the <a href="http://xp2010.org">Xp2010</a> conference, titled <a href="http://multimedie.adm.ntnu.no/mediasite/SilverlightPlayer/Default.aspx?peid=6a11ed3be82e48e1b018c330ccdbfa9b"><strong>&#8220;Agile fails without craftsmanship&#8221;</strong></a>. It was filmed, and you can see it <a href="http://multimedie.adm.ntnu.no/mediasite/SilverlightPlayer/Default.aspx?peid=6a11ed3be82e48e1b018c330ccdbfa9b"><strong>here</strong></a>. </p>
<p>Xp2010 was a great event by the way, and you can find other talks <a href="http://multimedie.adm.ntnu.no/mediasite/Catalog/pages/catalog.aspx?catalogId=741a4896-6bfc-4ca5-95fa-8865bcf58166">here</a>.</p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/Se9da9sotDc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/06/08/my-xp2010-lightning-talk/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2010/06/08/my-xp2010-lightning-talk/</feedburner:origLink></item>
		<item>
		<title>Speaking at NDC 2010</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/ajKBzOdvtFA/</link>
		<comments>http://tore.vestues.no/2010/04/13/speaking-at-ndc-2010/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 12:41:57 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Coding principles]]></category>
		<category><![CDATA[Learning]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=92</guid>
		<description><![CDATA[I&#8217;m very proud to announce that I will be speaking at the Norwegian Developers Conference 2010 (track 6, day 3, 16:20). 
My talk is named &#8220;A Style of programming&#8221;:
&#8220;This talk describes a set of coding principles that constitutes a style of programming that focuses on ease of coding, ease of changing the code, and ease [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_93" class="wp-caption alignright" style="width: 210px"><img src="http://vestues.no/tore/wp-content/ndc2010.png" alt="Ndc 2010" title="ndc2010" width="200" height="83" class="size-full wp-image-93" /><p class="wp-caption-text">Ndc 2010</p></div>
<p>I&#8217;m very proud to announce that I will be speaking at the <a href="http://www.ndc2010.no/">Norwegian Developers Conference 2010</a> (track 6, day 3, 16:20). </p>
<p>My talk is named &#8220;A Style of programming&#8221;:</p>
<p>&#8220;This talk describes a set of coding principles that constitutes a style of programming that focuses on ease of coding, ease of changing the code, and ease of testing the code. The presentation aims at helping programmers understand how the principles relate to actual code. Every principle is made concrete through examples. This style of programming is influenced by leading object oriented and agile developers, and it is perfect for applying on object oriented languages such as C#.&#8221;</p>
<p>Hope to see you there!</p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/ajKBzOdvtFA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/04/13/speaking-at-ndc-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2010/04/13/speaking-at-ndc-2010/</feedburner:origLink></item>
		<item>
		<title>Your relation to your source control system</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/SPdevdRGxAk/</link>
		<comments>http://tore.vestues.no/2010/03/06/your-relation-to-your-source-control-system/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 07:16:24 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Practices]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=81</guid>
		<description><![CDATA[Do you feel that your source control system is working against you? Well, it might not be perfect, but maybe you&#8217;re just rubbing it the wrong way. I find that if you are a bit unstructured in your approach towards it, you can easily get into some unnecessary problems. And a little structure is all [...]]]></description>
			<content:encoded><![CDATA[<p>Do you feel that your source control system is working against you? Well, it might not be perfect, but maybe you&#8217;re just rubbing it the wrong way. I find that if you are a bit unstructured in your approach towards it, you can easily get into some unnecessary problems. And a little structure is all that it takes in order to make your source control system a lot more frictionless.</p>
<p>This is how I work with my source control system:</p>
<p>First, what do I check in? Logical chunks that work.</p>
<p>Then, how do I check it in?:</p>
<ol>
<li>You start with no local changes (that&#8217;s the best way to start the day)</li>
<li>Get the latest source from the source control system.</li>
<li>At this point you want to be certain that not anyone else have broken anything, do one of the two:</li>
<ul>
<li>Have a build server that notifies when there is a check in, if the last check in was ok, then you&#8217;re good to go (this is by far the preferred way)</li>
<li>Run the tests just after getting the latest source</li>
</ul>
<li>Do what you came here for: code. Whenever you have a logical &#8220;chuck&#8221; of code, proceed to the next step. </li>
<li>You want to be certain that what you are about to check in is working: Run the unit tests one last time to make sure your code is working.</li>
<li>You have to merge your changes with anything else that might have changed: Get latest from the source control system.</li>
<li>Run the unit tests: At this point, you want to be certain that the combination of your code and the code from the repository is not failing anything. (Here it is a great advantage to have a build server that tells you that the source you are getting from the source control is working, so any failing tests you get now are guaranteed your fault)</li>
<li>Fix whatever is broken</li>
<li>check in (when all tests are green)</li>
<li>Repeat</li>
</ol>
<p>Also note:</p>
<ul>
<li>On a general note, do your best not to check in failing code (failing tests, or even not compiling code). Failing code impeeds all the other team members. Don&#8217;t you get really annoyed when someone else on your team are messing up the source, and making lots of extra unnecceary work for you? I do not want to be that person.</li>
<li>Do not, I repeat: <em>do not</em>, check in anything before getting the latest source. You want to make sure that all the latest source (not just yours) are working together.</li>
<li>Do not wait too long to check in (to check in at least once a day is a good rule of thumb). The longer you wait, the more merging you will probably have to do.</li>
<li>It is also a big advantage to have a build server that notifies if someone checks in failing code (so you do not have to be uncertain if it was you or someone else that broke the code when you get the latest source)</li>
</ul>
<p>I am very interested in hearing your experiences with how to work with source control systems, feel free to post comments.</p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/SPdevdRGxAk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/03/06/your-relation-to-your-source-control-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2010/03/06/your-relation-to-your-source-control-system/</feedburner:origLink></item>
		<item>
		<title>How I use the Glue mapping framework</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/tPzbOZFY2PM/</link>
		<comments>http://tore.vestues.no/2009/09/02/how-i-use-the-glue-mapping-framework/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 11:33:33 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/09/02/how-i-use-the-glue-mapping-framework/</guid>
		<description><![CDATA[Glue is a general purpose, bidirectional automatic mapping framework for the .Net platform, with strong verification and testing tools.
I&#8217;m actively developing it. I&#8217;m also actively using it. In this post I&#8217;ll share my experience as a user of Glue, and how easy it is to set up the mappings using TDD.
The example
We want to create [...]]]></description>
			<content:encoded><![CDATA[<p>Glue is a general purpose, bidirectional automatic mapping framework for the .Net platform, with strong verification and testing tools.</p>
<p>I&#8217;m actively developing it. I&#8217;m also actively using it. In this post I&#8217;ll share my experience as a user of Glue, and how easy it is to set up the mappings using TDD.</p>
<p><strong>The example</strong></p>
<p>We want to create a mapping between DomainPerson and GuiPerson, listed here.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DomainPerson
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Name <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> DomainAddress Address <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DomainAddress
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> StreetAddress <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> ZipCode <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> City <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Country <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> GuiPerson
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span> set <span style="color: #000000;">&#123;</span> return<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Name <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> StreetAddress <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> ZipCode <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> City <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Country <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Making sure all properties are related</strong></p>
<p>What I test first is that all the properties are related (or actively ignored). This makes it easy to later detect changes in the classes we map between. It also makes it easy for us to see what properties we have to handle. </p>
<p>But first, in traditional TDD style, lets start with writing a failing test. The test should verify that all properties on GuiPerson are either related or actively ignored. The test looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Fact<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AsserAllPropertiesRelated<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	mapping.<span style="color: #0000FF;">GetRelationsVerification</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AssertAllPropertiesRelated</span><span style="color: #008000;">&lt;</span>GuiPerson<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>and, as this will not compile, let&#8217;s create the actual mapping:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">mapping <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mapping<span style="color: #008000;">&lt;</span>DomainPerson, GuiPerson<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>It compiles, but as expected, it fails. We have created a mapping, but nothing is related. A nice feature with Glue is that it tries to be as informative as possible when it fails. Look at the exception we receive when running this test:</p>
<blockquote><p>
Not all properties are related (or ignored): Id, Name, StreetAddress, ZipCode, City, Country
</p></blockquote>
<p>It tells us what properties we need to handle. This makes my job easy. I&#8217;ll just have to relate (or ignore) all the properties reported in the exception:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">mapping.<span style="color: #0000FF;">Relate</span><span style="color: #000000;">&#40;</span>domain<span style="color: #008000;">=&gt;</span>domain.<span style="color: #0000FF;">Id</span>,gui<span style="color: #008000;">=&gt;</span>gui.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
mapping.<span style="color: #0000FF;">Relate</span><span style="color: #000000;">&#40;</span>domain <span style="color: #008000;">=&gt;</span> domain.<span style="color: #0000FF;">Name</span>, gui <span style="color: #008000;">=&gt;</span> gui.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var addressMapping <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mapping<span style="color: #008000;">&lt;</span>DomainAddress, GuiPerson<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
addressMapping.<span style="color: #0000FF;">AutoRelateEqualNames</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
mapping.<span style="color: #0000FF;">Flatten</span><span style="color: #000000;">&#40;</span>domain<span style="color: #008000;">=&gt;</span>domain.<span style="color: #0000FF;">Address</span>,addressMapping<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Running the test, I get another Exception:</p>
<blockquote><p>
Glue.GlueException : The relation between &#8216;Id&#8217; and &#8216;Id&#8217; has atleast one readonly property, and can not be related TwoWays
</p></blockquote>
<p>Ah, fair enough, it went a bit fast there. I related all properties, but I didn&#8217;t check if any where readonly. Luckily, Glue tells us this early. I&#8217;ll make this change:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">mapping.<span style="color: #0000FF;">RelateTowardsRight</span><span style="color: #000000;">&#40;</span>domain <span style="color: #008000;">=&gt;</span> domain.<span style="color: #0000FF;">Id</span>, gui <span style="color: #008000;">=&gt;</span> gui.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>And when I run the test again, it goes green. Now all properties are related, and we are protected from future changes. </p>
<p><strong>Make sure the mapping actually works</strong></p>
<p>But will it really work? We can also easily check if the actual values can be set on the target object. You should do this. I always do. So, lets start with another test:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Fact<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AssertMappingWorks<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    mapping.<span style="color: #0000FF;">GetMapperVerification</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AssertMapsCorrectlyTowards</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> GuiPerson<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Running this test, we get an Exception:</p>
<blockquote><p>
Glue.GlueException : Failed properties: Id (Failed to set value)
</p></blockquote>
<p>Ah, it detected that Id on GuiPerson is totally unable to set. Ok, lets say that this is supposed to be this way. That means we should not map Id towards GuiPerson. That means that we must remove this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">mapping.<span style="color: #0000FF;">RelateTowardsRight</span><span style="color: #000000;">&#40;</span>domain <span style="color: #008000;">=&gt;</span> domain.<span style="color: #0000FF;">Id</span>, gui <span style="color: #008000;">=&gt;</span> gui.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Running the tests again makes AssertMappingWorks gets green, but now AsserAllPropertiesRelated got red again. That is because we have not related Id on GuiPerson. But since we do not want to relate it, we ignore it, to signal that we actively have made that choice. We do it like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">mapping.<span style="color: #0000FF;">IgnoreTowards</span><span style="color: #008000;">&lt;</span>GuiPerson<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>gui<span style="color: #008000;">=&gt;</span>gui.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><img src='http://vestues.no/tore/wp-content/glue1.jpg' alt='Glue' style='float:right'/>Running the tests, they both are green, and now we know that we have a working mapping, which also are protected from failing on future changes.</p>
<p>Note that this example only tests the mapping and relations towards GuiPerson, in a bidirectional mapping scenario I advice to write the same two tests towards DomainPerson.</p>
<p>This is how I do it, and it works great for me. Hope you liked it. To learn more about Glue, and to download it, go to <a href="http://glue.codeplex.com">the Glue homepage at Codeplex</a></p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/tPzbOZFY2PM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/09/02/how-i-use-the-glue-mapping-framework/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2009/09/02/how-i-use-the-glue-mapping-framework/</feedburner:origLink></item>
		<item>
		<title>Tell, don’t ask</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/hM0sWsy0SKA/</link>
		<comments>http://tore.vestues.no/2009/08/16/tell-dont-ask/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 17:43:29 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Coding principles]]></category>
		<category><![CDATA[Practices]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/08/16/tell-dont-ask/</guid>
		<description><![CDATA[Tell, don&#8217;t ask is a way of thinking when you&#8217;re programming software, a mindset. It is something that should be in the back of your head every time you write a line of code, or a chunk of code that describes some functionality. It is not something you merely apply when trying to solve a [...]]]></description>
			<content:encoded><![CDATA[<p><em>Tell, don&#8217;t ask</em> is a way of thinking when you&#8217;re programming software, a mindset. It is something that should be in the back of your head every time you write a line of code, or a chunk of code that describes some functionality. It is not something you merely apply when trying to solve a particular problem, it is something you apply all the time.</p>
<p>It is about how your entities communicate, it is about where you place your logic. It is about where to place the code. And when you are programming you are putting code somewhere all the time. That means this is one of the fundamental things to learn as a developer.</p>
<p><strong>So, what is it?</strong></p>
<p>The phrasing itself means that you should tell other objects to do things, instead of asking other objects for information and processing it yourself. (&#8221;you&#8221; are of course an object in this case).</p>
<p><strong>Example: The car dealer</strong></p>
<p>Say you&#8217;re making software for a car dealer. You want to create a report that lists all cars for sale that can be distributed to the customers.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ForSaleReport
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> List<span style="color: #008000;">&lt;</span>Car<span style="color: #008000;">&gt;</span> allCars<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> ForSaleReport<span style="color: #000000;">&#40;</span>List<span style="color: #008000;">&lt;</span>Car<span style="color: #008000;">&gt;</span> allCars<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">allCars</span> <span style="color: #008000;">=</span> allCars<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> List<span style="color: #008000;">&lt;</span>Car<span style="color: #008000;">&gt;</span> GetAllCarsForSale<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> allCars.<span style="color: #0000FF;">FindAll</span><span style="color: #000000;">&#40;</span>car <span style="color: #008000;">=&gt;</span> car.<span style="color: #0000FF;">SoldDate</span> <span style="color: #008000;">!=</span> DateTime.<span style="color: #0000FF;">MinValue</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><em>There is a violation of &#8220;Tell, don&#8217;t ask&#8221; right there</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">car.<span style="color: #0000FF;">SoldDate</span> <span style="color: #008000;">!=</span> DateTime.<span style="color: #0000FF;">MinValue</span></pre></td></tr></table></div>

<p>You <em>ask</em> for information (the cars SoldDate), then make a calculation based on it (calculating if the car is for sale). So, what should you have done? You should <em>tell</em> the Car-object to resolve if it is for sale. It is not your concern to figure it out (when you are in the ForSaleReport), your only concern is to list out cars that are for sale, not why or how they are. So, instead of trying to figure things out yourself, you should just ask the car:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">car.<span style="color: #0000FF;">IsForSale</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p><img src='http://vestues.no/tore/wp-content/istock_000002694919xsmall_adjusted_smaller.jpg' alt='Head in the sand' style='float:right'/> <strong>Be ignorant</strong></p>
<p>It is easier to apply <em>Tell, don&#8217;t ask</em> if you think of your objects as ignorant. You give them a responsibility, and beyond that they should be totally ignorant. Always ask someone else to solve the problems. This makes them ask less questions. Asking less questions means less coupling. Only caring for a single concern means higher cohesion.</p>
<p><strong>And if I don&#8217;t .. ?</strong></p>
<p>Didn&#8217;t you see the major difference between the two (asking for the SoldDate versus telling the car to figure out if it IsForSale)? Well, there is a difference, and normally it doesn&#8217;t take long before it shows.</p>
<p>Say you ask for the SoldDate (thinking <em>Tell, don&#8217;t ask</em> are for stupid programmers that wouldn&#8217;t understand as advanced logic as yourself..). And  say that since this application is growing big you ask for SoldDate several places to resolve if the car is for sale (I&#8217;ve seen systems where asking for such &#8220;innocent&#8221; information is done hundreds of places). Then one day the owner of the car dealership comes to you, quite agitated, telling you that your reports are plain wrong: &#8220;A car doesn&#8217;t have to be for sale even though the sales date is not set. It can be held of or be in for repair. Fix it now!&#8221;</p>
<p>Hm, now you have to search for all the places where you did your &#8220;innocent&#8221; information asking, changing it everywhere, and test it to see if you broke something else. Pray that you do not have any indirect references to SoldDate (getting it from a car and sending it away for processing further away), that would be hell to track down. Wouldn&#8217;t it be better if you just had to change the logic in one place? in Cars &#8220;IsForSale&#8221; method? I would think so.</p>
<p>And that is not the only problem. Opening up a class, exposing information (like SoldDate in this case) makes it easier for other classes to hard wire itself to the class. This means more coupling, and more coupling means that the class will be harder to change. In this case, there is no way you easily can change the representation of SoldDate within Car. If you want to change it, you have to change every class that refers to it. This might be a very tedious task.</p>
<p>Another problem, which might not be so obvious since there is no &#8220;principle&#8221; attached to it, is the developer experience: How easy it is to write code in this system. I find this an important issue, and a bit underestimated at times. This is important because it has to do with productivity. When writing new code for this system, and you have to find out if the car is sold or not, it would be much easier just to write &#8220;car.IsSold()&#8221;, then it would be to actually have to find out how to calculate it, and then implement it (find somewhere else in the code that calculates it, copying that code, and so on&#8230;). Figuring out if a car is sold or not should be done once. When it is figured out and implemented, everyone else should just reuse that logic, never again caring for how it is implemented (unless of course, you have to fix or change something in that logic).</p>
<p><strong>Conclusion</strong></p>
<p>For me, <em>Tell, don&#8217;t ask</em> brings with it several realizations about programming:</p>
<ul>
<li>It is important to think about your modelling when you write code. Where you put your code has major implications on your softwares maintainability.</li>
<li>Modelling isn&#8217;t just for &#8220;architects&#8221;. As a developer you make decisions every day that can make or break the software.</li>
<li>Focus on responsibilities, and who should be concerned with what. This makes your application more cohesive and less coupled.</li>
<li>Don&#8217;t repeat yourself (DRY). <em>Tell, don&#8217;t ask</em> helps you put logic one place (where its concern is handled) instead of scattering and copying it all around.</li>
<li>When modelling, it is also important to consider how easy it is to read and use the code for the developers.</li>
</ul>
<p>So, if there is only one thing you want to keep in the back of your head when programming, I suggest it is <em>Tell, don&#8217;t ask</em>.</p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/hM0sWsy0SKA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/08/16/tell-dont-ask/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2009/08/16/tell-dont-ask/</feedburner:origLink></item>
		<item>
		<title>Glue – the new mapping framework</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/HXQE6yu3eiw/</link>
		<comments>http://tore.vestues.no/2009/08/10/glue-the-new-mapping-framework/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 09:57:36 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/08/10/glue-the-new-mapping-framework/</guid>
		<description><![CDATA[I&#8217;ve spent this summer implementing a new mapping framework for the .Net plattform: Glue.
You&#8217;ll find examples and code here: http://glue.codeplex.com
Glue is a general purpose, bidirectional automatic mapping for the .Net platform, with strong verification and testing tools.
I&#8217;ve seen quite a lot of less than optimal handling of mapping issues in quite a few projects over [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent this summer implementing a new mapping framework for the .Net plattform: Glue.</p>
<p><a href="http://glue.codeplex.com">You&#8217;ll find examples and code here: http://glue.codeplex.com</a></p>
<p>Glue is a general purpose, bidirectional automatic mapping for the .Net platform, with strong verification and testing tools.</p>
<p>I&#8217;ve seen quite a lot of less than optimal handling of mapping issues in quite a few projects over the years. Again and again mapping seems to be just a task we have to do, and a bit too small for automating. It becomes clearer and clearer to me that mapping is both an important task in many projects, and it is always a repetitive and boring task. In addition far to often subtle annoying bugs tend to sneak into mapping code. This motivated me to create Glue. And this is what I need from a mapping framework:</p>
<p>a) a way to automate mapping, mostly because it is boring, and that makes it error prone,<br />
b) a way to automatically test the mapping, I&#8217;ve found that manual written test code for mapping is seldom done in a good way,<br />
c) a way to prevent future changes making my mappings obsolete. When people make changes, they rarely focus on mapping (and honestly they should not either). They should be told when they have to update the mapping.</p>
<p>Now, you could argue that there is already a mapping framework available on the .Net plattform. And when I started this work, I actually started with that framework, thinking I could just make a few extensions to fulfil my needs. I actually spent some time in the  source code to try to implement it, but sadly I realized that our needs differed too much. So, I created Glue, and these where the driving forces: </p>
<p><strong>General purpose</strong></p>
<p>Glue is a general purpose mapper. We realize that in the real world there are a lot of different solutions, and not all of them follow &#8220;the one true pattern&#8221;. In fact, mapping is often used to map to and from subsystems that are far from well designed. Subsystems we try to hide in a layer because we do not want it to leak into the other layers. Thus, we believe a mapper must support quite a few different scenarios. The goal is to promote good coding practices, but not to ignore the fact that there is a lot of legacy code out there that forces us to work a bit differently at times.</p>
<p><strong>Bidirectional</strong></p>
<p>I would say that in many mapping scenarios we need to map in both directions. First we get data from an object in a layer, and map it to an object in the layer above. When that layer is done with manipulating that object, we often want to map it back to down to the layer where it all came from. I have noticed not all mapping frameworks see it this way, and this was one of the reasons why I started working on Glue instead of trying to extend existing frameworks.</p>
<p><strong>Strong verification and testing tools</strong></p>
<p>I want to be absolutely certain that my mapping works. I also want to make sure that it is very hard to break it later. Manually writing tests for mapping is even more tedious than writing manual mapping. Glue automates this. Future changes has a sad reputation of breaking mapping code. Glue helps you detect this. Tools for helping the mapping is very important to me. And more tools will be available in future releases.</p>
<p><strong>Simplicity</strong></p>
<p>Mapping should be simple. Glue tries to simplify both the mapping process, and the verification and testing. You should not have to state the obvious, and Glue support relating properties automatically based on names.</p>
<p><strong>Explicitness</strong></p>
<p>Although Glue enables you to automate much of the mapping process, it also gives you the opportunity to be explicit about the mapping. So if you want to describe every relation in detail, you can. When it comes to understandable code this can be a good thing. Taking difficult to understand implicit mappings, and stating them explicitly can sometimes make things much easier to understand. </p>
<p><strong>Current version and the future</strong></p>
<p>The current version is 0.2.0 Alpha. It is still in Alpha because if we find good ways to improve the API, we do not want to lock ourselves to it just yet, before we get more feedback. I&#8217;m guessing the next release will be Beta.</p>
<p>I am currently using Glue on the project I am working on, and in about a month it will reach production. This somewhat guarantees that it will continue to evolve as our needs expand, and that we will find bugs sooner, and they will be fixed sooner.</p>
<p>Looking into the future we have some exciting ideas on tools to help with the mapping, and we are also working hard to make Glue as easy as possible to use, so expect simplifications. In addition we want Glue to serve a broad set of needs, so feedback is highly appreciated and if you explain your special needs, we might just implement it.</p>
<p><a href="http://glue.codeplex.com">You&#8217;ll find examples and code here: http://glue.codeplex.com</a></p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/HXQE6yu3eiw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/08/10/glue-the-new-mapping-framework/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2009/08/10/glue-the-new-mapping-framework/</feedburner:origLink></item>
		<item>
		<title>ASP.NET MVC: DefaultControllerFactory is not thread safe!</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/ZOKvVeNILZw/</link>
		<comments>http://tore.vestues.no/2009/07/03/aspnet-mvc-defaultcontrollerfactory-is-not-thread-safe/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 09:05:32 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/07/03/aspnet-mvc-defaultcontrollerfactory-is-not-thread-safe/</guid>
		<description><![CDATA[I am not sure if this is a bug or a &#8220;feature&#8221; of the ASP.NET MVC framework. Either way, this is something you should be aware of as it can cause some very hard to track concurrency issues which might leak information between your HttpRequests.
In my last project we used the ASP.NET MVC framework along [...]]]></description>
			<content:encoded><![CDATA[<p>I am not sure if this is a bug or a &#8220;feature&#8221; of the ASP.NET MVC framework. Either way, this is something you should be aware of as it can cause some very hard to track concurrency issues which might leak information between your HttpRequests.</p>
<p>In my last project we used the ASP.NET MVC framework along with StructureMap, which made us create our own controller factory. I just blogged about it <a href="http://tore.vestues.no/2009/06/28/aspnet-mvc-let-structuremap-create-your-controllers/">here</a>. </p>
<p>If it was a good design decision or not I am not completely certain anymore, but we added some more framework logic to the controller factory, including checking state on the HttpRequest-object. This, it turned out, was a path to trouble. After a while we had unexpected behavior in our system, sometimes it leaked information between HttpRequests it seemed! But since this only happened during stress testing, it was hard to find the cause.</p>
<p>What we found was that the RequestContext.HttpContext-object was not all the way thread safe. When a request hits our custom controller, and the request goes to sleep to serve another request, the HttpContext of the factory changes to the second requests HttpContext. This means that the first request will work on the second requests HttpContext when it wakes up. This is bad news if you are dependent on the HttpContext in your custom controller factory.</p>
<p>The lesson is this: you have to do at least one of the two actions below if you want to access the HttpContext in your custom controller factory.</p>
<p>1) </p>
<p>The problem occurs when you set your controller factory like this (normally in the global.asax file)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">ControllerBuilder.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">SetControllerFactory</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> CustomControllerFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>If you send in an instance lik this, it will be the only instance of your controller factory, and this is where the HttpContext-concurrency issues arrive in the first place. What you can do, which will take away all the concurrency issues is to send in the type instead of an instance, like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">ControllerBuilder.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">SetControllerFactory</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>CustomControllerFactory<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The drawback to sending in the type is a) your controller factory will be instantiated for every request and b) you cannot send in anything to your instance (since you&#8217;re not instantiating it). This might not be a problem, and if not, you&#8217;re good to go.</p>
<p>2) </p>
<p>If you have to use one instance only of your controller factory, and you need to access the HttpContext for a request, you MUST use the HttpContext.Current-instance and not the RequestContext.HttpContext. The HttpContext.Current is as far as I&#8217;ve tested thread safe.</p>
<p>Not thread safe:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">RequestContext.<span style="color: #0000FF;">HttpContext</span></pre></td></tr></table></div>

<p>The thread safe alternative:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">HttpContext.<span style="color: #0000FF;">Current</span></pre></td></tr></table></div>

<p><a href="http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId=3724">I just found out that this is a bug</a> </p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/ZOKvVeNILZw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/07/03/aspnet-mvc-defaultcontrollerfactory-is-not-thread-safe/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2009/07/03/aspnet-mvc-defaultcontrollerfactory-is-not-thread-safe/</feedburner:origLink></item>
	</channel>
</rss>
