<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tore Vestues blogs &#187; Frameworks</title>
	<atom:link href="http://tore.vestues.no/category/frameworks/feed/" rel="self" type="application/rss+xml" />
	<link>http://tore.vestues.no</link>
	<description>On a quest for the silver bullet..</description>
	<lastBuildDate>Sun, 19 Jun 2011 19:34:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Glue 0.9 &#8220;Magic&#8221; Released</title>
		<link>http://tore.vestues.no/2011/01/05/glue-0-9-magic-released/</link>
		<comments>http://tore.vestues.no/2011/01/05/glue-0-9-magic-released/#comments</comments>
		<pubDate>Wed, 05 Jan 2011 13:54:58 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Glue]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=147</guid>
		<description><![CDATA[
My feelings on magic 
(Feel free to skip this section if you couldn&#8217;t care less about my thoughts on automagic)
I am not the biggest fan of automagical behavior. For that reason, up until now, Glue has not automatically created new objects when you map. You have to instantiate the object to map to (and all [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://vestues.no/tore/wp-content/211330471_0b84c297a3_m.jpg" alt="http://www.flickr.com/photos/grrrl/211330471/" title="http://www.flickr.com/photos/grrrl/211330471/" width="240" height="240" class="size-full wp-image-152" align="left"/></p>
<p><strong>My feelings on magic</strong> </p>
<p>(Feel free to skip this section if you couldn&#8217;t care less about my thoughts on automagic)</p>
<p>I am not the biggest fan of automagical behavior. For that reason, up until now, Glue has not automatically created new objects when you map. You have to instantiate the object to map to (and all nested objects within that object). The reason for this is that I want the developer to explicitly decide when it comes to how objects are to be created. Personally I like to be in control, so that problems do not arise at inconvenient times (like at runtime in a production environment). </p>
<p>In addition, I come from the OO school. I believe that objects should be, yes, objects, and not just data containers (a typical data container is a class containing only properties, and little or no logic). I believe objects should be relevant, individual, and contain logic. This often means that constructing them is not something you let a framework do (unless it is a framework specialized for that kind of job). Because constructing them often means making decisions, like what dependencies these objects should have.</p>
<p><strong>The &#8220;General purpose&#8221; mapping framework</strong></p>
<p>One of the driving forces for creating Glue was to create a <em>general purpose</em> mapping framework. Meaning it should support different scenarios, not just the ones I liked the most. In addition, using Glue extensively myself, I have found that there are (quite a lot of) times when having Glue create objects for me are both convenient, and low risk.</p>
<p><strong>Automagical and Strict mode</strong></p>
<p>So, with version 0.9 of Glue, I introduce the &#8220;Automagical&#8221; mode where Glue tries to instantiate objects whenever necessary. In addition, the existing behavior (making you create all the objects) is now named &#8220;Strict&#8221; mode. Choosing between the modes is simply done by choosing the right overload of the Mapper.Map-method. Intellisense will guide you.</p>
<p><strong>Examples</strong> can be found <a href="http://glue.codeplex.com/wikipage?title=Strict%20and%20Automagical%20Modes">here</a></p>
<p>(<a href="http://glue.codeplex.com/">What is Glue?</a>)</p>
<p><a href="http://glue.codeplex.com/releases/view/58617">Download Glue 0.9 binaries here</a></p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2011/01/05/glue-0-9-magic-released/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Glue &amp; Nu: Glue is a RubyGem!</title>
		<link>http://tore.vestues.no/2010/08/02/glue-nu-glue-is-a-rubygem/</link>
		<comments>http://tore.vestues.no/2010/08/02/glue-nu-glue-is-a-rubygem/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 18:30:02 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Glue]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=118</guid>
		<description><![CDATA[ No doubt, Ruby has an excellent package management system: Gems. Simply beautiful.
Also no doubt, we have not had such excellent package management system in the .Net-sphere. So why not just make RubyGems work for .Net as well? That&#8217;s exactly what Dru Sellers have done with Nu.
This is a great idea, so I decided right [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://vestues.no/tore/wp-content/135751440_2f59574661_m.jpg" title="http://www.flickr.com/photos/afternoon_sunlight/135751440/" width="240" height="227" class="size-full wp-image-119" align="right" /> No doubt, Ruby has an excellent package management system: <a href="http://rubygems.org/">Gems</a>. Simply beautiful.</p>
<p>Also no doubt, we have not had such excellent package management system in the .Net-sphere. So why not just make RubyGems work for .Net as well? That&#8217;s exactly what <a href="http://codebetter.com/blogs/dru.sellers/default.aspx">Dru Sellers</a> have done with <a href="http://codebetter.com/blogs/dru.sellers/archive/2010/07/17/nu.aspx">Nu</a>.</p>
<p>This is a great idea, so I decided right away to make a Gem out of <a href="http://glue.codeplex.com">Glue</a>.</p>
<p> To use it, start your favorite windows shell and type:</p>
<p><code>gem install gluemapping</code></p>
<p>Then go to your project directory (if you&#8217;re not already there) and type</p>
<p><code>nu install gluemapping</code></p>
<p>That&#8217;s it! The newest Glue-assemblies will be put in the lib-folder in your project directory.</p>
<p><strong>Requirements:</strong></p>
<p>1. You will need to install ruby. Download the latest <a href="http://rubyinstaller.org/downloads/">rubyinstaller</a>, and install it.<br />
2. Install Nu:<br />
<code><br />
gem update --system<br />
gem install nu<br />
</code></p>
<p>You can find more information on Nu <a href="http://codebetter.com/blogs/dru.sellers/archive/2010/07/17/nu.aspx">here</a>.</p>
<p>The Gem is hosted at RubyGems, you can find information about it <a href="http://rubygems.org/gems/gluemapping">here</a>.</p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/08/02/glue-nu-glue-is-a-rubygem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reflection and performance</title>
		<link>http://tore.vestues.no/2010/07/01/reflection-and-performance/</link>
		<comments>http://tore.vestues.no/2010/07/01/reflection-and-performance/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 11:07:45 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=85</guid>
		<description><![CDATA[The open source framework Glue that I&#8217;ve created, use a lot of reflection to invoke members on objects. This has sparked my interest in exploring the use of reflection in respect to performance. A colleague of mine, Åsmund Eldhuset, and I set aside an evening for a geeknight to get to the bottom of this, [...]]]></description>
			<content:encoded><![CDATA[<p>The open source framework <a href="http://glue.codeplex.com">Glue</a> that I&#8217;ve created, use a lot of reflection to invoke members on objects. This has sparked my interest in exploring the use of reflection in respect to performance. A colleague of mine, <a href="http://no.linkedin.com/in/aasmundeldhuset">Åsmund Eldhuset</a>, and I set aside an evening for a geeknight to get to the bottom of this, and this is what we found out.</p>
<p>There are many ways to use reflection, and the performance on them vary a lot. What we did was to try out every method and timing them to measure performance. These are the methods we tested: Normal property access, normal reflection, compiling and invoking a compiled expression, using Emit to create, compile and run the statement.</p>
<p><strong>Normal property access</strong></p>
<p>To compare the other methods, we measured the performance on accessing a property the normal way:</p>

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

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

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

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

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

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

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

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

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

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

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

<p>As we can see, this is also horribly slow.</p>
<p><strong>Conclusion</strong></p>
<p>Unless you can create an expression/emit statement and compile it and then run the compiled version more than 200 times, in the app&#8217;s lifetime, using normal reflection is by far the preferred way to go when you have to dynamically invoke members on objects.</p>
<p>- Tore Vestues</p>
<p>Thanks to <a href="http://no.linkedin.com/in/aasmundeldhuset">Åsmund Eldhuset</a>.</p>
<p><a href='http://tore.vestues.no/2010/07/01/reflection-and-performance/performancerunner/' rel='attachment wp-att-104'>Here&#8217;s the source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/07/01/reflection-and-performance/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How I use the Glue mapping framework</title>
		<link>http://tore.vestues.no/2009/09/02/how-i-use-the-glue-mapping-framework/</link>
		<comments>http://tore.vestues.no/2009/09/02/how-i-use-the-glue-mapping-framework/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 11:33:33 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Glue]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/09/02/how-i-use-the-glue-mapping-framework/</guid>
		<description><![CDATA[Glue is a general purpose, bidirectional automatic mapping framework for the .Net platform, with strong verification and testing tools.
I&#8217;m actively developing it. I&#8217;m also actively using it. In this post I&#8217;ll share my experience as a user of Glue, and how easy it is to set up the mappings using TDD.
The example
We want to create [...]]]></description>
			<content:encoded><![CDATA[<p>Glue is a general purpose, bidirectional automatic mapping framework for the .Net platform, with strong verification and testing tools.</p>
<p>I&#8217;m actively developing it. I&#8217;m also actively using it. In this post I&#8217;ll share my experience as a user of Glue, and how easy it is to set up the mappings using TDD.</p>
<p><strong>The example</strong></p>
<p>We want to create a mapping between DomainPerson and GuiPerson, listed here.</p>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<p><img src='http://vestues.no/tore/wp-content/glue1.jpg' alt='Glue' style='float:right'/>Running the tests, they both are green, and now we know that we have a working mapping, which also are protected from failing on future changes.</p>
<p>Note that this example only tests the mapping and relations towards GuiPerson, in a bidirectional mapping scenario I advice to write the same two tests towards DomainPerson.</p>
<p>This is how I do it, and it works great for me. Hope you liked it. To learn more about Glue, and to download it, go to <a href="http://glue.codeplex.com">the Glue homepage at Codeplex</a></p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/09/02/how-i-use-the-glue-mapping-framework/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Glue &#8211; the new mapping framework</title>
		<link>http://tore.vestues.no/2009/08/10/glue-the-new-mapping-framework/</link>
		<comments>http://tore.vestues.no/2009/08/10/glue-the-new-mapping-framework/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 09:57:36 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Glue]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/08/10/glue-the-new-mapping-framework/</guid>
		<description><![CDATA[I&#8217;ve spent this summer implementing a new mapping framework for the .Net plattform: Glue.
You&#8217;ll find examples and code here: http://glue.codeplex.com
Glue is a general purpose, bidirectional automatic mapping for the .Net platform, with strong verification and testing tools.
I&#8217;ve seen quite a lot of less than optimal handling of mapping issues in quite a few projects over [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent this summer implementing a new mapping framework for the .Net plattform: Glue.</p>
<p><a href="http://glue.codeplex.com">You&#8217;ll find examples and code here: http://glue.codeplex.com</a></p>
<p>Glue is a general purpose, bidirectional automatic mapping for the .Net platform, with strong verification and testing tools.</p>
<p>I&#8217;ve seen quite a lot of less than optimal handling of mapping issues in quite a few projects over the years. Again and again mapping seems to be just a task we have to do, and a bit too small for automating. It becomes clearer and clearer to me that mapping is both an important task in many projects, and it is always a repetitive and boring task. In addition far to often subtle annoying bugs tend to sneak into mapping code. This motivated me to create Glue. And this is what I need from a mapping framework:</p>
<p>a) a way to automate mapping, mostly because it is boring, and that makes it error prone,<br />
b) a way to automatically test the mapping, I&#8217;ve found that manual written test code for mapping is seldom done in a good way,<br />
c) a way to prevent future changes making my mappings obsolete. When people make changes, they rarely focus on mapping (and honestly they should not either). They should be told when they have to update the mapping.</p>
<p>Now, you could argue that there is already a mapping framework available on the .Net plattform. And when I started this work, I actually started with that framework, thinking I could just make a few extensions to fulfil my needs. I actually spent some time in the  source code to try to implement it, but sadly I realized that our needs differed too much. So, I created Glue, and these where the driving forces: </p>
<p><strong>General purpose</strong></p>
<p>Glue is a general purpose mapper. We realize that in the real world there are a lot of different solutions, and not all of them follow &#8220;the one true pattern&#8221;. In fact, mapping is often used to map to and from subsystems that are far from well designed. Subsystems we try to hide in a layer because we do not want it to leak into the other layers. Thus, we believe a mapper must support quite a few different scenarios. The goal is to promote good coding practices, but not to ignore the fact that there is a lot of legacy code out there that forces us to work a bit differently at times.</p>
<p><strong>Bidirectional</strong></p>
<p>I would say that in many mapping scenarios we need to map in both directions. First we get data from an object in a layer, and map it to an object in the layer above. When that layer is done with manipulating that object, we often want to map it back to down to the layer where it all came from. I have noticed not all mapping frameworks see it this way, and this was one of the reasons why I started working on Glue instead of trying to extend existing frameworks.</p>
<p><strong>Strong verification and testing tools</strong></p>
<p>I want to be absolutely certain that my mapping works. I also want to make sure that it is very hard to break it later. Manually writing tests for mapping is even more tedious than writing manual mapping. Glue automates this. Future changes has a sad reputation of breaking mapping code. Glue helps you detect this. Tools for helping the mapping is very important to me. And more tools will be available in future releases.</p>
<p><strong>Simplicity</strong></p>
<p>Mapping should be simple. Glue tries to simplify both the mapping process, and the verification and testing. You should not have to state the obvious, and Glue support relating properties automatically based on names.</p>
<p><strong>Explicitness</strong></p>
<p>Although Glue enables you to automate much of the mapping process, it also gives you the opportunity to be explicit about the mapping. So if you want to describe every relation in detail, you can. When it comes to understandable code this can be a good thing. Taking difficult to understand implicit mappings, and stating them explicitly can sometimes make things much easier to understand. </p>
<p><strong>Current version and the future</strong></p>
<p>The current version is 0.2.0 Alpha. It is still in Alpha because if we find good ways to improve the API, we do not want to lock ourselves to it just yet, before we get more feedback. I&#8217;m guessing the next release will be Beta.</p>
<p>I am currently using Glue on the project I am working on, and in about a month it will reach production. This somewhat guarantees that it will continue to evolve as our needs expand, and that we will find bugs sooner, and they will be fixed sooner.</p>
<p>Looking into the future we have some exciting ideas on tools to help with the mapping, and we are also working hard to make Glue as easy as possible to use, so expect simplifications. In addition we want Glue to serve a broad set of needs, so feedback is highly appreciated and if you explain your special needs, we might just implement it.</p>
<p><a href="http://glue.codeplex.com">You&#8217;ll find examples and code here: http://glue.codeplex.com</a></p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/08/10/glue-the-new-mapping-framework/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC: DefaultControllerFactory is not thread safe!</title>
		<link>http://tore.vestues.no/2009/07/03/aspnet-mvc-defaultcontrollerfactory-is-not-thread-safe/</link>
		<comments>http://tore.vestues.no/2009/07/03/aspnet-mvc-defaultcontrollerfactory-is-not-thread-safe/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 09:05:32 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/07/03/aspnet-mvc-defaultcontrollerfactory-is-not-thread-safe/</guid>
		<description><![CDATA[I am not sure if this is a bug or a &#8220;feature&#8221; of the ASP.NET MVC framework. Either way, this is something you should be aware of as it can cause some very hard to track concurrency issues which might leak information between your HttpRequests.
In my last project we used the ASP.NET MVC framework along [...]]]></description>
			<content:encoded><![CDATA[<p>I am not sure if this is a bug or a &#8220;feature&#8221; of the ASP.NET MVC framework. Either way, this is something you should be aware of as it can cause some very hard to track concurrency issues which might leak information between your HttpRequests.</p>
<p>In my last project we used the ASP.NET MVC framework along with StructureMap, which made us create our own controller factory. I just blogged about it <a href="http://tore.vestues.no/2009/06/28/aspnet-mvc-let-structuremap-create-your-controllers/">here</a>. </p>
<p>If it was a good design decision or not I am not completely certain anymore, but we added some more framework logic to the controller factory, including checking state on the HttpRequest-object. This, it turned out, was a path to trouble. After a while we had unexpected behavior in our system, sometimes it leaked information between HttpRequests it seemed! But since this only happened during stress testing, it was hard to find the cause.</p>
<p>What we found was that the RequestContext.HttpContext-object was not all the way thread safe. When a request hits our custom controller, and the request goes to sleep to serve another request, the HttpContext of the factory changes to the second requests HttpContext. This means that the first request will work on the second requests HttpContext when it wakes up. This is bad news if you are dependent on the HttpContext in your custom controller factory.</p>
<p>The lesson is this: you have to do at least one of the two actions below if you want to access the HttpContext in your custom controller factory.</p>
<p>1) </p>
<p>The problem occurs when you set your controller factory like this (normally in the global.asax file)</p>

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

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

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

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

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

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

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

<p><a href="http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId=3724">I just found out that this is a bug</a> </p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/07/03/aspnet-mvc-defaultcontrollerfactory-is-not-thread-safe/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC: Let StructureMap create your controllers</title>
		<link>http://tore.vestues.no/2009/06/28/aspnet-mvc-let-structuremap-create-your-controllers/</link>
		<comments>http://tore.vestues.no/2009/06/28/aspnet-mvc-let-structuremap-create-your-controllers/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 10:09:41 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/06/28/aspnet-mvc-let-structuremap-create-your-controllers/</guid>
		<description><![CDATA[For those of you already familiar with StructureMap and want to use it to configure your objects in ASP.NET MVC, read on.
The ASP.NET MVC framework has a default controller factory (DefaultControllerFactory) that requires all controllers to have a parameterless constructor. So, if you want to inject your dependencies to the constructor, you can&#8217;t do it. [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you already familiar with StructureMap and want to use it to configure your objects in ASP.NET MVC, read on.</p>
<p>The ASP.NET MVC framework has a default controller factory (DefaultControllerFactory) that requires all controllers to have a parameterless constructor. So, if you want to inject your dependencies to the constructor, you can&#8217;t do it. In addition, Microsoft suggests the following pattern to mock out the dependencies when testing:</p>
<p><strong>The Microsoft way of testing</strong></p>
<p>(Example from <a href="http://www.asp.net/learn/mvc/tutorial-31-cs.aspx">www.asp.net/mvc</a>)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//The Microsoft way...</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> GroupController <span style="color: #008000;">:</span> Controller
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> IContactManagerService _service<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> GroupController<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContactManagerService<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ModelStateWrapper<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ModelState</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> GroupController<span style="color: #000000;">&#40;</span>IContactManagerService service<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _service <span style="color: #008000;">=</span> service<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #008080; font-style: italic;">//...</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>In other words, create one constructor for the actual application, and one constructor for testing. It works, you get to test it, it&#8217;s not all that bad, but it just doesn&#8217;t sit well with me.</p>
<p><strong>Take control of your controllers!</strong></p>
<p>Don&#8217;t you like this either? Well, there is an easy way out of it. One of the really good things about ASP.NET MVC is that it is easy to configure. You can create your own controller factory and take control of the whole process. It might sound like a complex task, but it isn&#8217;t.</p>
<p>Let’s start with our controller, it has a dependency that is injected through the constructor:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HomeController <span style="color: #008000;">:</span> Controller
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> Speaker speaker<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> HomeController<span style="color: #000000;">&#40;</span>Speaker speaker<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">speaker</span> <span style="color: #008000;">=</span> speaker<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> ActionResult Index<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        ViewData.<span style="color: #0000FF;">Model</span> <span style="color: #008000;">=</span> speaker<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Running the website now, trying to reach this controller, will make the framework crash (&#8221;No parameterless constructor defined for this object.&#8221;). We need our own controller factory to make this work:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> StructureMapControllerFactory <span style="color: #008000;">:</span> DefaultControllerFactory
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IContainer container<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> StructureMapControllerFactory<span style="color: #000000;">&#40;</span>IContainer container<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">container</span> <span style="color: #008000;">=</span> container<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> IController GetControllerInstance<span style="color: #000000;">&#40;</span>Type controllerType<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>IController<span style="color: #000000;">&#41;</span>container.<span style="color: #0000FF;">GetInstance</span><span style="color: #000000;">&#40;</span>controllerType<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>This controller takes in a StructureMap.IContainer and uses it to create all the controllers. It inherits from the DefaultControllerFactory and overrides the GetControllerInstance-method that is used to create controllers.</p>
<p>Now all we have to do is to tell the ASP.NET MVC framework that it should use our controller factory. To do that simply add this line in the Application_Start() method in the Global.asax file:</p>

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

<p>(Notice that we do not need to configure StructureMap since our dependencies are so easy to resolve. In a real world app you will most probably need to configure the container)</p>
<p>There you go. Using StructureMap with the ASP.NET MVC framework is easy!</p>
<p><a href='http://vestues.no/tore/wp-content/structuremapcontrollerfactoryexample.zip'>Download the complete example here </a></p>
<p>-Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/06/28/aspnet-mvc-let-structuremap-create-your-controllers/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Why Microsoft should introduce compiler extensibility</title>
		<link>http://tore.vestues.no/2008/10/31/why-microsoft-should-introduce-compiler-extensibility/</link>
		<comments>http://tore.vestues.no/2008/10/31/why-microsoft-should-introduce-compiler-extensibility/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 21:54:22 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Boo]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/10/31/why-microsoft-should-introduce-compiler-extensibility/</guid>
		<description><![CDATA[At PDC08 I attended a presentation about “Contracts” (and Pex). “Contracts” is the result of the Spec# work. It is an attempt to enable Design by Contract (DbC) in .Net. Design by Contract is a concept popularized by Bertrand Meyer, and it is fully implemented and integrated in the language Eiffel. 
Microsoft decided that DbC [...]]]></description>
			<content:encoded><![CDATA[<p>At <a href="http://microsoftpdc.com/">PDC08</a> I attended a presentation about <a href="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/TL51.wmv">“Contracts” (and Pex)</a>. “Contracts” is the result of the Spec# work. It is an attempt to enable <a href="http://en.wikipedia.org/wiki/Design_by_contract">Design by Contract (DbC)</a> in .Net. Design by Contract is a concept popularized by <a href="http://en.wikipedia.org/wiki/Bertrand_Meyer">Bertrand Meyer</a>, and it is fully implemented and integrated in the language <a href="http://en.wikipedia.org/wiki/Eiffel_(programming_language)">Eiffel</a>. </p>
<p>Microsoft decided that DbC should not be a part of the languages themselves, but rather it should be offered as a standard library. I think this is unfortunate. If it had been a part of the language, the syntax could have been more declarative, and it probably would have given DbC more power. </p>
<p>The problem with having DbC as just another library is that you do not get any keywords, it is not allowed to be as declarative as it could have been, and the syntax gets unnecessary verbose. And besides, if it is just another library then this is really old news. There are several DbC libraries available on the .Net platform already. </p>
<p>But Contracts is actually a bit more than just another library. I believe the Contracts-team, not being allowed (I suspect) to make it a part of the languages, really wanted the power anyway. And so they got creative, and did a couple of interesting things.</p>
<p>First, they made tools as a plug-in to Visual Studio, and these tools are there to support the library. Second they actually added post compilation. In Contracts, you define all the Contracts-library code in the beginning of the method for readability (making it more declarative). But for this to execute correctly some of the code (the ensure-part), has to be at the end of the method. What Contracts&#8217; tools do is actually move that part of the code to the end of the method, after the code is compiled.</p>
<p>This is very, very interesting, and it raises the question: What is a library/framework? Normally it is just some code you can execute following the normal constraints of a language. What the Contracts-team has done is to extend the concept of what a library/framework is. They implicitly say a library/framework is both code and tools, meaning the tools can actually do some tweaking with the code after it has been written. In this case it is done for the sake of being declarative.</p>
<p>This means that when you create a library you get the power to add declarativeness to it, beyond the language. Now that is a very compelling concept! There are of course a couple of problems here. Creating these kinds of tools for your library is not a trivial task! Microsoft can do this because a) they&#8217;ve got the resources and b) because if all else fails, they can add support directly in the compiler, which they have already done on several occasions.</p>
<p>The pain here is of course, that Microsoft can do it, but you really can&#8217;t. It&#8217;s too much work, and you can forget about accessing the compiler. We as developers are not enabled to have this power.</p>
<p>I&#8217;ve been playing around with <a href="http://boo.codehaus.org/">Boo</a> for some time now, and I love the way Boo allows me to add keywords and to change things during compilation. My point is that Boo actually enables you to make frameworks that also incorporates tools the way the Contracts library does, and in addition it actually allows you to create keywords! And the best thing about it is that this is doable for all. It&#8217;s not very complicated. It really empowers us as developers to redefine the concept of frameworks.</p>
<p>If a framework can be functionality, tools and keywords then we enable us to write powerful, declarative, and less verbose code. Boo enables us. C#, sadly do not. </p>
<p><em>The very, very sad thing here is that during <a href="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/TL57.wmv">another session at PDC08</a>, Anders Hejlsberg &#8220;hinted strongly&#8221; that such compiler extensibility will never, ever be available in C#.</em></p>
<p>To demonstrate &#8220;Contracts&#8221;, this is how DbC is implemented:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> GetDescription<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> x<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
 Contract.<span style="color: #0000FF;">Requires</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&lt;</span>x<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 Contract.<span style="color: #0000FF;">Ensures</span><span style="color: #000000;">&#40;</span>Contract.<span style="color: #0000FF;">Result</span><span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008080; font-style: italic;">// Method body ...</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>You add a set of Requires and Ensures at the top of your method. The Requires-statements must be true at the beginning of the method, and the Ensures statements must be true at the end of the method. This means that the Ensures statements must be executed last. This is where Microsoft uses the post compilation to move that part of the code after compilation.</p>
<p>I have actually already made a little library for DbC in Boo, it&#8217;s very easy to do (probably only took me an hour or so to implement). This is how you say the same thing with my little library:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span>Require<span style="color: black;">&#40;</span>i<span style="color: #66cc66;">&lt;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span>Ensure<span style="color: black;">&#40;</span>result<span style="color: #66cc66;">!</span>=null<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> GetDescription<span style="color: black;">&#40;</span>x <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> String: 
	result <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #dc143c;">string</span>
	// Method body ...</pre></td></tr></table></div>

<p>It&#8217;s got all the functionality of Contracts, it&#8217;s just as declarative (or probably even more), and in my own opinion it&#8217;s less verbose. And do not forget that it took me about an hour to implement the whole library! This is enabling developers to be more productive!</p>
<p>Why Microsoft does not want to enable us with this is beyond me. But regardless of Microsoft, there is nothing stopping you from using Boo!</p>
<p>-Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/10/31/why-microsoft-should-introduce-compiler-extensibility/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/TL57.wmv" length="220868989" type="video/x-ms-wmv" />
<enclosure url="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/TL51.wmv" length="313739159" type="video/x-ms-wmv" />
		</item>
		<item>
		<title>Prolog within Java</title>
		<link>http://tore.vestues.no/2008/02/15/prolog-within-java/</link>
		<comments>http://tore.vestues.no/2008/02/15/prolog-within-java/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 07:32:44 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Learning]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/02/15/prolog-within-java/</guid>
		<description><![CDATA[Last night I attended a meeting at my local java user group. The theme of the presentation was actually Prolog, and a Prolog-framework for Java was presented.
I enjoy learning different ways to solve (the same) problems,  and I strongly believe it is important to do that. Seeing other ways to solve common problems will help expand [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I attended <a href="http://www4.java.no/web/show.do?page=42;7&amp;appmode=/showReply&amp;articleid=6108">a meeting at my local java user group</a>. The theme of the presentation was actually Prolog, and a <a href="https://jtrolog.dev.java.net/">Prolog-framework for Java </a>was presented.</p>
<p>I enjoy learning different ways to solve (the same) problems,  and I strongly believe it is important to do that. Seeing other ways to solve common problems will help expand your mind as to how you can solve problems yourself. That&#8217;s why i found this meeting so interesting too. Maybe I will not use Prolog so much myself, but I can still learn from it&#8217;s way of thinking, and enrich both my thinking and my code written in other languages.</p>
<p><strong>Prolog</strong> </p>
<p>Well, back to Prolog. I haven&#8217;t really been introduced to Prolog before, so I enjoyed the introduction we got. Three things strike me with Prolog:</p>
<p>1) How different it is from other mainstream programming languages.  Here we have a language that actually does not contain an &#8220;if&#8221;-statement, and it still solves problems. I find that fascinating in itself. It&#8217;s a completely different way to solve problems.</p>
<p>2) How declarative and explicit it is about the (domain) rules. Given a well defined domain I would love to express the rules for that domain in Prolog. It&#8217;s really beautiful.</p>
<p>3) It must be a nightmare to debug. When your ruleset and dataset starts to grow, it must be a nightmare to debug and even worse to try to make changes to it.</p>
<p><strong>Java and Prolog (<a href="https://jtrolog.dev.java.net/">jTrolog</a>)</strong></p>
<p>Given my observations about Prolog are &#8220;correct&#8221;, I believe Prolog can be a beautiful tool, but I think the domain must be small and specific. And given that many of us normally work on enterprise applications with not so small and well defined domains, maybe Prolog is not the thing.</p>
<p>But then again, what if we find smaller well defined sub-domains within those enterprise applications?</p>
<p>Enter<a href="https://jtrolog.dev.java.net/"> jTrolog </a>(or one of it&#8217;s equivalents). jTrolog let&#8217;s you define and execute Prolog in plain Java. What I see here is opportunity. Now we can still make our enterprise applications in Java, but all of a sudden we can introduce the elegance of Prolog for specific sub-domains.</p>
<p>When it comes to it, it&#8217;s ofcourse not just that easy to introduce Prolog in your next Java project. For instance, introducing a new language that probably no one else on the project understands well, will most often not be a smart thing to do. But such barriers are there to be overcome.</p>
<p>Expand the horizon!</p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/02/15/prolog-within-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Naked Objects</title>
		<link>http://tore.vestues.no/2007/09/16/naked-objects/</link>
		<comments>http://tore.vestues.no/2007/09/16/naked-objects/#comments</comments>
		<pubDate>Sun, 16 Sep 2007 11:12:15 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2007/09/16/naked-objects/</guid>
		<description><![CDATA[The other day I came across &#8220;Naked Objects&#8221;. Put short, naked objects is a pattern and a framework built on that pattern.  The goal is to remove the need to develop a gui-layer. The gui is automatically generated based on your domain-objects at run-time.
This has several advantages (and ofcourse some drawbacks), but what got me interested is [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I came across <a target="_blank" href="http://nakedobjects.org/wiki/Main_Page">&#8220;Naked Objects&#8221;</a>. Put short, naked objects is a pattern and a framework built on that pattern.  The goal is to remove the need to develop a gui-layer. The gui is automatically generated based on your domain-objects at run-time.</p>
<p>This has several advantages (and ofcourse some drawbacks), but what got me interested is that this will put focus back on modelling the domain objects. Like in real object oriented systems, this means that all the business logic is contained within the domain objects. I&#8217;ve seen more than enough systems where the business logic is entagled into the gui-layer, making these system highly coupled and less flexible.</p>
<p>I think Greg Vaughn has found the essence of Naked Objects when describing it like &#8220;..primarily a design and requirements gathering tool, and secondarily a UI framework&#8221;. TDD looks like a way of testing, but the real power of TDD is actually how it drives the design of the system. The same with Naked Objects. It&#8217;s not primarily a way of making gui fast. The point with naked objects, as with TDD, is that it is drives the design of the system, and gives us a more object oriented design. Have a look at his blog <a target="_blank" href="http://weblogs.java.net/blog/gvaughn/archive/2003/10/naked_objects_e.html">&#8220;Naked Objects exposed&#8221;</a>.  </p>
<p>I&#8217;ll do some experimentation with Naked Objects in the time to come, so stay tuned for more posts on the topic.</p>
<p>Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2007/09/16/naked-objects/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

