<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Tore Vestues blogs</title>
	
	<link>http://tore.vestues.no</link>
	<description>On a quest for the silver bullet..</description>
	<lastBuildDate>Thu, 19 Aug 2010 13:13:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ToreVestues" /><feedburner:info uri="torevestues" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Speaking at Javazone 2010</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/mwRSJEqTgLQ/</link>
		<comments>http://tore.vestues.no/2010/08/19/speaking-at-javazone-2010/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 13:13:26 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Talks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=141</guid>
		<description><![CDATA[I&#8217;m looking forward to be speaking at Javazone 2010. It&#8217;s been a few years since I attended Javazone, so I am really looking forward to the conference. The agenda looks great!
This time my talk will be in Norwegian.
My talk &#8211; Javazone 2010  &#8211; Agenda
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m looking forward to be speaking at Javazone 2010. It&#8217;s been a few years since I attended Javazone, so I am really looking forward to the conference. The agenda looks great!</p>
<p>This time my talk will be in Norwegian.</p>
<p><a href="http://javazone.no/incogito10/events/JavaZone%202010/sessions#508535d1-5601-4076-aa05-e1899741863f">My talk</a> &#8211; <a href="http://jz10.java.no/">Javazone 2010</a>  &#8211; <a href="http://javazone.no/incogito10/events/JavaZone%202010/sessions">Agenda</a></p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/mwRSJEqTgLQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/08/19/speaking-at-javazone-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2010/08/19/speaking-at-javazone-2010/</feedburner:origLink></item>
		<item>
		<title>Law of Demeter: A real-world example</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/Vm6QS2oO-Cg/</link>
		<comments>http://tore.vestues.no/2010/08/05/law-of-demeter-a-real-world-example/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 20:05:50 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding principles]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=128</guid>
		<description><![CDATA[ The Law of Demeter (LOD) helps decouple objects, it makes mocking intensive tests much easier to write, and it offers a concrete set of rules that can make things clearer.
I recently ran into an example in Glue where I refactored towards LOD. I will share my thoughts on that here.
The problem
I am working on [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://vestues.no/tore/wp-content/2416461325_00321d3ec6_m.jpg" alt="Demeter - http://www.flickr.com/photos/zaqarbal/2416461325/" align="left" title="Demeter - http://www.flickr.com/photos/zaqarbal/2416461325/" width="135" height="240" class="size-full wp-image-137" /> <a href="http://en.wikipedia.org/wiki/Law_of_Demeter">The Law of Demeter</a> (LOD) helps decouple objects, it makes mocking intensive tests much easier to write, and it offers a concrete set of rules that can make things clearer.</p>
<p>I recently ran into an example in <a href="http://glue.codeplex.com">Glue</a> where I refactored towards LOD. I will share my thoughts on that here.</p>
<p><strong>The problem</strong></p>
<p>I am working on a new feature in Glue. In order to make room for that feature, I had to do some refactoring, to make it smoother to add that feature. What I found during the refactoring was this line (in the <em>Mapper</em>-class):</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;">Relations</span>.<span style="color: #0000FF;">GetRelationsTowardsRight</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>I found it because I already had some trouble with the <em>Mapper</em> class&#8217; dependency on the <em>Mapping</em> class. I realized that I was exposing a lot of information, <em>Mapper</em> shouldn&#8217;t know about <em>Mapping</em>. This dependency made my refactoring job bigger. I strongly believe that objects should know as little as possible. </p>
<p>This is a good example of breaking LOD. I think breaking LOD isn&#8217;t always a big offense (Martin Fowler <a href="http://martinfowler.com/articles/mocksArentStubs.html">suggests</a> we rename it &#8220;<em>Suggestion</em> of Demeter&#8221;), and I don&#8217;t think doing it here is my largest mistake. But now a new feature had brought the need for some refactoring, and at this point applying LOD seemed like a good idea. So it&#8217;s not only a question of where to apply a pattern, but also when.</p>
<p><strong>The solution</strong></p>
<p>The <em>Mapping</em> class is a kind of <a href="http://en.wikipedia.org/wiki/Facade_pattern">facade</a> to the rest of the system. It is an attempt to make the API as simple as possible. That means it handles the creation of some objects. In our case this is how <em>Mapping</em> creates <em>Mapper</em> objects:</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;">var mapper <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mapper<span style="color: #008000;">&lt;</span>LeftType, RightType<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>(Notice how <em>Mapping</em> passes itself in to <em>Mapper</em>). <em>Mapper</em> gets what it needs by calling:</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;">Relations</span>.<span style="color: #0000FF;">GetRelationsTowardsRight</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p><em>Mapper</em> gets a <em>RelationContainer </em>(the Relations property) from <em>Mapping</em>.</p>
<p>This breaks LOD. There is a simple fix in this case. The code passes a <em>Mapping</em> object, while what the <em>Mapper</em> wants is a <em>RelationContainer</em>.  We should only pass the object it needs (in the <em>Mapping</em> class):</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;">var mapper <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mapper<span style="color: #008000;">&lt;</span>LeftType, RightType<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>Relations<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The <em>Mapper</em> now gets what it needs by calling:</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;">relations.<span style="color: #0000FF;">GetRelationsTowardsRight</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>It&#8217;s fixed!</p>
<p>This way we have lowered the coupling, making <em>Mapper</em> unaware of <em>Mapping</em>, meaning that now it can access only what it needs, not a whole lot more. For me it is important that objects know as little as possible, and now it knows a whole lot less. It doesn&#8217;t care (or know) where the <em>RelationContainer</em> comes from. That isn&#8217;t its concern, and shouldn&#8217;t be either.</p>
<p>(The Law of Demeter is a rather academic specification of who is allowed to call who, specified <a href="http://en.wikipedia.org/wiki/Law_of_Demeter#In_object-oriented_programming">here</a>. Basically what it boils down to is that you shouldn&#8217;t ask for other objects from an object, and do an action on that. Or in other words, you should just use one dot per line.)</p>
<p><strong>Not the &#8220;standard&#8221; solution</strong></p>
<p>This solution is not how one normally is advised to resolve the breaking of LOD. Normally you&#8217;re supposed to hide the inner object. In this case it would mean that <em>Mapper</em> should be unaware of the fact that <em>Mapping</em> contains a <em>RelationContainer</em>, it should just ask <em>Mapping</em> to get the relations (a concept called encapsulation, it&#8217;s pretty standard object oriented thinking). 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;">GetRelationsTowardsRight</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>That&#8217;s a smart solution, if you&#8217;ve got the right problem. That solution enables <em>Mapper</em> to only know about <em>Mapping</em>, and none of its collaborators (hiding the <em>RelationContainer</em> in this example). </p>
<p>But the problem we have here is slightly different. What I found here was that <em>Mapper</em> should not know about <em>Mapping</em> at all. <em>Mapper</em> should only concern itself with the relations, and in this case <em>Mapper</em> should only receive the <em>RelationContainer</em> to work with. So the problem was that we depended on the wrong object altogether, not that we had the right object, and had to know about its collaborator. A subtle, but important difference. It&#8217;s important to be aware of what your problem is in order to find the right solution. It&#8217;s not always as easy as choosing the &#8220;standard&#8221; solution.</p>
<p><strong>Reflections on Law of Demeter</strong></p>
<p>LOD is just a concrete set of rules that aims to help writing less coupled code. You can do this without even knowing about LOD. In our case, focusing on separation of concerns  can yield the same effects. As can sound object oriented thinking, like hiding as much as possible (not exposing things through properties or getters), focusing on knowing as little as possible, applying <a href="http://tore.vestues.no/2009/08/16/tell-dont-ask/">&#8220;Tell, don&#8217;t ask&#8221;</a>, and so on.</p>
<p>The problem with concrete rules like LOD is that it works great if you understand the intention, but if you do not, you&#8217;re probably going to apply it wrong. And if you understand the intention, often you can get the effects without even thinking about LOD. </p>
<p>For me, I like the intention of LOD , but I do not think about it that often, as I use other guidelines to help me yield the same result. On the other hand, knowing the LOD principle have helped me on several occasions, as in this example, to see clearly what is going on.</p>
<p>Also, if you need to justify a style of programming that is heavy on mocking, LOD is an often used argument.</p>
<p>This refactoring is checked in as <a href="http://glue.codeplex.com/SourceControl/changeset/changes/52023">changeset 52023</a> in <a href="http://glue.codeplex.com/">Glue</a>. Compare that and the previous changeset to see what was done.</p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/Vm6QS2oO-Cg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/08/05/law-of-demeter-a-real-world-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2010/08/05/law-of-demeter-a-real-world-example/</feedburner:origLink></item>
		<item>
		<title>Glue &amp; Nu: Glue is a RubyGem!</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/1rVMvjjhLTY/</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>

		<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>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/1rVMvjjhLTY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/08/02/glue-nu-glue-is-a-rubygem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2010/08/02/glue-nu-glue-is-a-rubygem/</feedburner:origLink></item>
		<item>
		<title>The Microsoft MVP Award</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/F4hXuAGJr_M/</link>
		<comments>http://tore.vestues.no/2010/07/26/the-microsoft-mvp-award/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 12:20:35 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Talks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=113</guid>
		<description><![CDATA[I am proud to announce that I have received the Microsoft MVP Award in C#, for my &#8220;exceptional contributions and commitment to technical communities worldwide&#8221;.
That means I am a C# MVP, and I am very proud of it!
I feel it is important for me to contribute to, and learn from, the technical communities around the [...]]]></description>
			<content:encoded><![CDATA[<p>I am proud to announce that I have received the Microsoft MVP Award in C#, for my &#8220;exceptional contributions and commitment to technical communities worldwide&#8221;.</p>
<p>That means I am a C# MVP, and I am very proud of it!</p>
<p>I feel it is important for me to contribute to, and learn from, the technical communities around the world, and therefore I&#8217;m honored by receiving this award for my contributions. I will do my best to continue to contribute to the communities in the time to come, and I know I will learn a lot from them.</p>
<p>- Tore Vestues</p>
<p><img src="http://vestues.no/tore/wp-content/MVP_Horizontal_FullColor_resized_500x.png" alt="MVP" title="MVP" width="500" height="202" class="size-full wp-image-114" /></p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/F4hXuAGJr_M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2010/07/26/the-microsoft-mvp-award/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2010/07/26/the-microsoft-mvp-award/</feedburner:origLink></item>
		<item>
		<title>Reflection and performance</title>
		<link>http://feedproxy.google.com/~r/ToreVestues/~3/16nOSCbRdHU/</link>
		<comments>http://tore.vestues.no/2010/07/01/reflection-and-performance/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 11:07:45 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/?p=85</guid>
		<description><![CDATA[The open source framework Glue that I&#8217;ve created, use a lot of reflection to invoke members on objects. This has sparked my interest in exploring the use of reflection in respect to performance. A colleague of mine, Åsmund Eldhuset, and I set aside an evening for a geeknight to get to the bottom of this, [...]]]></description>
			<content:encoded><![CDATA[<p>The open source framework <a href="http://glue.codeplex.com">Glue</a> that I&#8217;ve created, use a lot of reflection to invoke members on objects. This has sparked my interest in exploring the use of reflection in respect to performance. A colleague of mine, <a href="http://no.linkedin.com/in/aasmundeldhuset">Åsmund Eldhuset</a>, and I set aside an evening for a geeknight to get to the bottom of this, and this is what we found out.</p>
<p>There are many ways to use reflection, and the performance on them vary a lot. What we did was to try out every method and timing them to measure performance. These are the methods we tested: Normal property access, normal reflection, compiling and invoking a compiled expression, using Emit to create, compile and run the statement.</p>
<p><strong>Normal property access</strong></p>
<p>To compare the other methods, we measured the performance on accessing a property the normal way:</p>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<p><img src='http://vestues.no/tore/wp-content/glue1.jpg' alt='Glue' style='float:right'/>Running the tests, they both are green, and now we know that we have a working mapping, which also are protected from failing on future changes.</p>
<p>Note that this example only tests the mapping and relations towards GuiPerson, in a bidirectional mapping scenario I advice to write the same two tests towards DomainPerson.</p>
<p>This is how I do it, and it works great for me. Hope you liked it. To learn more about Glue, and to download it, go to <a href="http://glue.codeplex.com">the Glue homepage at Codeplex</a></p>
<p>- Tore Vestues</p>
<img src="http://feeds.feedburner.com/~r/ToreVestues/~4/tPzbOZFY2PM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/09/02/how-i-use-the-glue-mapping-framework/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://tore.vestues.no/2009/09/02/how-i-use-the-glue-mapping-framework/</feedburner:origLink></item>
	</channel>
</rss>
