<?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>Sat, 06 Mar 2010 07:20:11 +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>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>0</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2009/07/03/aspnet-mvc-defaultcontrollerfactory-is-not-thread-safe/</feedburner:origLink></item>
		<item>
		<title>ASP.NET MVC: Let StructureMap create your controllers</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/hhP1OCH3PsE/</link>
		<comments>http://tore.vestues.no/2009/06/28/aspnet-mvc-let-structuremap-create-your-controllers/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 10:09:41 +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/06/28/aspnet-mvc-let-structuremap-create-your-controllers/</guid>
		<description><![CDATA[For those of you already familiar with StructureMap and want to use it to configure your objects in ASP.NET MVC, read on.
The ASP.NET MVC framework has a default controller factory (DefaultControllerFactory) that requires all controllers to have a parameterless constructor. So, if you want to inject your dependencies to the constructor, you can&#8217;t do it. [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you already familiar with StructureMap and want to use it to configure your objects in ASP.NET MVC, read on.</p>
<p>The ASP.NET MVC framework has a default controller factory (DefaultControllerFactory) that requires all controllers to have a parameterless constructor. So, if you want to inject your dependencies to the constructor, you can&#8217;t do it. In addition, Microsoft suggests the following pattern to mock out the dependencies when testing:</p>
<p><strong>The Microsoft way of testing</strong></p>
<p>(Example from <a href="http://www.asp.net/learn/mvc/tutorial-31-cs.aspx">www.asp.net/mvc</a>)</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: #008080; font-style: italic;">//The Microsoft way...</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> GroupController <span style="color: #008000;">:</span> Controller
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> IContactManagerService _service<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> GroupController<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContactManagerService<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ModelStateWrapper<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ModelState</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;
    <span style="color: #0600FF;">public</span> GroupController<span style="color: #000000;">&#40;</span>IContactManagerService service<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _service <span style="color: #008000;">=</span> service<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #008080; font-style: italic;">//...</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>In other words, create one constructor for the actual application, and one constructor for testing. It works, you get to test it, it&#8217;s not all that bad, but it just doesn&#8217;t sit well with me.</p>
<p><strong>Take control of your controllers!</strong></p>
<p>Don&#8217;t you like this either? Well, there is an easy way out of it. One of the really good things about ASP.NET MVC is that it is easy to configure. You can create your own controller factory and take control of the whole process. It might sound like a complex task, but it isn&#8217;t.</p>
<p>Let’s start with our controller, it has a dependency that is injected through the constructor:</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> HomeController <span style="color: #008000;">:</span> Controller
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> Speaker speaker<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> HomeController<span style="color: #000000;">&#40;</span>Speaker speaker<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">speaker</span> <span style="color: #008000;">=</span> speaker<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> ActionResult Index<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        ViewData.<span style="color: #0000FF;">Model</span> <span style="color: #008000;">=</span> speaker<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</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>Running the website now, trying to reach this controller, will make the framework crash (&#8221;No parameterless constructor defined for this object.&#8221;). We need our own controller factory to make this work:</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> StructureMapControllerFactory <span style="color: #008000;">:</span> DefaultControllerFactory
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IContainer container<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> StructureMapControllerFactory<span style="color: #000000;">&#40;</span>IContainer container<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">container</span> <span style="color: #008000;">=</span> container<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> IController GetControllerInstance<span style="color: #000000;">&#40;</span>Type controllerType<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>IController<span style="color: #000000;">&#41;</span>container.<span style="color: #0000FF;">GetInstance</span><span style="color: #000000;">&#40;</span>controllerType<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>This controller takes in a StructureMap.IContainer and uses it to create all the controllers. It inherits from the DefaultControllerFactory and overrides the GetControllerInstance-method that is used to create controllers.</p>
<p>Now all we have to do is to tell the ASP.NET MVC framework that it should use our controller factory. To do that simply add this line in the Application_Start() method 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> StructureMapControllerFactory<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Container<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>(Notice that we do not need to configure StructureMap since our dependencies are so easy to resolve. In a real world app you will most probably need to configure the container)</p>
<p>There you go. Using StructureMap with the ASP.NET MVC framework is easy!</p>
<p><a href='http://vestues.no/tore/wp-content/structuremapcontrollerfactoryexample.zip'>Download the complete example here </a></p>
<p>-Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/hhP1OCH3PsE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/06/28/aspnet-mvc-let-structuremap-create-your-controllers/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2009/06/28/aspnet-mvc-let-structuremap-create-your-controllers/</feedburner:origLink></item>
		<item>
		<title>Understand, don’t just follow</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/j-dhlSBzVj4/</link>
		<comments>http://tore.vestues.no/2009/05/02/understand-dont-just-follow/#comments</comments>
		<pubDate>Sat, 02 May 2009 13:52:48 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Coding principles]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Practices]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/05/02/understand-dont-just-follow/</guid>
		<description><![CDATA[It still surprises me, and scares me, how many people in our industry that just follow ideas and practices just because &#8220;someone&#8221; says it is the way to go, and how those same people do not understand why they are actually doing it.
Let me give you an example.
When Kent Beck and his extreme programmers started [...]]]></description>
			<content:encoded><![CDATA[<p>It still surprises me, and scares me, how many people in our industry that just follow ideas and practices just because &#8220;someone&#8221; says it is the way to go, and how those same people do not understand <em>why</em> they are actually doing it.</p>
<p>Let me give you an example.</p>
<p>When Kent Beck and his extreme programmers started out (with XP &#8211; extreme programming), they were talking about not writing comments in your code. And they explained what they meant: Comments are often an indication that your code isn&#8217;t readable on its own, the fix is often to write code that is easier to understand, not to add comments.</p>
<p>For me this is great advice. I try to keep my code clean and readable, and when I feel the urge to write comments, I first check if I can make the code more self explainable on its own. I personally write some comments, but never to explain code that looks like crap.</p>
<p>The problem with this advice is that it is actually bad advice if you do not understand <em>why</em> you should follow it. Those that do not understand why they should reduce their amount of comments, but still dig the idea, are probably writing as unreadable code as they always did, but now explaining the mess with even less comments. The result is probably all over worse code than if they didn&#8217;t follow the advice in the first place!</p>
<p>There are at least two lessons to be learned here.</p>
<p>First, if you do not understand <em>why</em> you are doing something, you are probably doing it wrong. It is time to start questioning what you are doing, always ask why.</p>
<p>Second. Say too many people misunderstand the purpose of something, thus applies it wrong, and thus is actually producing worse results than before. In our &#8220;no comment&#8221; example, people would, based on empirical evidence of the practitioners, say that the &#8220;no comment&#8221;-idea is a joke (since most of its followers are actually writing worse code). So by applying the idea wrong you are probably discrediting the whole idea.</p>
<p>I like to define agile in one sentence: &#8220;Ask Why&#8221;. &#8220;Why should we write this piece of documentation?&#8221; &#8220;Why should we follow this process?&#8221; &#8220;Why should we follow this practice?&#8221; If we better understand what we are doing, and can adjust our practices accordingly, I think we will get a long way. I think this is the core of agile.</p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/j-dhlSBzVj4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/05/02/understand-dont-just-follow/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2009/05/02/understand-dont-just-follow/</feedburner:origLink></item>
		<item>
		<title>Recommended Books</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/18NxNJ-pER4/</link>
		<comments>http://tore.vestues.no/2009/04/27/recommended-books/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 11:53:04 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Learning]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/04/27/recommended-books/</guid>
		<description><![CDATA[I just added a new page on my blog: Recommended Books. I will update it regurarly. You can also subscribe to the recommendations (feed at the bottom of that page).
]]></description>
			<content:encoded><![CDATA[<p>I just added a new page on my blog: <a href="http://tore.vestues.no/recommended-books/">Recommended Books</a>. I will update it regurarly. You can also subscribe to the recommendations (feed at the bottom of that page).</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/18NxNJ-pER4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/04/27/recommended-books/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2009/04/27/recommended-books/</feedburner:origLink></item>
		<item>
		<title>Unit tests should not test software</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/9AFpaX1Ry8E/</link>
		<comments>http://tore.vestues.no/2009/03/20/unit-tests-should-not-test-software/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 05:32:23 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Practices]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/03/20/unit-tests-should-not-test-software/</guid>
		<description><![CDATA[Register your claim
When I refactor or make changes to code I need somehow to verify that the changes do not break anything. I want to do it with unit tests. Unit tests are my safety net. My philosophy on unit testing is something like, if you do not want me to change some of your [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Register your claim</strong><br />
When I refactor or make changes to code I need somehow to verify that the changes do not break anything. I want to do it with unit tests. Unit tests are my safety net. My philosophy on unit testing is something like, if you do not want me to change some of your functionality then &#8220;register your claim&#8221; on that functionality as a unit test. </p>
<p><strong>Unit tests specify</strong><br />
There is a lot of functionality (or business logic) that is accidental, meaning that it is not really necessary and it might even be plain wrong, a bug. It happens, a lot. It is hard to know if a piece of code is accidental or not if you do not have a way to express it. A unit test is a great way to express your intent and your &#8220;real&#8221; functionality. For me, unit tests are specifications. They specify the intended functionality. This is explicit functionality. I love explicit functionality. In fact I love almost anything explicit when it comes to code. I want to read loud and clear what the intentions are. The opposite of explicit functionality is of course implicit functionality. This is the functionality that resides in the code without anything or one to claim its importance. Implicit functionality might always be accidental, and that is why it is so hard to work with that kind of functionality. You do not know if it&#8217;s supposed to be there. </p>
<p><strong>Unit tests verify</strong><br />
If all important functionality is ensured with unit tests, you have your safety net. The unit tests verify what you specified. Then refactoring is a dream. Keeping the codebase neat, up to date and clean is easy. To be able to nurture your codebase, through refactoring, you will need this safety net. It verifies your intentions and ignores all unnecessary code. </p>
<p><strong>Unit tests do not test software</strong><br />
This way unit tests ensures that the intended functionality in your classes stays there, and makes it easier to refactor. But it does not check if your system work. It does not verify that you have put it all together the right way. It does not check if everything goes right in the databases, on files or in the user interface. Partly integration tests will do this job for you, and partly manual testing. </p>
<p><strong>Abstractions through OO</strong><br />
When I talk about unit tests, I consider my units to be classes. I also want to stress the point of knowing where to put your logic. Responsibilities should be placed where it belongs, in the right concepts (on the right classes). This is the fundament of object orientation, expressed elegantly through domain driven design. The concepts are the classes. Thus it is very important to verify the logic where it belongs, on the classes. And why should we use classes and separate logic? There are mighty reasons for this, but for now let me just say: to avoid cognitive overload. </p>
<p><strong>You need all types of tests</strong><br />
So, unit tests are there to specify the behavior of the systems concepts (think DDD). This is imperative to be able to refactor the core of your system. But as I&#8217;ve said, this is far from enough to test your system. I find integration tests best for testing your software. Tests that are more like acceptance tests, checking a complete path of calls from top to bottom (and yes, I often include the database in these tests). In addition there are things you will not be able to test automatically (that easily). How does the gui look and feel? You need someone to test that too. </p>
<p>To conclude, you need a whole lot of unit tests to lock down functionality. You need integration tests to test your software, and you need people to test gui and other stuff that isn&#8217;t feasible to test any other way. And my opinion is that <i>you</i> should do all this before you send it to QA. </p>
<p>-Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/9AFpaX1Ry8E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/03/20/unit-tests-should-not-test-software/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2009/03/20/unit-tests-should-not-test-software/</feedburner:origLink></item>
		<item>
		<title>The importance of code that communicates</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/Rw20G9uFRPY/</link>
		<comments>http://tore.vestues.no/2009/03/10/the-importance-of-code-that-communicates/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 19:03:51 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Coding principles]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Practices]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/03/10/the-importance-of-code-that-communicates/</guid>
		<description><![CDATA[One of the major reasons that our industry produces software that is full of bugs, with a massive cost of change and with a short life expectancy is that developers do not understand the importance of writing code that communicates. Yes, one of the most important aspects of software development is to write code that [...]]]></description>
			<content:encoded><![CDATA[<p>One of the major reasons that our industry produces software that is full of bugs, with a massive cost of change and with a short life expectancy is that developers do not understand the importance of writing code that communicates. Yes, <i>one of the most important aspects of software development is to write code that communicates</i>. </p>
<p><strong>Disclaimer:</strong> There are several reasons software projects fail and many of them are caused by management &#8211; way above the developers. Still, we as developers have to take our part of the blame, and crap code is our contribution to the failure. </p>
<p><strong>It is not about algorithms and performance</strong></p>
<p>I didn&#8217;t learn much about communicative code at the University. And I still do not think people learn a lot about it. Students probably still think that software development is all about performance and effective algorithms. I&#8217;m sorry, often it&#8217;s nothing like that. In most business applications today there are no fancy algorithms. It&#8217;s just a huge amount of code that is trying to express the rules and logic that exist in the business. Performance might be an issue, but it has nothing to do with tweaking algorithms, it&#8217;s about architecture, data loads and sometimes really bad code.</p>
<p><strong>So, what&#8217;s the problem?</strong></p>
<p>The major problem with business applications today is the huge amount of code. The amount is not a problem in itself, but it gets to be a problem very fast when it&#8217;s hard to understand what it does.</p>
<p>Let&#8217;s take a simple example: Methods in today’s software are often way too long. It takes a lot of time to understand 100 lines of code that does several different things, and it&#8217;s even harder to change it without breaking something. I&#8217;ve seen tons of code that looks like that, and I&#8217;ve spent ages fixing such code. Believe me, code like this is very expensive and it will soon be full of bugs. Such software deteriorates pretty fast, and before you know it you&#8217;re at a point where whenever you remove a bug, you add two new ones. We should all be ashamed, honestly.</p>
<p>Code that is hard to understand is hard to change; software that is hard to change will be expensive and slow to change, and the risk of the changes adding new bugs is high. Who wants an application where it costs a fortune to add features, takes forever, and adds new bugs?</p>
<p><strong>It&#8217;s about communication</strong></p>
<p>Yes, it is a mess, but what can you do? We have to understand that writing software is about communication!</p>
<p>Put simple: Code that is easy to understand is easy to change. When it&#8217;s easy to change the risk of introducing bugs is low. That&#8217;s the kind of software your customers want!</p>
<p>Martin Fowler hit the nail on the head when he said:</p>
<blockquote><p>&#8220;Any fool can write code that a computer can understand. Good programmers write programs that humans understand.&#8221;</p></blockquote>
<p><strong>What can I do?</strong></p>
<p>When I write software, my main concern is actually if I am expressing my code in a way that others will be able to understand. And the &#8220;others&#8221; are quite often myself. When I get back to code I wrote two months ago I want it to be really easy to both understand and change that code. </p>
<p>When I have to work with code that is hard to understand I often make it easier to understand first, then make the changes. I keep my methods short. I keep my classes short. I try to organize my code so that related logic is grouped together, and unrelated logic is separated.  I write tests for my code that explains the code. <i>I care about the code I write</i>.</p>
<p><i>Writing code that communicates is first and foremost a mindset. Get it!</i> Then it is a set of practices and principles. Read them, train them and practice them. Find out what works for you. Nothing can replace experience.</p>
<p>Robert &#8220;Uncle Bob&#8221; Martin has written an excellent book on the topic: <a href="http://www.amazon.co.uk/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1235312824&#038;sr=8-1">Clean Code</a>. Read it now! Kent Beck is also passionate about this, and wrote this book: <a href="http://www.amazon.co.uk/Implementation-Patterns-Addison-Wesley-Signature-Kent/dp/0321413091/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1235313000&#038;sr=1-1">Implementation Patterns</a>. </p>
<p>I&#8217;m holding various presentations about what I call Code Quality. Communication is a vital part of that. </p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/Rw20G9uFRPY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/03/10/the-importance-of-code-that-communicates/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2009/03/10/the-importance-of-code-that-communicates/</feedburner:origLink></item>
	</channel>
</rss>
