<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[thedersen.com]]></title>
  <link href="https://thedersen.com/atom.xml" rel="self"/>
  <link href="https://thedersen.com/"/>
  <updated>2020-09-17T08:09:19+00:00</updated>
  <id>https://thedersen.com/</id>
  <author>
    <name><![CDATA[Thomas Pedersen]]></name>
    <email><![CDATA[thedersen@gmail.com]]></email>
  </author>

  
  <entry>
    <title type="html"><![CDATA[Learn from the history]]></title>
    <link href="https://thedersen.com/blog/2013/07/30/learn-from-the-history/"/>
    <updated>2013-07-30T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2013/07/30/learn-from-the-history</id>
    <content type="html"><![CDATA[<p>Sharing knowledge is hard. </p>

<p>Even if you are part of a small team, having the overview of what is going on, and especially in the code base, requires quite some effort from all your team members. </p>

<p>And the problem with this lack of overview, is that often it leads to developers solving the same problem, or perhaps similar problems, several times and in different ways. And not only that, but developers also make errors due to lack of knowledge about the domain and the rest of the code base. </p>

<p>Now, we all probably understand that this will eventually lead to a maintenance hell somewhere down the road. And we do not want to go there, do we?</p>

<p>Practices like Pair Programming and Code Reviews are of course great ways of sharing knowledge, but sharing is then limited between two persons and not the whole team.</p>

<p>I’m going to tell you about a little practice I’ve been doing for some years now. Of course, it’s no silver bullit, but I can most certainly promise that it will help you and your team with delivering better, more maintainable software. </p>

<p>It goes like this: <em>When you get in the office in the morning, you grab a cup of coffee as usual. But, instead of drinking your coffee while reading yesterdays news, you drink your coffee while reading yesterdays commits done by you and your team. You pull up the history, read the commit messages, and the changes that are made.</em></p>

<p>When you find something good, you will learn something. When you find something wrong, you tell the person who did it, and work together with him to fix it. </p>

<p>It’s as simple as that, and it only takes you a few minutes every day. </p>

<p>You might experience that it can be a bit hard to begin with, especially if you have a lot of crappy commits. But guess what? You’ll get better at committing or checking in as well. Your commits will get more focused, and your messages will get more descriptive. Your source control is no longer just a place where you keep a backup of your files.</p>

<p>One other thing. I mentioned Code Reviews. They are hard. Not doing them necessarily, but getting people to do them. At least from my experience, things go well in the beginning, but then people tend to not do it so much more. I hope I don’t have to convince you that doing code reviews are a good thing. Right? By following the little advice I’ve just given you, you also get code reviews for free. Let’s call it a check in review. You’ll be amazed by how many bugs, wrongly implemented business rules, and duplicated code you’ll find.</p>

<p>So, start learning from your history, stop repeating it, and encourage your team mates to do the same!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Backbone.Validation]]></title>
    <link href="https://thedersen.com/blog/2011/12/28/backbonevalidation/"/>
    <updated>2011-12-28T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2011/12/28/backbonevalidation</id>
    <content type="html"><![CDATA[<p>Update: If you are looking for documentation for Backbone.Validation it can be found <a href="http://thedersen.com/projects/backbone-validation/">here</a></p>

<p>I just released version 0.2.0 of my <a href="http://github.com/thedersen/backbone.validation">validation plugin</a> for <a href="http://backbonejs.org/">Backbone.js</a>. The plugin lets you declare validation rules on your Backbone model, ensuring that input from the user is valid. It currently ships with 18 built-in validators such as required, min, max, range, length, minLength, maxLength, regex-validator etc. In addition to the built-in validators, it is very easy to extend with your custom validators.</p>

<p>Lets start with an example of how to declare validation rules on your model:</p>

<figure class="highlight"><pre><code class="language-js" data-lang="js"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre><span class="kd">var</span> <span class="nx">SomeModel</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Model</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
  <span class="na">validation</span><span class="p">:</span> <span class="p">{</span>
    <span class="na">name</span><span class="p">:</span> <span class="p">{</span>
      <span class="na">required</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span>
      <span class="na">msg</span><span class="p">:</span> <span class="dl">'</span><span class="s1">Name is required</span><span class="dl">'</span>
    <span class="p">},</span>
    <span class="na">age</span><span class="p">:</span> <span class="p">{</span>
      <span class="na">range</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">80</span><span class="p">]</span>
    <span class="p">},</span>
    <span class="na">email</span><span class="p">:</span> <span class="p">{</span>
      <span class="na">pattern</span><span class="p">:</span> <span class="dl">'</span><span class="s1">email</span><span class="dl">'</span>
    <span class="p">},</span>
    <span class="na">someAttribute</span><span class="p">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">if</span><span class="p">(</span><span class="nx">value</span> <span class="o">!==</span> <span class="dl">'</span><span class="s1">somevalue</span><span class="dl">'</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="dl">'</span><span class="s1">Error</span><span class="dl">'</span><span class="p">;</span>
      <span class="p">}</span>
    <span class="p">}</span>
  <span class="p">}</span>
<span class="p">});</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>To configure your validation rules, simply add a validation object with a property for each attribute you want to validate on your model. The validation rules can either be an object with one of the built-in validators or a combination of two or more of them, or a function where you implement your own custom validation logic. If you want to provide a custom error message when using one of the built-in validators, simply define the <code class="language-plaintext highlighter-rouge">msg</code> property with your message.</p>

<p>To add your own validators, extend the <code class="language-plaintext highlighter-rouge">Backbone.Validation.validators</code> object as shown in the example below where we use <a href="http://underscorejs.org">underscore.js</a> <code class="language-plaintext highlighter-rouge">extend</code> method. You can also redefine one or more of the built-in validators if it doesn’t fit your needs.</p>

<figure class="highlight"><pre><code class="language-js" data-lang="js"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre><span class="nx">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Validation</span><span class="p">.</span><span class="nx">validators</span><span class="p">,</span> <span class="p">{</span>
  <span class="na">myValidator</span><span class="p">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">attr</span><span class="p">,</span> <span class="nx">customValue</span><span class="p">,</span> <span class="nx">model</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span><span class="p">(</span><span class="nx">value</span> <span class="o">!==</span> <span class="nx">customValue</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">return</span> <span class="dl">'</span><span class="s1">error</span><span class="dl">'</span><span class="p">;</span>
    <span class="p">}</span>
  <span class="p">},</span>
  <span class="na">required</span><span class="p">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">attr</span><span class="p">,</span> <span class="nx">customValue</span><span class="p">,</span> <span class="nx">model</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">return</span> <span class="dl">'</span><span class="s1">My version of the required validator</span><span class="dl">'</span><span class="p">;</span>
    <span class="p">}</span>
  <span class="p">},</span>
<span class="p">});</span>

<span class="kd">var</span> <span class="nx">Model</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Model</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
  <span class="na">validation</span><span class="p">:</span> <span class="p">{</span>
    <span class="na">age</span><span class="p">:</span> <span class="p">{</span>
      <span class="na">myValidator</span><span class="p">:</span> <span class="mi">1</span> <span class="c1">// uses your custom validator</span>
    <span class="p">}</span>
  <span class="p">}</span>
<span class="p">});</span>
</pre></td></tr></tbody></table></code></pre></figure>

<h2 id="a-couple-conventions">A couple conventions</h2>

<p>After performing validation, an <code class="language-plaintext highlighter-rouge">isValid</code> attribute is set on the model that is (obviously) <code class="language-plaintext highlighter-rouge">true</code> when all the attributes on the model is valid, otherwise it is <code class="language-plaintext highlighter-rouge">false</code>.</p>

<p>The <code class="language-plaintext highlighter-rouge">Backbone.Validation.callbacks</code> contains two methods: <code class="language-plaintext highlighter-rouge">valid</code> and <code class="language-plaintext highlighter-rouge">invalid</code>. These are called (in addition to the error event raised by Backbone) after validation of an attribute is performed.</p>

<p>The default implementation of invalid tries to look up an element within the view with an id equal to the name of the attribute that is validated. If it finds one, an invalid class is added to the element as well as a data-error attribute with the error message. The valid method removes these if they exists. You can also configure it to use a class selector instead of id selector.</p>

<p>As with the validators, you can redefined the behavior of these callbacks by overriding the implementation.</p>

<figure class="highlight"><pre><code class="language-js" data-lang="js"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre><span class="nx">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Validation</span><span class="p">.</span><span class="nx">callbacks</span><span class="p">,</span> <span class="p">{</span>
  <span class="na">valid</span><span class="p">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">view</span><span class="p">,</span> <span class="nx">attr</span><span class="p">,</span> <span class="nx">selector</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// do something</span>
  <span class="p">},</span>
  <span class="na">invalid</span><span class="p">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">view</span><span class="p">,</span> <span class="nx">attr</span><span class="p">,</span> <span class="nx">error</span><span class="p">,</span> <span class="nx">selector</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// do something</span>
  <span class="p">}</span>
<span class="p">});</span>
</pre></td></tr></tbody></table></code></pre></figure>

<h2 id="validation-binding">Validation binding</h2>

<p>For all of this to work, you need to hook up the validation. The validation binding code is executed with a call to <code class="language-plaintext highlighter-rouge">Backbone.Validation.bind(view)</code>. The <a href="http://backbonejs.org/#Model-validate">validate</a> method on the view’s model is then overridden to perform the validation.</p>

<p>There are several places that it can be called from, depending on your circumstances.</p>

<figure class="highlight"><pre><code class="language-js" data-lang="js"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre><span class="c1">// Binding when rendering</span>
<span class="kd">var</span> <span class="nx">SomeView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
  <span class="na">render</span><span class="p">:</span> <span class="kd">function</span><span class="p">(){</span>
    <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Validation</span><span class="p">.</span><span class="nx">bind</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">});</span>

<span class="c1">// Binding when initializing</span>
<span class="kd">var</span> <span class="nx">SomeView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
  <span class="na">initialize</span><span class="p">:</span> <span class="kd">function</span><span class="p">(){</span>
    <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Validation</span><span class="p">.</span><span class="nx">bind</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">});</span>

<span class="c1">// Binding from outside a view</span>
<span class="kd">var</span> <span class="nx">SomeView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
<span class="p">});</span>
<span class="kd">var</span> <span class="nx">someView</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">SomeView</span><span class="p">();</span>
<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Validation</span><span class="p">.</span><span class="nx">bind</span><span class="p">(</span><span class="nx">someView</span><span class="p">);</span>
</pre></td></tr></tbody></table></code></pre></figure>

<h2 id="summary">Summary</h2>

<p>With this declarative way of defining validation, combined with the conventional updating of your user interface, it makes for an easy way to handle client validation. At least I think so.</p>

<p>But remember: you add client validation to be nice to your users, but you also need to add server validation to be nice to yourself and your company.</p>

<p>The source code for this project is licensed under the <a href="http://thedersen.mit-license.org/">MIT License</a> and is available on <a href="https://github.com/thedersen/backbone.validation">GitHub</a>.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[UnityConfiguration 1.4 is out]]></title>
    <link href="https://thedersen.com/blog/2011/11/15/unityconfiguration-14-is-out/"/>
    <updated>2011-11-15T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2011/11/15/unityconfiguration-14-is-out</id>
    <content type="html"><![CDATA[<p>Some minor improvements has been made, issues are closed, and here’s the new version for you.
<!--more-->
So, what’s changed in v1.4?</p>

<ul>
  <li>Added extension methods to the <code class="language-plaintext highlighter-rouge">IAssemblyScanner</code> for easier discovery and configuration of conventions
    <ul>
      <li><code class="language-plaintext highlighter-rouge">scan.WithFirstInterfaceConvention()</code></li>
      <li><code class="language-plaintext highlighter-rouge">scan.WithNamingConvention()</code></li>
      <li><code class="language-plaintext highlighter-rouge">scan.WithAddAllConvention()</code></li>
      <li><code class="language-plaintext highlighter-rouge">scan.WithSetAllPropertiesConvention()</code></li>
    </ul>
  </li>
  <li>Added non-generic overload to the <code class="language-plaintext highlighter-rouge">AddAllConvention</code> that allows you to register open generic interfaces
    <ul>
      <li><code class="language-plaintext highlighter-rouge">scan.WithAddAllConvention().TypesImplementing(typeof(IHandler&lt;&gt;))</code></li>
    </ul>
  </li>
  <li>Added option on the <code class="language-plaintext highlighter-rouge">IAssemblyScanner</code> interface to scan for internal types in an assembly
    <ul>
      <li><code class="language-plaintext highlighter-rouge">scan.InternalTypes()</code></li>
    </ul>
  </li>
  <li>Strong named the assembly</li>
  <li>Switched to <a href="http://semver.org/">Semantic Versioning</a>. In short this means that when
    <ul>
      <li>the major number is changed: There will be breaking changes</li>
      <li>the minor number is changed: New features are added, but it is still backwards compatible</li>
      <li>the patch number is changed: Backwards compatible bugs fixes only</li>
    </ul>
  </li>
</ul>

<p>As usual you can install it via <a href="http://nuget.org/List/Packages/UnityConfiguration">NuGet</a> using the package manager in Visual Studio:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre>PM&gt; Install-Package UnityConfiguration 
</pre></td></tr></tbody></table></code></pre></figure>

<p>or, if you like, you can grab the code at <a href="http://github.com/thedersen/unityconfiguration">GitHub</a>.</p>

<p>Hope you enjoy it!</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[New version of UnityConfiguration]]></title>
    <link href="https://thedersen.com/blog/2011/07/17/new-version-of-unityconfiguration/"/>
    <updated>2011-07-17T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2011/07/17/new-version-of-unityconfiguration</id>
    <content type="html"><![CDATA[<p>I just pushed a new release of <a href="http://github.com/thedersen/unityconfiguration">UnityConfiguration</a> to <a href="http://nuget.org/List/Packages/UnityConfiguration">NuGet</a>.</p>

<p>So what’s new in v1.3?</p>

<ul>
  <li>Documented public facing methods</li>
  <li>Included the xml documentation in the NuGet package</li>
  <li>Debugging symbols are available on <a href="http://symbolsource.org">SymbolSource.org</a></li>
  <li>Several more overloads for adding assemblies to the scanner</li>
  <li>One more overload for adding a registry that takes an instance of a UnityRegistry</li>
  <li>New convention that scans for registries</li>
  <li>New convention that uses an overridable naming convention for registering a type mapping</li>
  <li>Added Transient, PerThread, PerResolve, ExternallyControlled and HierarchicalControlled lifetime scope configuration</li>
  <li><code>MakeSingleton<T>()&lt;/code&gt; and <code>MakeSingleton<T>(string)&lt;/code&gt; is marked as obsolete. Use <code>Configure<T>().AsSingleton()&lt;/code&gt; or <code>Configure<T>().WithName(name).AsSingleton()&lt;/code&gt; instead</T></code></T></code></T></code></T></code></li>
  <li><code>ConfigureCtorArgsFor<T>(params object[] args)&lt;/code&gt; is marked as obsolete. Use <code>Configure<T>().WithConstructorArguments(params object[])&lt;/code&gt; instead</T></code></T></code></li>
</ul>

<p>Install using the package manager console in Visual Studio:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre>PM&gt; Install-Package UnityConfiguration
</pre></td></tr></tbody></table></code></pre></figure>

<p>Hope you enjoy it!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Auto mocking with Unity and Moq]]></title>
    <link href="https://thedersen.com/blog/2011/02/21/auto-mocking-with-unity-and-moq/"/>
    <updated>2011-02-21T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2011/02/21/auto-mocking-with-unity-and-moq</id>
    <content type="html"><![CDATA[<p>As mentioned in my <a href="http://thedersen.com/2011/02/20/convention-based-configuration-for-microsoft-unity/">previous post</a> we used the <a href="http://unity.codeplex.com/">Microsoft Unity</a> container in a previous project. We also used <a href="http://code.google.com/p/moq/">Moq</a> as our mocking library. Combining these two and making an auto mocking container seemed fairly easy, so I went ahead and created something I called UnityAutoMoq.</p>

<p>Now, some say that auto mocking is a bad practice since it can hide certain design flaws in your system, like classes with to many dependencies etc. I say, that if you have these issues, it is probably not the auto mocking that is causing them. Auto mocking can increase your productivity, so I have no issues with using it. In fact, I encourage you to do so.</p>

<h2 id="using-the-container">Using the container</h2>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre><span class="c1">// Creating a new instance of the auto mock container</span>
<span class="kt">var</span> <span class="n">container</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">UnityAutoMoqContainer</span><span class="p">();</span>

<span class="c1">// Resolving a concrete class automatically creates</span>
<span class="c1">// mocks for the class dependencies and injects them</span>
<span class="c1">// before returning an instance of the class</span>
<span class="n">Service</span> <span class="n">service</span> <span class="p">=</span> <span class="n">container</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">Service</span><span class="p">&gt;();</span>

<span class="c1">// Resolving an interface, returns a mocked</span>
<span class="c1">// instance of that interface</span>
<span class="n">IService</span> <span class="n">mocked</span> <span class="p">=</span> <span class="n">container</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IService</span><span class="p">&gt;();</span>

<span class="c1">// GetMock returns the mock on which you can do setup etc.</span>
<span class="n">Mock</span><span class="p">&lt;</span><span class="n">IService</span><span class="p">&gt;</span> <span class="n">mock</span> <span class="p">=</span> <span class="n">container</span><span class="p">.</span><span class="n">GetMock</span><span class="p">&lt;</span><span class="n">IService</span><span class="p">&gt;();</span>

<span class="c1">// Sometimes you need to cast your interface to some other type</span>
<span class="c1">// This is how that is done</span>
<span class="n">container</span><span class="p">.</span><span class="n">ConfigureMock</span><span class="p">&lt;</span><span class="n">IService</span><span class="p">&gt;().</span><span class="n">As</span><span class="p">&lt;</span><span class="n">IDisposable</span><span class="p">&gt;();</span>
<span class="n">Mock</span><span class="p">&lt;</span><span class="n">IDisposable</span><span class="p">&gt;</span> <span class="n">disposable</span> <span class="p">=</span> <span class="n">container</span><span class="p">.</span><span class="n">GetMock</span><span class="p">&lt;</span><span class="n">IService</span><span class="p">&gt;().</span><span class="n">As</span><span class="p">&lt;</span><span class="n">IDisposable</span><span class="p">&gt;();</span>
</pre></td></tr></tbody></table></code></pre></figure>

<h2 id="download">Download</h2>

<p>The framework is available as a <a href="http://nuget.org/Packages/Packages/Details/UnityAutoMoq-2-0-1-0">NuGet package</a>, and can be installed either using the package manager console in Visual Studio like this:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre>PM&gt; Install-Package UnityAutoMoq
</pre></td></tr></tbody></table></code></pre></figure>

<p>Or by right clicking on references in your project and select the Add Library Package Reference option</p>

<p><img src="/blog/images/unity-auto-moq-nuget.png" alt="NuGet" /></p>

<p>The source code for this project is licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> and is available on my <a href="http://github.com/thedersen/unityautomoq">GitHub profile</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Convention based configuration for Microsoft Unity]]></title>
    <link href="https://thedersen.com/blog/2011/02/20/convention-based-configuration-for-microsoft-unity/"/>
    <updated>2011-02-20T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2011/02/20/convention-based-configuration-for-microsoft-unity</id>
    <content type="html"><![CDATA[<p>In a previous project, long story short, we used the <a href="http://unity.codeplex.com/">Microsoft Unity</a> IoC container. After writing line upon line with configuration statements while looking at the nice convention based configuration options for other containers, I decided that enough was enough. Replacing the Unity container was sadly not an option at the moment, so I went ahead and created a convention based configuration api for Unity.</p>

<p>Below you can see a small example of configuring a container using registries, and I should probably apologize for the poor (or rather lack of) documentation at this time.</p>

<p>You get access to the configuration api by using the extension method Configure on the IUnityContainer interface.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="code"><pre><span class="kt">var</span> <span class="n">container</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">UnityContainer</span><span class="p">();</span>
<span class="n">container</span><span class="p">.</span><span class="nf">Configure</span><span class="p">(</span><span class="n">x</span> <span class="p">=&gt;</span>
<span class="p">{</span>
  <span class="n">x</span><span class="p">.</span><span class="n">AddRegistry</span><span class="p">&lt;</span><span class="n">FooRegistry</span><span class="p">&gt;();</span>
  <span class="n">x</span><span class="p">.</span><span class="n">AddRegistry</span><span class="p">&lt;</span><span class="n">BarRegistry</span><span class="p">&gt;();</span>
<span class="p">});</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>Configuration is done in one or several registries that inherit the UnityRegistry base class.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">class</span> <span class="nc">FooRegistry</span> <span class="p">:</span> <span class="n">UnityRegistry</span>
<span class="p">{</span>
  <span class="k">public</span> <span class="nf">FooRegistry</span><span class="p">()</span>
  <span class="p">{</span>
    <span class="c1">// Scan one or several assemblies and auto register types based on a</span>
    <span class="c1">// convention. You can also include or exclude certain types and/or</span>
    <span class="c1">// namespaces using a filter.</span>
    <span class="nf">Scan</span><span class="p">(</span><span class="n">scan</span> <span class="p">=&gt;</span>
    <span class="p">{</span>
      <span class="n">scan</span><span class="p">.</span><span class="n">AssemblyContaining</span><span class="p">&lt;</span><span class="n">FooRegistry</span><span class="p">&gt;();</span>
      <span class="n">scan</span><span class="p">.</span><span class="n">With</span><span class="p">&lt;</span><span class="n">FirstInterfaceConvention</span><span class="p">&gt;();</span>
      <span class="n">scan</span><span class="p">.</span><span class="n">With</span><span class="p">&lt;</span><span class="n">AddAllConvention</span><span class="p">&gt;().</span><span class="n">TypesImplementing</span><span class="p">&lt;</span><span class="n">IHaveManyImplementations</span><span class="p">&gt;();</span>
      <span class="n">scan</span><span class="p">.</span><span class="n">With</span><span class="p">&lt;</span><span class="n">SetAllPropertiesConvention</span><span class="p">&gt;().</span><span class="n">OfType</span><span class="p">&lt;</span><span class="n">ILogger</span><span class="p">&gt;();</span>
      <span class="n">scan</span><span class="p">.</span><span class="n">ExcludeType</span><span class="p">&lt;</span><span class="n">FooService</span><span class="p">&gt;();</span>
    <span class="p">});</span>

    <span class="c1">// Manually register a service</span>
    <span class="n">Register</span><span class="p">&lt;</span><span class="n">IFooService</span><span class="p">,</span> <span class="n">FooService</span><span class="p">&gt;().</span><span class="nf">WithName</span><span class="p">(</span><span class="s">"Foo"</span><span class="p">).</span><span class="nf">AsSingleton</span><span class="p">();</span>

    <span class="c1">// Make services a singleton</span>
    <span class="n">MakeSingleton</span><span class="p">&lt;</span><span class="n">ISingletonService</span><span class="p">&gt;();</span>

    <span class="c1">// You can automatically configure the container to call</span>
    <span class="c1">// a method on any service when they are created</span>
    <span class="n">AfterBuildingUp</span><span class="p">&lt;</span><span class="n">IStartable</span><span class="p">&gt;().</span><span class="nf">Call</span><span class="p">((</span><span class="n">c</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">s</span><span class="p">.</span><span class="nf">Start</span><span class="p">();</span>

    <span class="c1">// You can automatically configure the container to</span>
    <span class="c1">// decorate services when they are created</span>
    <span class="n">AfterBuildingUp</span><span class="p">&lt;</span><span class="n">IFooService</span><span class="p">&gt;().</span><span class="nf">DecorateWith</span><span class="p">((</span><span class="n">c</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="nf">FooDecorator</span><span class="p">(</span><span class="n">t</span><span class="p">));</span>

    <span class="c1">// If you want to inject values or objects that are not registered in</span>
    <span class="c1">// the container, you can do so by adding them using this statement.</span>
    <span class="c1">// For instances you want the container to create, just specify the type.</span>
    <span class="n">ConfigureCtorArgsFor</span><span class="p">&lt;</span><span class="n">ServiceWithCtorArgs</span><span class="p">&gt;(</span><span class="s">"some string"</span><span class="p">,</span> <span class="k">typeof</span> <span class="p">(</span><span class="n">IFooService</span><span class="p">));</span>

    <span class="c1">// Unity follows the greedy constructor pattern when creating instances.</span>
    <span class="c1">// If you want to use a different constructor, you specify it by listing</span>
    <span class="c1">// the types of the arguments in the constructor you want it to use.</span>
    <span class="n">SelectConstructor</span><span class="p">&lt;</span><span class="n">ServiceWithCtorArgs</span><span class="p">&gt;(</span><span class="k">typeof</span> <span class="p">(</span><span class="n">IFooService</span><span class="p">));</span>
  <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>At the moment, built in conventions includes AddAllConvention, FirstInterfaceConvention and SetAllPropertiesConvention. If these doesn’t suit you, creating custom conventions is as easy as creating a class that implements the IAssemblyScanner interface.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">class</span> <span class="nc">CustomConvention</span> <span class="p">:</span> <span class="n">IAssemblyScannerConvention</span>
<span class="p">{</span>
  <span class="k">void</span> <span class="n">IAssemblyScannerConvention</span><span class="p">.</span><span class="nf">Process</span><span class="p">(</span><span class="n">Type</span> <span class="n">type</span><span class="p">,</span> <span class="n">IUnityRegistry</span> <span class="n">registry</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">type</span> <span class="p">==</span> <span class="k">typeof</span><span class="p">(</span><span class="n">FooService</span><span class="p">))</span>
      <span class="n">registry</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">IFooService</span><span class="p">,</span> <span class="n">FooService</span><span class="p">&gt;().</span><span class="nf">WithName</span><span class="p">(</span><span class="s">"Custom"</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<h2 id="download">Download</h2>

<p>The framework is available as a <a href="http://nuget.org/Packages/Packages/Details/UnityConfiguration-1-2-0-0">NuGet package</a>, and can be installed either using the package manager console in Visual Studio like this:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre>	PM&gt; Install-Package UnityConfiguration
</pre></td></tr></tbody></table></code></pre></figure>

<p>Or by right clicking on references in your project and select the Add Library Package Reference option</p>

<p><img src="/blog/images/unity-configuration-nuget.png" alt="NuGet" /></p>

<p>The source code for this project is licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> and is available on my <a href="http://github.com/thedersen/unityconfiguration">GitHub profile</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Best of 2010]]></title>
    <link href="https://thedersen.com/blog/2010/12/19/best-of-2010/"/>
    <updated>2010-12-19T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/12/19/best-of-2010</id>
    <content type="html"><![CDATA[<p>It’s that time of the year again. Lists are popping up all over. Here is one more.</p>

<ul id="paginate">
	<li>
		<img src="/blog/images/bestof2010/01.jpg" width="500" height="500" alt="" />
		<p>01. Arcade Fire - The Suburbs</p>
	</li>
   	<li>
   		<img src="/blog/images/bestof2010/02.jpg" width="500" height="500" alt="" />
   		<p>02. The National - High Violet</p>
   	</li>
	<li>
		<img src="/blog/images/bestof2010/03.jpg" width="500" height="500" alt="" />
		<p>03. Sufjan Stevens - The Age of Adz</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/04.jpg" width="500" height="500" alt="" />
		<p>04. Deerhunter - Halcyon Digest</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/05.jpg" width="500" height="500" alt="" />
		<p>05. Motorpsycho - Heavy Metal Fruit</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/06.jpg" width="500" height="500" alt="" />
		<p>06. Beach House - Teen Dream</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/07.jpg" width="500" height="500" alt="" />
		<p>07. LCD Soundsystem - This Is Happening</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/08.jpg" width="500" height="500" alt="" />
		<p>08. Tame Impala - Innerspeaker</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/09.jpg" width="500" height="500" alt="" />
		<p>09. Shearwater - The Golden Archipelago</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/10.jpg" width="500" height="500" alt="" />
		<p>10. Menomena - Mines</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/11.jpg" width="500" height="500" alt="" />
		<p>11. The Black Angels - Phosphene Dream</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/12.jpg" width="500" height="500" alt="" />
		<p>12. The Besnard Lakes - The Besnard Lakes Are The Roaring Night</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/13.jpg" width="500" height="500" alt="" />
		<p>13. The Walkmen - Lisbon</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/14.jpg" width="500" height="500" alt="" />
		<p>14. I Am Kloot - Sky at Night</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/15.jpg" width="500" height="500" alt="" />
		<p>15. Titus Andronicus - The Monitor</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/16.jpg" width="500" height="500" alt="" />
		<p>16. Broken Social Scene - Forgiveness Rock Record</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/17.jpg" width="500" height="500" alt="" />
		<p>17. Perfume Genius - Learning</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/18.jpg" width="500" height="500" alt="" />
		<p>18. Belle &amp; Sebastian - Write about Love</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/19.jpg" width="500" height="500" alt="" />
		<p>19. The Black Keys - Brothers</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/20.jpg" width="500" height="500" alt="" />
		<p>20. Black Mountain - Wilderness Heart</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/21.jpg" width="500" height="500" alt="" />
		<p>21. The Tallest Man on Earth - The Wild Hunt</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/22.jpg" width="500" height="500" alt="" />
		<p>22. Suckers - Wild Smile</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/23.jpg" width="500" height="500" alt="" />
		<p>23. The Morning Benders - Big Echo</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/24.jpg" width="500" height="500" alt="" />
		<p>24. MGMT - Congratulations</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/25.jpg" width="500" height="500" alt="" />
		<p>25. Midlake - The Courage Of Others</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/26.jpg" width="500" height="500" alt="" />
		<p>26. Wolf Parade - Expo 86</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/27.jpg" width="500" height="500" alt="" />
		<p>27. Broken Bells - Broken Bells</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/28.jpg" width="500" height="500" alt="" />
		<p>28. Ariel Pink's Haunted Graffiti - Before Today</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/29.jpg" width="500" height="500" alt="" />
		<p>29. Teenage Fanclub - Shadows</p>
	</li>
	<li>
		<img src="/blog/images/bestof2010/30.jpg" width="500" height="500" alt="" />
		<p>30. of Montreal - False Priest</p>
	</li>
</ul>
<div class="clear">&nbsp;</div>

<p>It’s been a good year.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Setting up a Rails 3 app with RSpec, Cucumber, MongoDB and jQuery]]></title>
    <link href="https://thedersen.com/blog/2010/09/13/setting-up-a-rails-3-app-with-rspec-cucumber-mongodb-and-jquery/"/>
    <updated>2010-09-13T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/09/13/setting-up-a-rails-3-app-with-rspec-cucumber-mongodb-and-jquery</id>
    <content type="html"><![CDATA[<p>With <a href="http://weblog.rubyonrails.org/2010/8/29/rails-3-0-it-s-done">Rails 3 released</a>, it comes with a set of new switches that let you opt out various stuff when generating apps. This makes it a bit easier to use alternatives to the default Active Record, Test::Unit etc. than it was with previous versions of Rails. Some of the options are:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>-O, [--skip-activerecord]  # Skip Active Record files
-J, [--skip-prototype]     # Skip Prototype files
-T, [--skip-testunit]      # Skip Test::Unit files
</code></pre></div></div>

<p>You can get a complete list of all the available options by running the command:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre>	<span class="nv">$ </span>rails new <span class="nt">--help</span>
</pre></td></tr></tbody></table></code></pre></figure>

<h2 id="creating-the-app">Creating the app</h2>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="code"><pre><span class="nv">$ </span><span class="nb">sudo </span>gem <span class="nb">install </span>rails
<span class="nv">$ </span>rails new YourApp <span class="nt">-OJT</span>
<span class="nv">$ </span><span class="nb">cd </span>YourApp
</pre></td></tr></tbody></table></code></pre></figure>

<p>The gem command will install the latest version of Rails 3. The rails command creates a new app named ‘YourApp’ without Active Record (-O), Test::Unit (-T), and the Prototype javascript files (-J).</p>

<h2 id="configuring-the-app-and-the-required-gems">Configuring the app and the required gems</h2>

<p>To get things working you first have to install all the required gems. Since Rails 3 introduces  <a href="http://gembundler.com/">Bundler</a>, the gem management is as simple as it can get.</p>

<p>Open your Gemfile at the root of you application and specify the gems:</p>

<figure class="highlight"><pre><code class="language-rb" data-lang="rb"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre><span class="n">source</span> <span class="s2">"http://rubygems.org"</span>

<span class="n">gem</span> <span class="s2">"rails"</span><span class="p">,</span> <span class="s2">"3.0.0"</span>
<span class="n">gem</span> <span class="s2">"bson_ext"</span>
<span class="n">gem</span> <span class="s2">"mongo_mapper"</span>

<span class="n">group</span> <span class="ss">:test</span><span class="p">,</span> <span class="ss">:spec</span><span class="p">,</span> <span class="ss">:cucumber</span> <span class="k">do</span>
  <span class="n">gem</span> <span class="s2">"rspec"</span>
  <span class="n">gem</span> <span class="s2">"rspec-rails"</span><span class="p">,</span> <span class="s2">"&gt;= 2.0.0.beta"</span>
  <span class="n">gem</span> <span class="s2">"capybara"</span>
  <span class="n">gem</span> <span class="s2">"cucumber"</span>
  <span class="n">gem</span> <span class="s2">"database_cleaner"</span>
  <span class="n">gem</span> <span class="s2">"cucumber-rails"</span>
  <span class="n">gem</span> <span class="s2">"spork"</span>
  <span class="n">gem</span> <span class="s2">"launchy"</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>Then, install all of them by simply running:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="nv">$ </span>bundle <span class="nb">install</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>Finally, you have to tell the generators that you are not using the default stuff. You do that by editing the  config/application.rb and add these lines:</p>

<figure class="highlight"><pre><code class="language-rb" data-lang="rb"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="code"><pre><span class="n">config</span><span class="p">.</span><span class="nf">generators</span> <span class="k">do</span> <span class="o">|</span><span class="n">g</span><span class="o">|</span>
	<span class="n">g</span><span class="p">.</span><span class="nf">orm</span>             <span class="ss">:mongo_mapper</span>
	<span class="n">g</span><span class="p">.</span><span class="nf">template_engine</span> <span class="ss">:erb</span>
	<span class="n">g</span><span class="p">.</span><span class="nf">test_framework</span>  <span class="ss">:rspec</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></figure>

<h2 id="setting-up-the-connection-to-mongodb">Setting up the connection to MongoDB</h2>

<p>Create a new file, config/initializers/mongo.rb with the following content:</p>

<figure class="highlight"><pre><code class="language-rb" data-lang="rb"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="code"><pre><span class="n">logger</span> <span class="o">=</span> <span class="no">Logger</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s2">"log/mongodb-</span><span class="si">#{</span><span class="no">Rails</span><span class="p">.</span><span class="nf">env</span><span class="si">}</span><span class="s2">.log"</span><span class="p">)</span>
<span class="no">MongoMapper</span><span class="p">.</span><span class="nf">connection</span> <span class="o">=</span> <span class="no">Mongo</span><span class="o">::</span><span class="no">Connection</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s1">'127.0.0.1'</span><span class="p">,</span> <span class="mi">27017</span><span class="p">,</span> <span class="ss">:logger</span> <span class="o">=&gt;</span> <span class="n">logger</span><span class="p">)</span>
<span class="no">MongoMapper</span><span class="p">.</span><span class="nf">database</span> <span class="o">=</span> <span class="s2">"YourApp-</span><span class="si">#{</span><span class="no">Rails</span><span class="p">.</span><span class="nf">env</span><span class="si">}</span><span class="s2">"</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>If you use different databases in the different environments (e.g. dev, production), go ahead and put some if-else logic in here.</p>

<h2 id="generating-the-cucumber-and-rspec-stuff">Generating the Cucumber and RSpec stuff</h2>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
</pre></td><td class="code"><pre><span class="nv">$ </span>rails g rspec:install    
<span class="nv">$ </span>rails g cucumber:install <span class="nt">--capybara</span> <span class="nt">--rspec</span> <span class="nt">--skip-database</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>That’s it. You’ll now have a folder <em>features</em> for your Cucumber features and a folder <em>spec</em> for your RSpec specs.</p>

<h2 id="adding-jquery">Adding jQuery</h2>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
</pre></td><td class="code"><pre><span class="nv">$ </span>curl http://code.jquery.com/jquery-1.4.2.min.js <span class="o">&gt;</span> public/javascripts/jquery-1.4.2.min.js
<span class="nv">$ </span>curl http://github.com/rails/jquery-ujs/raw/master/src/rails.js <span class="o">&gt;</span> public/javascripts/rails.js
</pre></td></tr></tbody></table></code></pre></figure>

<p>The first line downloads jQuery 1.4.2 and saves it to your public/javascripts/ folder. The second line downloads rails.js which is a wrapper around jQuery for Rails.</p>

<p>Finally, specify that the javascripts should be loaded as part of the default scripts by opening the file config/application.rb and change</p>

<figure class="highlight"><pre><code class="language-rb" data-lang="rb"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="n">config</span><span class="p">.</span><span class="nf">action_view</span><span class="p">.</span><span class="nf">javascript_expansions</span><span class="p">[</span><span class="ss">:defaults</span><span class="p">]</span> <span class="o">=</span> <span class="sx">%w()</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>to</p>

<figure class="highlight"><pre><code class="language-rb" data-lang="rb"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="n">config</span><span class="p">.</span><span class="nf">action_view</span><span class="p">.</span><span class="nf">javascript_expansions</span><span class="p">[</span><span class="ss">:defaults</span><span class="p">]</span> <span class="o">=</span> <span class="sx">%w(jquery rails application)</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>And, you’re done!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Shotgun]]></title>
    <link href="https://thedersen.com/blog/2010/08/23/shotgun/"/>
    <updated>2010-08-23T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/08/23/shotgun</id>
    <content type="html"><![CDATA[<p>In my <a href="http://http://thedersen.com/2010/08/05/new-blog-in-five-minutes/">previous post</a> I wrote about using the thin web server to run your Rack app locally. This works fine, but there is one issue. If you change your code or configuration you have to restart the server. This is not a big deal, but it do get quite annoying after a while. The solution is a gem called <a href="http://github.com/rtomayko/shotgun">Shotgun</a>.</p>

<p>From the <a href="http://github.com/rtomayko/shotgun/blob/master/README">readme</a>:</p>
<blockquote>
  <p>The shotgun command starts one of Rack’s supported servers (e.g., mongrel, thin,
webrick) and listens for requests but does not load any part of the actual
application. Each time a request is received, it forks, loads the application in
the child process, processes the request, and exits the child process. The
result is clean, application-wide reloading of all source files and templates on
each request.</p>
</blockquote>

<p>As you can see, there in no longer a need to restart your server when code has changed, Shotgun does it for you. Nice.</p>

<p>To get going you need to install the gem</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="nv">$ </span><span class="nb">sudo </span>gem <span class="nb">install </span>shotgun
</pre></td></tr></tbody></table></code></pre></figure>

<p>To start the web server, simply type</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="nv">$ </span>shotgun
</pre></td></tr></tbody></table></code></pre></figure>

<p>if the current directory contains the config.ru file, or specify the config file like this</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="nv">$ </span>shotgun config.ru
</pre></td></tr></tbody></table></code></pre></figure>

<p>The output should be something like</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="o">==</span> Shotgun/Mongrel on http://127.0.0.1:9393/
</pre></td></tr></tbody></table></code></pre></figure>

<p>telling you which server got started and on what port it is listening.</p>

<p>There are some more options available, which you can find by looking at the help</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre><span class="nv">$ </span>shotgun <span class="nt">--help</span>

Ruby options:
  <span class="nt">-e</span>, <span class="nt">--eval</span> LINE          evaluate a LINE of code
  <span class="nt">-d</span>, <span class="nt">--debug</span>              <span class="nb">set </span>debugging flags <span class="o">(</span><span class="nb">set</span> <span class="nv">$DEBUG</span> to <span class="nb">true</span><span class="o">)</span>
  <span class="nt">-w</span>, <span class="nt">--warn</span>               turn warnings on <span class="k">for </span>your script
  <span class="nt">-I</span>, <span class="nt">--include</span> PATH       specify <span class="nv">$LOAD_PATH</span> <span class="o">(</span>may be used more than once<span class="o">)</span>
  <span class="nt">-r</span>, <span class="nt">--require</span> LIBRARY    require the library, before executing your script

Rack options:
  <span class="nt">-s</span>, <span class="nt">--server</span> SERVER      server <span class="o">(</span>webrick, mongrel, thin, etc.<span class="o">)</span>
  <span class="nt">-o</span>, <span class="nt">--host</span> HOST          listen on HOST <span class="o">(</span>default: 127.0.0.1<span class="o">)</span>
  <span class="nt">-p</span>, <span class="nt">--port</span> PORT          use PORT <span class="o">(</span>default: 9393<span class="o">)</span>
  <span class="nt">-E</span>, <span class="nt">--env</span> ENVIRONMENT    use ENVIRONMENT <span class="k">for </span>defaults <span class="o">(</span>default: development<span class="o">)</span>

Shotgun options:
  <span class="nt">-O</span>, <span class="nt">--browse</span>             open browser immediately after starting
  <span class="nt">-u</span>, <span class="nt">--url</span> URL            specify url path <span class="o">(</span>default: /<span class="o">)</span>
  <span class="nt">-P</span>, <span class="nt">--public</span> PATH        serve static files under PATH
  <span class="nt">-h</span>, <span class="nt">--help</span>               show this message
      <span class="nt">--version</span>            show version
</pre></td></tr></tbody></table></code></pre></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[New blog in five minutes]]></title>
    <link href="https://thedersen.com/blog/2010/08/05/new-blog-in-five-minutes/"/>
    <updated>2010-08-05T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/08/05/new-blog-in-five-minutes</id>
    <content type="html"><![CDATA[<p>As part of learning Ruby I wanted to move my blog to the Ruby platform. I started out by creating one myself using <a href="http://rubyonrails.org/">Ruby on Rails</a>, but then I came across <a href="http://cloudhead.io/toto">toto</a>, “the tiniest blogging engine in Oz”, in a <a href="http://cre8ivethought.com/blog/2010/08/04/blog-moved-once-again/">blog post by Mark Nijhof</a>. It looked very promising, and I decided to give it a shot.</p>

<p>This walkthrough assumes that you have installed <a href="http://www.ruby-lang.org/en/">Ruby</a> and <a href="http://git-scm.com/">Git</a> and is somewhat familiar with using Git. Oh, and I am using a Mac, but I think it should work just as fine on Windows.</p>

<h2 id="toto">toto</h2>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="nv">$ </span><span class="nb">sudo </span>gem <span class="nb">install </span>toto
</pre></td></tr></tbody></table></code></pre></figure>

<p>That’s it. You have now installed the blog engine, but before you can get started writing your articles you need a toto template.</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
</pre></td><td class="code"><pre><span class="nv">$ </span>git clone git://github.com/cloudhead/dorothy.git myblog
<span class="nv">$ </span><span class="nb">cd </span>myblog
</pre></td></tr></tbody></table></code></pre></figure>

<p>Dorothy is toto’s default template. It’s very minimalistic, but fully functional. If you want to customize it, all the files you need to change is located in the templates and public folders. It also comes with a <a href="http://rack.rubyforge.org/">Rack</a> configuration file, <em>config.ru</em>, where you can configure your toto instance.</p>

<h2 id="writing-in-markdown">Writing in Markdown</h2>
<p>toto uses no database or anything except .txt files for storing articles with metadata. The articles are written in <a href="http://daringfireball.net/projects/markdown/">Markdown</a> and the metadata is stored in <a href="http://www.yaml.org/">yaml</a> at the very beginning of the file. Markdown is a plain text formatting syntax that gets converted to html. Its goal is to make it as readable as possible and I think they’ve succeeded. All you need is a simple text editor to get going.</p>

<p>To create a new article I recommend using the rake task for it.</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
</pre></td><td class="code"><pre><span class="nv">$ </span>rake new
Title:
</pre></td></tr></tbody></table></code></pre></figure>

<p>You enter the title, hit enter, and the .txt file is created with necessary metadata and saved to the articles folder.</p>

<p>Open the file and start writing your article. When you are done writing, you need to commit the article to the git repository.</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
</pre></td><td class="code"><pre><span class="nv">$ </span>git add <span class="nt">-A</span>
<span class="nv">$ </span>git commit <span class="nt">-m</span><span class="s2">"Added new article"</span>
</pre></td></tr></tbody></table></code></pre></figure>

<h2 id="hosting-it">Hosting it</h2>
<p>The best (in my opinion) hosting option for Ruby is <a href="http://heroku.com/">Heroku</a>. It’s very easy to set up and it uses a pure git workflow for publishing your blog or site. For a small blog like mine, it is also completely free. No cost what so ever.</p>

<p>To publish your blog go to <a href="http://heroku.com/">Heroku</a> and sign up for an account. Then install Heroku on your machine.</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="nv">$ </span><span class="nb">sudo </span>gem <span class="nb">install </span>heroku
</pre></td></tr></tbody></table></code></pre></figure>

<p>After that is done, you need to make your blog a Heroku application.</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="code"><pre><span class="nv">$ </span>heroku create myblog
Created http://myblog.heroku.com
git@heroku.com:myblog.git
</pre></td></tr></tbody></table></code></pre></figure>

<p>Finally, you publish it. There are two ways of doing so. You can either use the rake task that comes with Dorothy or you can use the git push command. Essentially, they both do the same thing.</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="nv">$ </span>rake publish
</pre></td></tr></tbody></table></code></pre></figure>

<p>or</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="code"><pre><span class="nv">$ </span>git push heroku master
<span class="nt">-----</span><span class="o">&gt;</span> Heroku receiving push
<span class="nt">-----</span><span class="o">&gt;</span> Rack app detected
<span class="nt">-----</span><span class="o">&gt;</span> Launching......... <span class="k">done</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>Your blog is now online! If you want to use your own domain it is also possible and very easy to set up with Heroku.</p>

<h2 id="disqus-it">Disqus it</h2>
<p>As part of the minimalistic approach of toto, it does not come with any custom built commentary module. For comments it uses the <a href="http://disqus.com/">Disqus service</a>. If you would like to enable comments, you go to <a href="http://disqus.com/">http://disqus.com/</a>, register for an account, and then you open the toto configuration file, config.ru, and changes</p>

<figure class="highlight"><pre><code class="language-rb" data-lang="rb"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="c1">#set :disqus, false          # disqus id, or false</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>to</p>

<figure class="highlight"><pre><code class="language-rb" data-lang="rb"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="n">set</span> <span class="ss">:disqus</span><span class="p">,</span> <span class="s2">"YourDisqusId"</span>  <span class="c1"># disqus id, or false</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>People can now comment on your articles.</p>

<h2 id="running-it-locally">Running it locally</h2>
<p>Sometimes it can be very useful to run the blog on local machine. Especially when tweaking layout and css there can be a lot of changes. I choose to run it on the <a href="http://code.macournoyer.com/thin/">thin web server</a>.</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="nv">$ </span><span class="nb">sudo </span>gem <span class="nb">install </span>thin
</pre></td></tr></tbody></table></code></pre></figure>

<p>To start the web server type:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="nv">$ </span>thin start <span class="nt">-R</span> config.ru
</pre></td></tr></tbody></table></code></pre></figure>

<p>When I started it the first time I got an error</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre>/Library/Ruby/Gems/1.8/gems/rack-1.2.1/lib/rack/utils.rb:138:in <span class="sb">`</span>union<span class="s1">': can'</span>t convert Array into String <span class="o">(</span>TypeError<span class="o">)</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>After some Googling I found that the error was most likely caused by Rack 1.2.1. When downgrading to Rack 1.1.0 it worked, although I don’t know why (yet).</p>

<p>When thin is running, go to http://localhost:3000 and browse your blog.</p>

<h2 id="summary">Summary</h2>
<p>It took me longer to write this article than to get the blog up and running. That, I think, says a lot. I really like the minimalistic approach of toto and the frictionless publishing to Heroku. I also actually like writing in markdown.</p>

<p>If you are considering creating a blog, or maybe moving your existing, I would definitively recommend the toto and Heroku combo. After all, it will only cost you five minutes.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Show hidden files in Finder]]></title>
    <link href="https://thedersen.com/blog/2010/08/04/show-hidden-files-in-finder/"/>
    <updated>2010-08-04T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/08/04/show-hidden-files-in-finder</id>
    <content type="html"><![CDATA[<p>By convention all files starting with a dot (.) is hidden in Mac OS X. To view these files open terminal and type the following:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
</pre></td><td class="code"><pre><span class="nv">$ </span>defaults write com.apple.finder AppleShowAllFiles TRUE
<span class="nv">$ </span>killall Finder
</pre></td></tr></tbody></table></code></pre></figure>

<p>This will show you all of the hidden files and folders on your operating system. If you want to reverse the command replace TRUE with FALSE.</p>

<p>That’s all.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Showing modal dialogs from a background thread]]></title>
    <link href="https://thedersen.com/blog/2010/05/23/showing-modal-dialogs-from-a-background-thread/"/>
    <updated>2010-05-23T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/05/23/showing-modal-dialogs-from-a-background-thread</id>
    <content type="html"><![CDATA[<p>Showing a modal dialog on another thread than the main UI thread results for some in unexpected behavior. The dialog is not at all modal, and you have no control over where the dialog shows up. For a dialog to be shown properly, it must be opened on the UI thread. Sometimes, however, this is not always possible, especially if you have long running tasks that need user input during the operation.</p>

<p><code>SynchronizationContext</code> to the rescue. Here’s what the documentation says:</p>

<blockquote>
  <p>Provides the basic functionality for propagating a synchronization context in various synchronization models. The purpose of the synchronization model implemented by this class is to allow the internal asynchronous/synchronization operations of the common language runtime (CLR) to behave properly with different synchronization models. This model also simplifies some of the requirements that managed applications have had to follow in order to work correctly under different synchronization environments.</p>
</blockquote>

<p>What this means is that you can use the <code>SynchronizationContext</code> class to dispatch a call to the right thread synchronously using <code>Send</code>, or asynchronously using <code>Post</code>.</p>

<p>Here is a simple example where we show a message box from a background worker using the <code>Send</code> method on the <code>SynchronizationContext</code> class. When we initialize the <code>Worker</code> class on line 15 we pass in <code>SynchronizationContext.Current</code> which holds the synchronization context for the current thread, i.e. the main UI thread. We then use this context to dispatch calls from the background worker to the UI thread, and the message box is properly shown.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">partial</span> <span class="k">class</span> <span class="nc">Form</span> <span class="p">:</span> <span class="n">System</span><span class="p">.</span><span class="n">Windows</span><span class="p">.</span><span class="n">Forms</span><span class="p">.</span><span class="n">Form</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="nf">Form</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nf">InitializeComponent</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="k">private</span> <span class="k">void</span> <span class="nf">buttonStart_Click</span><span class="p">(</span><span class="kt">object</span> <span class="n">sender</span><span class="p">,</span> <span class="n">EventArgs</span> <span class="n">e</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="nf">StartAsyncJob</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="k">private</span> <span class="k">void</span> <span class="nf">StartAsyncJob</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="kt">var</span> <span class="n">worker</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">Worker</span><span class="p">(</span><span class="n">SynchronizationContext</span><span class="p">.</span><span class="n">Current</span><span class="p">);</span>

        <span class="n">backgroundWorker</span><span class="p">.</span><span class="n">DoWork</span> <span class="p">+=</span> <span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">worker</span><span class="p">.</span><span class="nf">DoWork</span><span class="p">();</span>
        <span class="n">backgroundWorker</span><span class="p">.</span><span class="nf">RunWorkerAsync</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="k">public</span> <span class="k">class</span> <span class="nc">Worker</span>
<span class="p">{</span>
    <span class="k">private</span> <span class="k">readonly</span> <span class="n">SynchronizationContext</span> <span class="n">_synchronizationContext</span><span class="p">;</span>

    <span class="k">public</span> <span class="nf">Worker</span><span class="p">(</span><span class="n">SynchronizationContext</span> <span class="n">synchronizationContext</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_synchronizationContext</span> <span class="p">=</span> <span class="n">synchronizationContext</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">DoWork</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="n">_synchronizationContext</span><span class="p">.</span><span class="nf">Send</span><span class="p">(</span><span class="n">callback</span> <span class="p">=&gt;</span> <span class="n">MessageBox</span><span class="p">.</span><span class="nf">Show</span><span class="p">(</span><span class="s">"Hello from background worker!"</span><span class="p">),</span> <span class="k">null</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>The full example application can be found <a href="http://github.com/thedersen/Sandbox/tree/master/SynchronizationContext/">here</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Dependency Inversion]]></title>
    <link href="https://thedersen.com/blog/2010/04/20/dependency-inversion/"/>
    <updated>2010-04-20T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/04/20/dependency-inversion</id>
    <content type="html"><![CDATA[<p>In object-oriented programming we create a lot of classes. In order for a class to be somewhat useful, it usually needs to communicate with other classes; classes depend on each other to create functionality. This dependency can also be seen as coupling between classes and different parts of our system. High coupling can make a system harder to change, and changes made can ripple through to places you didn’t foresee. Therefore we strive to have as low coupling as possible.</p>

<p>The Dependency Inversion Principle is one form of decoupling as it states:</p>

<ol>
  <li><em>High-level modules should not depend on low-level modules. Both should depend on abstractions.</em></li>
  <li><em>Abstractions should not depend upon details. Details should depend upon abstractions.</em></li>
</ol>

<p>So what does this actually mean? Let’s look at a method in a <code>BlogService</code>.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">class</span> <span class="nc">BlogService</span>
<span class="p">{</span>
    <span class="k">private</span> <span class="n">Repository</span> <span class="n">_repository</span><span class="p">;</span>

    <span class="k">public</span> <span class="nf">BlogService</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="n">_repository</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">Repository</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">ProcessComment</span><span class="p">(</span><span class="n">Comment</span> <span class="n">comment</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_repository</span><span class="p">.</span><span class="nf">Save</span><span class="p">(</span><span class="n">comment</span><span class="p">);</span>

        <span class="c1">// Send notification to author and do other stuff here</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>The <code>BlogService</code>, being the high-level module, has a dependency on the <code>Repository</code>, the low-level module. (A dependency, by the way, can easily be spotted by looking for the new keyword.)</p>

<p><img src="/blog/images/dependency-inversion-01.png" alt="Image01" /></p>

<p>To break this dependency, we are supposed to invert the dependency and make both the <code>Repository</code> and <code>BlogService</code> dependent to an abstraction.</p>

<p>To create the abstraction we introduce an interface called <code>IRepository</code>. In order to make the <code>BlogService</code> depend upon the abstraction, we need to remove the creation of the <code>Repository</code> inside of the <code>BlogService</code>. Instead we inject an <code>IRepository</code> in the constructor when we create a <code>BlogService</code> instance. This way the <code>BlogService</code> is decoupled from the Repository, and it does not really care what repository it gets, as long as it implements the <code>IRepository</code>.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">interface</span> <span class="nc">IRepository</span>
<span class="p">{</span>
    <span class="k">void</span> <span class="nf">Save</span><span class="p">(</span><span class="n">Comment</span> <span class="n">comment</span><span class="p">);</span>
<span class="p">}</span>

<span class="k">public</span> <span class="k">class</span> <span class="nc">BlogService</span>
<span class="p">{</span>
    <span class="k">private</span> <span class="n">IRepository</span> <span class="n">_repository</span><span class="p">;</span>

    <span class="k">public</span> <span class="nf">BlogService</span><span class="p">(</span><span class="n">IRepository</span> <span class="n">repository</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_repository</span> <span class="p">=</span> <span class="n">repository</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">ProcessComment</span><span class="p">(</span><span class="n">Comment</span> <span class="n">comment</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="nf">_repository</span><span class="p">().</span><span class="nf">Save</span><span class="p">(</span><span class="n">comment</span><span class="p">);</span>

        <span class="c1">// Send notification to author and do other stuff here</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>To make the <code>Repository</code> depend on an abstraction and make it useful for the <code>BlogService</code>, it simply needs to implement the <code>IRepository</code> interface.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">class</span> <span class="nc">Repository</span> <span class="p">:</span> <span class="n">IRepository</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">void</span> <span class="nf">Save</span><span class="p">(</span><span class="n">Comment</span> <span class="n">comment</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// Save the comment</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>The dependency between <code>BlogService</code> and <code>Repository</code> is now removed and both depends upon an abstraction; the <code>IRepository</code>. Both classes can easily be changed, and even replaced without affecting each other.</p>

<p><img src="/blog/images/dependency-inversion-02.png" alt="Image02" /></p>

<h2 id="who-owns-the-abstraction">Who “owns” the abstraction?</h2>

<p>Let’s say the <code>BlogService</code> belongs in the Domain or Business Layer and the <code>Repository</code> belongs in the Persistence or Data Access Layer. Before we introduced DI this made the Domain Layer depend upon the Persistence Layer. To invert this dependency we should make the Persistence Layer dependent on the Domain Layer. Therefore the <code>IRepository</code> belongs in the Domain Layer.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Installing RSpec TextMate Bundle]]></title>
    <link href="https://thedersen.com/blog/2010/04/02/installing-rspec-textmate-bundle/"/>
    <updated>2010-04-02T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/04/02/installing-rspec-textmate-bundle</id>
    <content type="html"><![CDATA[<p>For future reference, this is how to install and update the RSpec TextMate Bundle. The bundle can be found in the <a href="http://github.com/dchelimsky/rspec-tmbundle">RSpec Git repository</a>.</p>

<p>To install, open a terminal window and type:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
</pre></td><td class="code"><pre><span class="nv">$ </span><span class="nb">cd</span> /Applications/TextMate.app/Contents/SharedSupport/Bundles
<span class="nv">$ </span>git clone git://github.com/dchelimsky/rspec-tmbundle.git RSpec.tmbundle
</pre></td></tr></tbody></table></code></pre></figure>

<p>The output should be something like:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="code"><pre>remote: Counting objects: 46507, <span class="k">done</span><span class="nb">.</span>
remote: Compressing objects: 100% <span class="o">(</span>10783/10783<span class="o">)</span>, <span class="k">done</span><span class="nb">.</span>
remote: Total 46507 <span class="o">(</span>delta 33212<span class="o">)</span>, reused 46206 <span class="o">(</span>delta 33056<span class="o">)</span>
Receiving objects: 100% <span class="o">(</span>46507/46507<span class="o">)</span>, 5.94 MiB | 882 KiB/s, <span class="k">done</span><span class="nb">.</span>
Resolving deltas: 100% <span class="o">(</span>33212/33212<span class="o">)</span>, <span class="k">done</span>.
</pre></td></tr></tbody></table></code></pre></figure>

<p>Now, reload your bundles in TextMate from the ‘Bundles &gt; Bundle Editor &gt; Reload Bundles’ menu.</p>

<p>To update, simply open a terminal window and type:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
</pre></td><td class="code"><pre><span class="nv">$ </span><span class="nb">cd</span> /Applications/TextMate.app/Contents/SharedSupport/Bundles/RSpec.tmbundle
<span class="nv">$ </span>git pull
</pre></td></tr></tbody></table></code></pre></figure>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Principles and Practices]]></title>
    <link href="https://thedersen.com/blog/2010/02/20/principles-and-practices/"/>
    <updated>2010-02-20T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/02/20/principles-and-practices</id>
    <content type="html"><![CDATA[<p>Here’s a random list with some principles and practices that I think every developer should be familiar with. Please add a comment with the ones I have forgotten.</p>

<h2 id="principles">Principles:</h2>

<ul>
  <li><a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY – Don’t Repeat Yourself</a></li>
  <li><a href="http://en.wikipedia.org/wiki/KISS_principle">KISS – Keep It Simple and Stupid</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/magazine/dd419655.aspx">Convention over Configuration</a></li>
  <li><a href="http://www.lostechies.com/blogs/chad_myers/archive/2010/02/12/composition-versus-inheritance.aspx">Composition over Inheritance</a></li>
  <li><a href="http://www.pragprog.com/articles/tell-dont-ask">Tell Don’t Ask</a></li>
  <li><a href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a></li>
  <li><a href="http://en.wikipedia.org/wiki/Command-query_separation">CQS - Command/Query Separation</a></li>
  <li><a href="http://www.lostechies.com/blogs/chad_myers/archive/2008/03/07/pablo-s-topic-of-the-month-march-solid-principles.aspx">S.O.L.I.D</a></li>
</ul>

<h2 id="practices">Practices:</h2>

<ul>
  <li><a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD - Test Driven Development</a> (<a href="http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd">The Three Laws of  TDD</a>)</li>
  <li><a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">BDD - Behavior Driven Development</a></li>
  <li><a href="http://en.wikipedia.org/wiki/Code_refactoring">Code Refactoring</a></li>
  <li><a href="http://en.wikipedia.org/wiki/Pair_programming">Pair Programming</a></li>
  <li><a href="http://agilesoftwaredevelopment.com/xp/practices/sit-together">Sit Together</a></li>
  <li><a href="http://agileinaflash.blogspot.com/2009/02/collective-code-ownership.html">Collective Code Ownership</a></li>
  <li><a href="http://en.wikipedia.org/wiki/Continuous_integration">Continuous Integration</a></li>
  <li><a href="http://en.wikipedia.org/wiki/Code_review">Code Review</a></li>
</ul>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Properties are an anti-pattern]]></title>
    <link href="https://thedersen.com/blog/2010/01/20/properties-are-an-anti-pattern/"/>
    <updated>2010-01-20T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/01/20/properties-are-an-anti-pattern</id>
    <content type="html"><![CDATA[<p>A while back I attended a course with <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a> focusing on <a href="http://domaindrivendesign.org/">Domain Driven Design</a> and command and query separation (CQRS). Here Greg was talking about how properties on your entities can be an <a href="http://en.wikipedia.org/wiki/Anti-pattern">anti-pattern</a>. This got me thinking, and I must say that I agree, not only in a DDD context, but as good object oriented design principle.</p>

<p>If you are using the same entities for both querying and updating (commands), you can’t get rid of the properties all together, but you can limit their use to querying by making them read-only.</p>

<p>Let me explain by giving an example.</p>

<h2 id="model-behavior-not-state">Model behavior not state</h2>

<p>Take a prescription object with one property, <code>CeasedAt</code>. (Ceasing a prescription means aborting the treatment.) To cease a prescription we simply set <code>DateTime.Now</code> on this property. Fine, that works.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">class</span> <span class="nc">Prescription</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="n">DateTime</span><span class="p">?</span> <span class="n">CeasedAt</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>Then of course the requirements changes. We now have to specify a reason why the prescription is ceased. We add a new property to the prescription object, <code>CeasedBecauseOf</code>.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">class</span> <span class="nc">Prescription</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="n">DateTime</span><span class="p">?</span> <span class="n">CeasedAt</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
    <span class="k">public</span> <span class="kt">string</span> <span class="n">CeasedBecauseOf</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>Fine, but how can we ensure that this is set every time we set the <code>CeasedAt</code> property? Well, the problem is that we can’t. All the developers must know this when using this class, and let’s face it, there is a very small chance of that happening, at least over a period of time. Of course we can add a comment describing the requirement, but I don’t like comments. Not everybody reads them, and they tend to get outdated over time as the code changes and the comments doesn’t. I want the code to express it self.</p>

<p>So let’s try to express this requirement using nothing but code. We keep the <code>CeasedAt</code> property but make the setter private. Then we add a method, <code>Cease</code>.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">class</span> <span class="nc">Prescription</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="n">DateTime</span><span class="p">?</span> <span class="n">CeasedAt</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">private</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">Cease</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">CeasedAt</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span>
            <span class="k">throw</span> <span class="k">new</span> <span class="nf">InvalidOperationException</span><span class="p">(</span><span class="s">"Prescription is already ceased."</span><span class="p">);</span>

        <span class="n">CeasedAt</span> <span class="p">=</span> <span class="n">DateTime</span><span class="p">.</span><span class="n">Now</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>Much better! Not only is the code now both easier to understand as well as use, but it’s also easier to change since we have encapsulated how we cease a prescription. We are also protected against somebody somewhere accidentally setting <code>CeasedAt</code> on a already ceased prescription.</p>

<p>When requirement changes we simply add reason as a parameter to the <code>Cease</code> method.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">class</span> <span class="nc">Prescription</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="n">DateTime</span><span class="p">?</span> <span class="n">CeasedAt</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">private</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
    <span class="k">public</span> <span class="kt">string</span> <span class="n">CeasedBecauseOf</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">private</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">Cease</span><span class="p">(</span><span class="kt">string</span> <span class="n">reason</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">CeasedAt</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span>
            <span class="k">throw</span> <span class="k">new</span> <span class="nf">InvalidOperationException</span><span class="p">(</span><span class="s">"Prescription is already ceased."</span><span class="p">);</span>

        <span class="k">if</span> <span class="p">(</span><span class="kt">string</span><span class="p">.</span><span class="nf">IsNullOrEmpty</span><span class="p">(</span><span class="n">reason</span><span class="p">))</span>
            <span class="k">throw</span> <span class="k">new</span> <span class="nf">ArgumentNullException</span><span class="p">(</span><span class="s">"reason"</span><span class="p">);</span>

        <span class="n">CeasedAt</span> <span class="p">=</span> <span class="n">DateTime</span><span class="p">.</span><span class="n">Now</span><span class="p">;</span>
        <span class="n">CeasedBecauseOf</span> <span class="p">=</span> <span class="n">reason</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>This will ensure at compile time that the requirement is fulfilled. The code also expresses the requirements more clearly. When we cease a prescription we have to specify a reason. In the previous example we could not know that from just reading the code, and therefore we were running a risk of introducing a bug if not all developers on our team was familiar with this requirement.</p>

<h2 id="dont-repeat-yourself">Don’t Repeat Yourself</h2>

<p>One other problem with the first example where we weren’t encapsulating how to cease a prescription is that we violate the <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY (Don’t Repeat Yourself) principle</a>. Let’s say that we can cease a prescription ten different places in our program, we then have ten places where we actually specify how to cease a prescription: by setting a property. I can almost guaranty you that this will lead to a future bug report, or at the very least, fragile and hard to change code.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Passive View the way we do it]]></title>
    <link href="https://thedersen.com/blog/2010/01/15/passive-view-the-way-we-do-it/"/>
    <updated>2010-01-15T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/01/15/passive-view-the-way-we-do-it</id>
    <content type="html"><![CDATA[<p>At <a href="http://infodoc.no/">work</a> we do a lot of Windows Forms development. To ease unit testing and apply better <a href="http://en.wikipedia.org/wiki/Separation_of_concerns">separation of concerns</a> our UI is built using the <a href="http://martinfowler.com/eaaDev/PassiveScreen.html">Passive View</a> pattern, a variant of the Model View Presenter pattern. Using this pattern we can test the presenter in total isolation from the UI by <a href="http://en.wikipedia.org/wiki/Mock_object">mocking</a> the view. Since the view contains no logic we run a very little risk by not testing it.</p>

<p>We do a presenter first approach. This means that the presenter is created/loaded first, then it is asked to give you a UI that can be shown on the screen.</p>

<h2 id="the-presenter">The Presenter</h2>

<p>The interface implemented by all presenters looks something like this:</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">interface</span> <span class="nc">IPresenter</span>
<span class="p">{</span>
    <span class="k">void</span> <span class="nf">Initialize</span><span class="p">();</span>
    <span class="kt">object</span> <span class="n">UI</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>The initialize method is called whenever the presenter is constructed. The <code>UI</code> property returns the view. The reason that it is of type object, is that then there is no need to reference <code>System.Windows.Forms</code> in the presenter library. This is not crucial as long as you keep UI elements out of the presenters since they are not very unit test friendly.</p>

<p>Instead of the presenters implementing <code>IPresenter</code> directly, we create a specific interface that inherits <code>IPresenter</code> for each presenter. This interface is usually empty.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">interface</span> <span class="nc">IMyPresenter</span> <span class="p">:</span> <span class="n">IPresenter</span>
<span class="p">{</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>In addition, the presenter must implement a callback interface. The view knows nothing about the presenter itself. Instead it gets a reference to this interface which contains one method for each operation that can be performed on the view plus one for each event that can be raised by a control in the view. Needless to say, we don’t create a method for all events, only the ones we need to respond on.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">interface</span> <span class="nc">IMyPresenterCallbacks</span>
<span class="p">{</span>
    <span class="k">void</span> <span class="nf">OnSave</span><span class="p">();</span>
    <span class="k">void</span> <span class="nf">OnMyTextChanged</span><span class="p">();</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>As you may have noticed, both methods on this interface is declared without any parameters and returns void. This is important, otherwise presentation logic might leaking into the view. The presenter always asks the view when it needs information, the view never tells the presenter anything, thus parameter less. Instead of the presenter returning something to the view, it explicitly sets a property or several on the view, thus void. This is actually what makes the view passive – the presenter is doing all the work.</p>

<p>Enough abstractions, let’s move on to the actual implementation of the presenter.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">class</span> <span class="nc">MyPresenter</span> <span class="p">:</span> <span class="n">IMyPresenter</span><span class="p">,</span> <span class="n">IMyPresenterCallbacks</span>
<span class="p">{</span>
    <span class="k">private</span> <span class="n">IMyView</span> <span class="n">_view</span><span class="p">;</span>

    <span class="k">public</span> <span class="nf">MyPresenter</span><span class="p">(</span><span class="n">IMyView</span> <span class="n">view</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_view</span> <span class="p">=</span> <span class="n">view</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="kt">object</span> <span class="n">UI</span>
    <span class="p">{</span>
        <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_view</span><span class="p">;</span> <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">Initialize</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="n">_view</span><span class="p">.</span><span class="nf">Attach</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
        <span class="n">_view</span><span class="p">.</span><span class="n">SaveButtonText</span> <span class="p">=</span> <span class="s">"Save"</span><span class="p">;</span>
        <span class="n">_view</span><span class="p">.</span><span class="n">SaveButtonEnabled</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">OnSave</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// Save _view.MyText</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">OnMyTextChanged</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="n">_view</span><span class="p">.</span><span class="n">SaveButtonEnabled</span> <span class="p">=</span> <span class="p">!</span><span class="kt">string</span><span class="p">.</span><span class="nf">IsNullOrEmpty</span><span class="p">(</span><span class="n">_view</span><span class="p">.</span><span class="n">MyText</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>Nothing magic going on here. As you can see the presenter implements both the <code>IMyPresenter</code> interface as well as the <code>IMyPresenterCallbacks</code> and it gets an instance of an <code>IMyView</code> injected in the constructor, which again can be retrieved form the UI property.</p>

<p>Attaching the presenter to the view so that they can communicate with each other is done on line 17 in the initialize method. Here we also set the default or initial state of the view.</p>

<p>The callback methods doesn’t do much in this example other then enabling/disabling the save button when the text is changed. Just notice that the presenter asks the view for the <code>MyText</code> property, and then tells the view to enable/disable its save button based on whether or not the <code>MyText</code> property is empty. Since this logic is moved from the view into the presenter, we can actually have unit tests that verify this behavior. Cool, or what?</p>

<p>So, how does the callback methods gets invoked? To find out we need to take a look at how the view is implemented.</p>

<h2 id="the-view">The View</h2>

<p>The base interface for all view looks like this:</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">interface</span> <span class="nc">IView</span><span class="p">&lt;</span><span class="n">TCallbacks</span><span class="p">&gt;</span>
<span class="p">{</span>
    <span class="k">void</span> <span class="nf">Attach</span><span class="p">(</span><span class="n">TCallbacks</span> <span class="n">presenter</span><span class="p">);</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>A simple generic interface with one method used by the presenter to attach itself. The <code>TCallbacks</code> type parameter is the type of the callbacks interface that the presenter implements. In this example; <code>IMyPresenterCallbacks</code>.</p>

<p>As you saw, <code>MyPresenter</code> got an instance of an <code>IMyView</code>. This is the interface specific to one view and it contains properties for all input fields such as text boxes, check boxes, drop downs etc., labels and state properties of the controls.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">interface</span> <span class="nc">IMyView</span> <span class="p">:</span> <span class="n">IView</span><span class="p">&lt;</span><span class="n">IMyPresenterCallbacks</span><span class="p">&gt;</span>
<span class="p">{</span>
    <span class="kt">string</span> <span class="n">MyText</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
    <span class="kt">string</span> <span class="n">SaveButtonText</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
    <span class="kt">bool</span> <span class="n">SaveButtonEnabled</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>Finally, we’ll look at how the view is implemented.</p>

<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre><span class="k">public</span> <span class="k">partial</span> <span class="k">class</span> <span class="nc">MyView</span> <span class="p">:</span> <span class="n">Form</span><span class="p">,</span> <span class="n">IMyView</span>
<span class="p">{</span>
    <span class="k">private</span> <span class="n">Button</span> <span class="n">_saveButton</span><span class="p">;</span>
    <span class="k">private</span> <span class="n">TextBox</span> <span class="n">_myTextBox</span><span class="p">;</span>

	<span class="k">public</span> <span class="nf">MyView</span><span class="p">()</span>
	<span class="p">{</span>
		<span class="nf">InitializeComponent</span><span class="p">();</span>
	<span class="p">}</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">Attach</span><span class="p">(</span><span class="n">IMyPresenterCallbacks</span> <span class="n">callback</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_saveButton</span><span class="p">.</span><span class="n">Click</span> <span class="p">+=</span> <span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">callback</span><span class="p">.</span><span class="nf">OnSave</span><span class="p">();</span>
        <span class="n">_myTextBox</span><span class="p">.</span><span class="n">TextChanged</span> <span class="p">+=</span> <span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">callback</span><span class="p">.</span><span class="nf">OnMyTextChanged</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="kt">string</span> <span class="n">MyText</span>
    <span class="p">{</span>
        <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_myTextBox</span><span class="p">.</span><span class="n">Text</span><span class="p">;</span> <span class="p">}</span>
        <span class="k">set</span> <span class="p">{</span> <span class="n">_myTextBox</span><span class="p">.</span><span class="n">Text</span> <span class="p">=</span> <span class="k">value</span><span class="p">;</span> <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="kt">string</span> <span class="n">SaveButtonText</span>
    <span class="p">{</span>
        <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_saveButton</span><span class="p">.</span><span class="n">Text</span><span class="p">;</span> <span class="p">}</span>
        <span class="k">set</span> <span class="p">{</span> <span class="n">_saveButton</span><span class="p">.</span><span class="n">Text</span> <span class="p">=</span> <span class="k">value</span><span class="p">;</span> <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="kt">bool</span> <span class="n">SaveButtonEnabled</span>
    <span class="p">{</span>
        <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_saveButton</span><span class="p">.</span><span class="n">Enabled</span><span class="p">;</span> <span class="p">}</span>
        <span class="k">set</span> <span class="p">{</span> <span class="n">_saveButton</span><span class="p">.</span><span class="n">Enabled</span> <span class="p">=</span> <span class="k">value</span><span class="p">;</span> <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>This class is a simple Windows Forms form or control implementing the <code>IMyView</code> interface. The properties on the interface is basically just wrapping the properties of the controls, and does not do anything else. Remember that any logic is in the presenter.</p>

<p>Attaching the presenter to the view enabling the presenter to react on events generated by the view is done in the attach method, which you saw the presenter called when it was initialized. On line 8 the Click event of the save button is attached to the <code>OnSave</code> method on the presenters callback interface and on line 9 the <code>TextChanged</code> event of the text box is attached to the <code>OnMyTextChanged</code> method. Here we do this using a <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx">lambda expression</a>, keeping things nice and simple I think, but it can of course be done using normal event/delegate syntax. When the events are hooked up, the callback methods on the presenter gets called whenever one of these events is raised. And that’s about it.</p>

<h2 id="summary">Summary</h2>

<p>At first this might look a bit overwhelming. Creating three interfaces, one presenter and one view just to get something to show up on the screen can seem costly, but I think the benefits outweighs the cost. After all it does not take many minutes creating these interfaces and classes.</p>

<p>First of all using this approach it makes almost everything testable. There is not much logic left in the view that are not tested. Secondly, since it introduces separation between the view and the presentation logic they can be developed separately by different developers and/or designers as long as the contracts (<code>IMyView</code> and <code>IMyPresenterCallbacks</code>) are implemented first.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Best of 2009]]></title>
    <link href="https://thedersen.com/blog/2010/01/08/best-of-2009/"/>
    <updated>2010-01-08T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/01/08/best-of-2009</id>
    <content type="html"><![CDATA[<p>Perhaps a bit late, but anyway, here are ten of the best albums 2009 brought us. It was a good year. You can order them as you like, except for the first. It stays where it belongs.</p>

<ul>
  <li>The Antlers - Hospice</li>
  <li>Sunset Rubdown - Dragonslayer</li>
  <li>Grizzly Bear - Veckatimest</li>
  <li>Animal Collective - Merriweather Post Pavilion</li>
  <li>The Flaming Lips - Embryonic</li>
  <li>Girls - Album</li>
  <li>Cymbals Eat Guitars - Why There Are Mountains</li>
  <li>Dinosaur Jr. - Farm</li>
  <li>Baroness - Blue Record</li>
  <li>Thåström - Kärlek Är För Dom</li>
</ul>

<p>We’ve also entered a new decade, which means that I should probably also make up my mind about the best album of the 2000’s. Off course I can’t. It stands between Arcade Fire - Funeral and Radiohead - Kid A. You decide.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[So, are you blogging now?]]></title>
    <link href="https://thedersen.com/blog/2010/01/07/so-are-you-blogging-now/"/>
    <updated>2010-01-07T00:00:00+00:00</updated>
    <id>https://thedersen.com/blog/2010/01/07/so-are-you-blogging-now</id>
    <content type="html"><![CDATA[<p>If this is blogging, then I guess I am. But don’t get your hopes up, I make no promises whatsoever on either quantity nor quality. I just wanted to create a blog, so let’s see what the future brings.</p>
]]></content>
  </entry>
  
</feed>