<?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; Bits and bytes</title>
	<atom:link href="http://tore.vestues.no/category/bits-and-bytes/feed/" rel="self" type="application/rss+xml" />
	<link>http://tore.vestues.no</link>
	<description>On a quest for the silver bullet..</description>
	<lastBuildDate>Mon, 26 Jul 2010 12:22:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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>3</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>

		<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>3</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>

		<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>6</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>1</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>4</slash:comments>
		</item>
		<item>
		<title>Interviewing Clemens Vasters</title>
		<link>http://tore.vestues.no/2009/03/04/interviewing-clemens-vasters/</link>
		<comments>http://tore.vestues.no/2009/03/04/interviewing-clemens-vasters/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 12:09:53 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/03/04/interviewing-clemens-vasters/</guid>
		<description><![CDATA[In december Lars Wilhelmsen and I made an interview with Clemens Vasters. We talk about different topics around the new Azure and Cloud technologies from Microsoft. Clemens Vasters works as a Program Manager in the .NET Online Services team and is responsible for the &#8216;Service Bus&#8217; feature area of Microsoft&#8217;s upcoming Cloud Platform.
Børge Hansen from [...]]]></description>
			<content:encoded><![CDATA[<p>In december <a href="http://larswilhelmsen.com/">Lars Wilhelmsen</a> and I made an interview with Clemens Vasters. We talk about different topics around the new Azure and Cloud technologies from Microsoft. Clemens Vasters works as a Program Manager in the .NET Online Services team and is responsible for the &#8216;Service Bus&#8217; feature area of Microsoft&#8217;s upcoming Cloud Platform.</p>
<p><a href="http://borge3000.no/">Børge Hansen</a> from Microsoft is the man behind the camera.</p>
<p><iframe src="http://silverlight.services.live.com/invoke/31897/Interview%20with%20Clemens%20Vasters/iframe.html" scrolling="no" frameborder="0" style="width:500px; height:375px"></iframe></p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/03/04/interviewing-clemens-vasters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a dynamic xml reader with C# 4.0</title>
		<link>http://tore.vestues.no/2009/01/05/creating-a-dynamic-xml-reader-with-c-40/</link>
		<comments>http://tore.vestues.no/2009/01/05/creating-a-dynamic-xml-reader-with-c-40/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 21:28:49 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[Boo]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/01/05/creating-a-dynamic-xml-reader-with-c-40/</guid>
		<description><![CDATA[&#8220;The static type dynamic&#8221; was the catchphrase at PDC&#8217;08 when talking about what&#8217;s new in C# 4.0. The dynamic type seems to be introduced mainly to simplify the code you write when doing Com interop. But many also see this as a step towards the dynamic languages for C#. I like C#. I also like [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;The static type dynamic&#8221; was the catchphrase at PDC&#8217;08 when talking about what&#8217;s new in C# 4.0. The dynamic type seems to be introduced mainly to simplify the code you write when doing Com interop. But many also see this as a step towards the dynamic languages for C#. I like C#. I also like dynamic languages like python. In addition I am a big fan of the static language Boo that actually feels quite dynamic. This has given me a special interest in the dynamic type in C#.</p>
<p>One of the nice things (there are several others!) about dynamic languages like Python is that the code feels much cleaner. It is not bloated with type declarations all over. This clean feel was also the goal of the language Boo, and even though it is a static language, Boo really gives you that dynamic feel.</p>
<p>The question is the of course: Is the C# getting more dynamic?</p>
<p>Well to be honest I do not think so. Having to declare a type at all (even if the declaration is &#8220;dynamic&#8221;) feels quite static, and the code bloats like static languages. Most of the time. I think it is wrong to think that C# is a more dynamic language due to the dynamic keyword, and I feel you are not using the right tools if you try to use C# as a dynamic language. If you want to write dynamic code, use a dynamic language.</p>
<p>But when we get the dynamic keyword, lets find things to use it for. When I am introduced to new features I am not that interested in all the fancy things you can do with it, I want real value. And I think I have found an example of real value in the dynamic keyword. I&#8217;ll give you an example, other than in com-interop, where the dynamic keyword will help you keep your code nice and clean, where otherwise you&#8217;d have to write a whole lot of ugly code.</p>
<p><strong>The Quick Xml Reader</strong></p>
<p>The funny thing is that I have already implemented a nice xmlreader in Boo (using the IQuackFoo-interface). When the dynamic keyword was introduced in C# 4 and you&#8217;ve got the IDynamicObject from the DLR, I figured I can do the exact same thing in C#. That&#8217;s just cool!</p>
<p>So what did I do? Here&#8217;s the comment from the Boo code file:</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
25
26
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> XmlReader<span style="color: black;">&#40;</span>IQuackFu<span style="color: black;">&#41;</span>:
<span style="color: #483d8b;">&quot;&quot;&quot;
Represents an xmldocument using &quot;dynamic typing&quot;. 
Can be used to retrieve single values from the xml-document.
There is no support for retrieving lists.
&nbsp;
Language: 
 Element: Both nodes and leaves
 Node: an element containing sub-elements
 Leaf: An element that represents a value (in xml this is an attribute or a node with nodetype text)
&nbsp;
use method names to retrieve nodes, and properties to retrieve leaves
example:
&lt;myXml stat=&quot;ok&quot;&gt;
	&lt;user id=&quot;123&quot; nsid=&quot;123&quot;&gt;
	    &lt;username&gt;Tore&lt;/username&gt;
	    &lt;password&gt;foo&lt;/password&gt;
	&lt;/user&gt;
&lt;/myXml&gt;
&nbsp;
myXml as duck = XmlReader(xmlString)
myXml //will fail since &quot;myXml&quot; is no leaf
myXml() // will return a new XmlReaderrepresenting the rsp-node with children
myXml().stat // will return &quot;ok&quot;
myXml().user().username // will return &quot;Tore&quot;
&quot;&quot;&quot;</span></pre></td></tr></table></div>

<p>How cool is that? This is dynamic, resolved at run time, and it can now be done in C# as well. The point with parsing xml is that you won&#8217;t catch errors in the xml structure at compile time anyway. So why not just do the code dynamic?</p>
<p>So, our Xml reader let&#8217;s you easily get values from an xml structure without having to declare all the types and without having to worry about all the details of the xml-parsing that normally is a bit complex and frankly, boring. In addition the code is much more readable than normal xml-parsing code.</p>
<p><strong>Xml reading in C#</strong></p>
<p>So, how do we do this in C#? Our XmlReader is a class where you can use any method name or property name on it, and it will still compile. For example, This will compile:</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;">dynamic ourReader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlReader<span style="color: #000000;">&#40;</span>xmlString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ourReader.<span style="color: #0000FF;">WhateverMethodNameYouLike</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>To do this, the XmlReader needs to inherit the IDynamicObject, and it has to implement the method GetMetaObject that returns a MetaObject. </p>
<p><strong>Simplifying the MetaObject</strong></p>
<p>To implement a MetaObject and do some useful stuff you actually have to understand how to build a Linq expression tree that describes the action to be taken. If people are going to use this, it should be as simple as possible. Building Linq expression trees is not as simple as possible IMHO. So, I&#8217;ll make a generic base class that hides the whole MetaObject with its Linq expression trees, and let you override some methods in your XmlReader class instead.</p>
<p>I will not go into details on how I have done this, but if you&#8217;re interested, you can look at the source (there&#8217;s a link at the end). The &#8216;magic&#8217; is within the MetaBaseDynamicObject. Together with the BaseDynamicObject these can be reused for different tasks that needs on the fly interpreting of property and method calls.</p>
<p>All <i>you</i> have to do is to inherit the BaseDynamicObject, and override any of these methods:</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> BaseDynamicObject<span style="color: #008000;">:</span>IDynamicObject
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// If a method on this object is called run time, this method will be called.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;action&quot;&gt;Information on the method that has been called&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;args&quot;&gt;parameters on the method call&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;The return value for the originally called method&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #FF0000;">Object</span> Call<span style="color: #000000;">&#40;</span>CallAction action, <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// If a property is set on this object run time, this method will be called.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;action&quot;&gt;Information on the property which value is attemted set&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;value&quot;&gt;the value that the property is attemted set with&lt;/param&gt;    </span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> SetMember<span style="color: #000000;">&#40;</span>SetMemberAction action, <span style="color: #FF0000;">object</span> value<span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// If a property is called on this object run time, this method will be called.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;action&quot;&gt;Information on the property which is being called&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;The return value for the property that has been called&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #FF0000;">Object</span> GetMember<span style="color: #000000;">&#40;</span>GetMemberAction action<span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">//, MetaObject[] args)</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Implementing the XmlReader</strong></p>
<p>So what we have to do here is to create a class (XmlReader), inherit the BaseDynamicObject, and override Call and GetMember. We will not bother with SetMember now, as we do not want to support updating the xml. This is what it looks like:</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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> XmlReader<span style="color: #008000;">:</span>BaseDynamicObject
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> XmlElement element<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> XmlReader<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> xmlText<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        var doc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlDocument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        doc.<span style="color: #0000FF;">LoadXml</span><span style="color: #000000;">&#40;</span>xmlText<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        element <span style="color: #008000;">=</span> doc.<span style="color: #0000FF;">DocumentElement</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> XmlReader<span style="color: #000000;">&#40;</span>XmlElement xmlelement<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        element <span style="color: #008000;">=</span> xmlelement<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> Call<span style="color: #000000;">&#40;</span>CallAction action, <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var elements <span style="color: #008000;">=</span> element.<span style="color: #0000FF;">SelectNodes</span><span style="color: #000000;">&#40;</span>action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>elements.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> 
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Element '&quot;</span> <span style="color: #008000;">+</span> action.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;' doesn't exist in xml&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>IsLeaf<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>XmlElement<span style="color: #000000;">&#41;</span>elements<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: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Element '&quot;</span> <span style="color: #008000;">+</span> action.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;' is a Leaf and cannot be fetched as a Node&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> XmlReader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>XmlElement<span style="color: #000000;">&#41;</span>elements<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: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> GetMember<span style="color: #000000;">&#40;</span>GetMemberAction action<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var elements <span style="color: #008000;">=</span> element.<span style="color: #0000FF;">SelectNodes</span><span style="color: #000000;">&#40;</span>action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>elements.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>IsLeaf<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>XmlElement<span style="color: #000000;">&#41;</span>elements<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: #0600FF;">return</span> elements<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">InnerText</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">else</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Element '&quot;</span><span style="color: #008000;">+</span>action.<span style="color: #0000FF;">Name</span><span style="color: #008000;">+</span><span style="color: #666666;">&quot;' is not a Leaf and cannot be fetched one&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        var attribute <span style="color: #008000;">=</span> element.<span style="color: #0000FF;">GetAttributeNode</span><span style="color: #000000;">&#40;</span>action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>attribute <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Element '&quot;</span><span style="color: #008000;">+</span>action.<span style="color: #0000FF;">Name</span><span style="color: #008000;">+</span><span style="color: #666666;">&quot;' doesn't exist in xml&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> attribute.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> IsLeaf<span style="color: #000000;">&#40;</span>XmlElement leafElement<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> leafElement.<span style="color: #0000FF;">ChildNodes</span>.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&amp;&amp;</span>
            leafElement.<span style="color: #0000FF;">ChildNodes</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">NodeType</span> <span style="color: #008000;">==</span> XmlNodeType.<span style="color: #0000FF;">Text</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 class now does all the xml-handling. So that means you can do stuff like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> xmlString <span style="color: #008000;">=</span> 
	<span style="color: #666666;">&quot;&lt;?xml version=<span style="color: #008080; font-weight: bold;">\&quot;</span>1.0<span style="color: #008080; font-weight: bold;">\&quot;</span> encoding=<span style="color: #008080; font-weight: bold;">\&quot;</span>utf-8<span style="color: #008080; font-weight: bold;">\&quot;</span> ?&gt;&quot;</span> <span style="color: #008000;">+</span>
	<span style="color: #666666;">&quot;&lt;myXml stat=<span style="color: #008080; font-weight: bold;">\&quot;</span>ok<span style="color: #008080; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #008000;">+</span>
		<span style="color: #666666;">&quot;&lt;user id=<span style="color: #008080; font-weight: bold;">\&quot;</span>321<span style="color: #008080; font-weight: bold;">\&quot;</span> nsid=<span style="color: #008080; font-weight: bold;">\&quot;</span>123<span style="color: #008080; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #008000;">+</span>
		    <span style="color: #666666;">&quot;&lt;username&gt;Tore&lt;/username&gt;&quot;</span> <span style="color: #008000;">+</span>
		    <span style="color: #666666;">&quot;&lt;password&gt;foo&lt;/password&gt;&quot;</span> <span style="color: #008000;">+</span>
		<span style="color: #666666;">&quot;&lt;/user&gt;&quot;</span> <span style="color: #008000;">+</span>
	<span style="color: #666666;">&quot;&lt;/myXml&gt;&quot;</span><span style="color: #008000;">;</span>
dynamic myXml <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlReader<span style="color: #000000;">&#40;</span>xmlString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>myXml.<span style="color: #0000FF;">stat</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// &quot;ok&quot;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>myXml.<span style="color: #0000FF;">user</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">username</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// &quot;Tore&quot;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>myXml.<span style="color: #0000FF;">user</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">nsid</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// &quot;123&quot;</span></pre></td></tr></table></div>

<p>This is a real declarative and easy way to handle xml, and since xml will only fail run time anyway, there is no argument that you lose compile time checking either.</p>
<p>I hope that has triggered you a bit on some exciting possibilities on using the dynamic type.</p>
<p><a href='http://vestues.no/tore/wp-content/dynamic.zip' title='Dynamic C#4.0 Xml example'>Here&#8217;s</a> the source. Please remember you will need C# 4.0 to make this work.</p>
<p>Thanks to <a href="http://saftsack.fs.uni-bayreuth.de/~dun3/archives/first-look-ducktyping-c-4-0-idynamicobject-metaobject/202.html">this</a> and <a href="http://blogs.msdn.com/cburrows/archive/2008/10/28/c-dynamic-part-ii.aspx">this</a> blog for giving me information and ideas.</p>
<p>-Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/01/05/creating-a-dynamic-xml-reader-with-c-40/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Boo AstMacros explained</title>
		<link>http://tore.vestues.no/2008/12/22/boo-astmacros-explained/</link>
		<comments>http://tore.vestues.no/2008/12/22/boo-astmacros-explained/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 21:15:39 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[Boo]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/12/22/boo-astmacros-explained/</guid>
		<description><![CDATA[In this post I am going to explain how you write your own macros in Boo. Writing macros is a powerful way to use the compiler extensibility built into Boo. Macros in Boo actually let you create your own keywords which are resolved at compile time.
Before you read on, these posts might be useful to [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I am going to explain how you write your own macros in Boo. Writing macros is a powerful way to use the compiler extensibility built into Boo. Macros in Boo actually let you create your own keywords which are resolved at compile time.</p>
<p>Before you read on, these posts might be useful to read:</p>
<ul>
<li><a href="http://tore.vestues.no/2008/09/09/boo-getting-started/">Boo Getting Started</a></li>
<li><a href="http://tore.vestues.no/2008/12/06/boo-extensions-explained/">Boo extensions explained</a></li>
<li><a href="http://tore.vestues.no/2008/12/18/boo-astattributes-explained/">Boo AstAttributes explained</a></li>
</ul>
<p>To create a Macro, you simply have to create a class that inherits from AbstractAstMacro and overrides the Expand-method. In addition your class name must end with &#8220;Macro&#8221;.</p>
<p>Let&#8217;s demonstrate with my DevNull-macro:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> DevNullMacro<span style="color: black;">&#40;</span>AbstractAstMacro<span style="color: black;">&#41;</span>:
	override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>How do you use it? In your code (in a different assembly than where DevNullMacro is situated) you can write something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> DoTheMath<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> <span style="color: #008000;">int</span>:
	devNull:
		x<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span></pre></td></tr></table></div>

<p>Here you send in a &#8220;Block&#8221; to the macro (&#8221;X>0&#8243;). You can actually also send arguments:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> DoTheMath<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> <span style="color: #008000;">int</span>:
	devNull x,<span style="color: #ff4500;">10</span>,<span style="color: #483d8b;">&quot;Hello&quot;</span>:
		x<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span></pre></td></tr></table></div>

<p>So, a Macro can take any number of arguments, and then it takes a block (which can be as big as you like).</p>
<p>Now why did I call this macro DevNull? I did it to demonstrate how the macros work. The point is if you choose to do nothing in the Expand method, nothing will happen. The compiler will actually remove the macro with its arguments and its Block. Meaning that since it is only our macro that handles the code inside the block sent into the macro we can send in whatever we&#8217;d like. </p>
<p>This code actually compiles:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> DoTheMath<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> <span style="color: #008000;">int</span>:
	devNull x,<span style="color: #ff4500;">10</span>,<span style="color: #483d8b;">&quot;Hello&quot;</span>:
		x<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span>
		I_can_write_whatever_here_actually</pre></td></tr></table></div>

<p>Ok, so when the compiler hits a macro statement in the compiling code, it finds the macro implementation (in this case the DevNullMacro), and sends it both the arguments and the code block. The compiler then removes all that code, and continues. </p>
<p>(I know this might seem a little confusing. When I started learning Boo it helped quite a lot to look at the compiled code using a tool like Reflector. You should try to write this code and look at it. It will clear up a few things, believe me.)</p>
<p>So what is the point then? The point is that within the macro, you can add code to the code structure. You can of course add whatever you like, but normally you add something based on the input to the macro.</p>
<p>To demonstrate something useful, I will show you how to implement <a href="http://en.wikipedia.org/wiki/Design_by_contract">Design By Contract</a>. I have previously demonstrated it in several talks I&#8217;ve held, but then I have implemented it as AstAttributes like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Account:	
	public Balance <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span>
&nbsp;
	<span style="color: black;">&#91;</span>Require<span style="color: black;">&#40;</span>amount<span style="color: #66cc66;">&gt;</span>=<span style="color: #ff4500;">0</span><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>Balance == originalBalance + amount<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> Deposit<span style="color: black;">&#40;</span>amount <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span><span style="color: black;">&#41;</span>:
		originalBalance = Balance
		Balance += amount</pre></td></tr></table></div>

<p>I will not go into details about this, but I&#8217;m pretty sure you will manage to do it yourself after reading <a href="http://tore.vestues.no/2008/12/18/boo-astattributes-explained/">my post about AstAttributes</a>.</p>
<p>Now I want to implement Design By Contract as macros, in the same way it is implemented in Eiffel, the language that introduced it. I want to write my assertions like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Account:
&nbsp;
	public Balance <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> Deposit<span style="color: black;">&#40;</span>amount <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span><span style="color: black;">&#41;</span>:
		requires:
			amount<span style="color: #66cc66;">&gt;</span>=<span style="color: #ff4500;">0</span>
		body:
			originalBalance = Balance
			Balance += amount
		ensures:
			Balance == originalBalance + amount</pre></td></tr></table></div>

<p>We have three macros here: requires, body, and ensures. I&#8217;m going to show you in detail how to implement the requires-part of the code, then I&#8217;ll add the source for both body and ensures as well. When we hit the requires macro in the code, we&#8217;re going to replace it with some actual code that forces those requirements. So, where we find this code in our Deposit-method,</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">requires:
	amount<span style="color: #66cc66;">&gt;</span>=<span style="color: #ff4500;">0</span></pre></td></tr></table></div>

<p>we want, at compile time, to put something like this to the output assembly:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: black;">&#40;</span>amount<span style="color: #66cc66;">&gt;</span>=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Some suitable error message&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>To add code where the macro statement is situated, we simply return a Statement from our Expand-method. What we want to make is a Block (which is a derived type of Statement):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Statement:
	blockToReturn = Block<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> blockToReturn</pre></td></tr></table></div>

<p>Still nothing happens, since we&#8217;re just returning an empty Block. We have to fill the return block with something. So for each assertion (which turns out as Statements) in our code, we want to assert it. Let&#8217;s loop all our statements:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Statement:
	blockToReturn = Block<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> statement <span style="color: #ff7700;font-weight:bold;">in</span> macro.<span style="color: black;">Block</span>.<span style="color: black;">Statements</span>:
		expression <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression = TryMakeBinaryExpression<span style="color: black;">&#40;</span>statement<span style="color: black;">&#41;</span>
		blockToReturn.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>
			CreateAssertblockFromBinaryExpression<span style="color: black;">&#40;</span>expression<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">return</span> blockToReturn</pre></td></tr></table></div>

<p>In addition to the looping, which is pretty straight forward, we&#8217;re doing two new things here. We&#8217;re trying to turn every statement into a BinaryExpression, and we&#8217;re creating blocks for each BinaryExpression.</p>
<p>Let&#8217;s look at our TryMakeBinaryExpression first. The point is that if we want to assert something, we have to make sure they resolve to either true or false. First we need all the Statements to be ExpressionStatements. If you for instance had written an if-statement in you macro like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">requires:
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff4500;">1</span>==<span style="color: #ff4500;">1</span>:
		do_something</pre></td></tr></table></div>

<p>This will not be an ExpressionStatement, it is an IfStatement (you should look into the Boo class-hierarchy, use Visual studio or Reflector). </p>
<p>So, when we have assured us it is all ExpressionStatements we have to deal with, we also want to ensure they are all BinaryExpressions. They all have to resolve into either true or false. Both of these checks are run here:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> TryMakeBinaryExpression<span style="color: black;">&#40;</span>statement <span style="color: #ff7700;font-weight:bold;">as</span> Statement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression:
	expression = statement <span style="color: #ff7700;font-weight:bold;">as</span> ExpressionStatement 
	<span style="color: #ff7700;font-weight:bold;">if</span> expression <span style="color: #ff7700;font-weight:bold;">is</span> null:
		RaiseNotBinaryException<span style="color: black;">&#40;</span>statement.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	binaryexpression = expression.<span style="color: black;">Expression</span> <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression 
	<span style="color: #ff7700;font-weight:bold;">if</span> binaryexpression <span style="color: #ff7700;font-weight:bold;">is</span> null:
		RaiseNotBinaryException<span style="color: black;">&#40;</span>expression.<span style="color: black;">Expression</span>.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">return</span> binaryexpression</pre></td></tr></table></div>

<p>Ok, now we either have a BinaryExpression or it has already failed. Let&#8217;s make it into a block:</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="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> CreateAssertblockFromBinaryExpression<span style="color: black;">&#40;</span>expression <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Block:
	exceptionText = <span style="color: #483d8b;">&quot;Voilated precondition: &quot;</span> + expression.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>|
			block:
				<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> $<span style="color: black;">&#40;</span>expression<span style="color: black;">&#41;</span>:
					<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span>$exceptionText<span style="color: black;">&#41;</span>
		|<span style="color: black;">&#93;</span>.<span style="color: black;">Block</span></pre></td></tr></table></div>

<p>Here we&#8217;re using quasiquotations again. It&#8217;s understandable and easy to both read and write.</p>
<p>And that&#8217;s the complete macro. So now for every BinaryExpression written, the macro will assert its correctness. Here&#8217;s the complete code:</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
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> System
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RequiresMacro<span style="color: black;">&#40;</span>AbstractAstMacro<span style="color: black;">&#41;</span>:
&nbsp;
	override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Statement:
		blockToReturn = Block<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> statement <span style="color: #ff7700;font-weight:bold;">in</span> macro.<span style="color: black;">Block</span>.<span style="color: black;">Statements</span>:
			expression <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression = TryMakeBinaryExpression<span style="color: black;">&#40;</span>statement<span style="color: black;">&#41;</span>
			blockToReturn.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>
				CreateAssertblockFromBinaryExpression<span style="color: black;">&#40;</span>expression<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> blockToReturn
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> CreateAssertblockFromBinaryExpression<span style="color: black;">&#40;</span>expression <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Block:
		exceptionText = <span style="color: #483d8b;">&quot;Voilated precondition: &quot;</span> + expression.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>|
				block:
					<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> $<span style="color: black;">&#40;</span>expression<span style="color: black;">&#41;</span>:
						<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span>$exceptionText<span style="color: black;">&#41;</span>
			|<span style="color: black;">&#93;</span>.<span style="color: black;">Block</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> TryMakeBinaryExpression<span style="color: black;">&#40;</span>statement <span style="color: #ff7700;font-weight:bold;">as</span> Statement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression:
		expression = statement <span style="color: #ff7700;font-weight:bold;">as</span> ExpressionStatement 
		<span style="color: #ff7700;font-weight:bold;">if</span> expression <span style="color: #ff7700;font-weight:bold;">is</span> null:
			RaiseNotBinaryException<span style="color: black;">&#40;</span>statement.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
		binaryexpression = expression.<span style="color: black;">Expression</span> <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression 
		<span style="color: #ff7700;font-weight:bold;">if</span> binaryexpression <span style="color: #ff7700;font-weight:bold;">is</span> null:
			RaiseNotBinaryException<span style="color: black;">&#40;</span>expression.<span style="color: black;">Expression</span>.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">return</span> binaryexpression
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> RaiseNotBinaryException<span style="color: black;">&#40;</span>typeInfo <span style="color: #ff7700;font-weight:bold;">as</span> String<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Not a binary expression: &quot;</span> + typeInfo<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The other two macros, BodyMacro and EnsuresMacro are listed here:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> System
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> BodyMacro<span style="color: black;">&#40;</span>AbstractAstMacro<span style="color: black;">&#41;</span>:
&nbsp;
	override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Statement:
		<span style="color: #ff7700;font-weight:bold;">return</span> macro.<span style="color: black;">Block</span></pre></td></tr></table></div>

<p>Here we only need to return the actual code written within the macro.</p>
<p>And here&#8217;s the EnsuresMacro:</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
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> System
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> EnsuresMacro<span style="color: black;">&#40;</span>AbstractAstMacro<span style="color: black;">&#41;</span>:
&nbsp;
	override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span>:
		ancestor <span style="color: #ff7700;font-weight:bold;">as</span> Method = macro.<span style="color: black;">GetAncestor</span><span style="color: black;">&#40;</span>NodeType.<span style="color: black;">Method</span><span style="color: black;">&#41;</span>
&nbsp;
		oldBody <span style="color: #ff7700;font-weight:bold;">as</span> Block = ancestor.<span style="color: black;">Body</span>
		ensuresBody <span style="color: #ff7700;font-weight:bold;">as</span> Block = RequiresMacro<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">Expand</span><span style="color: black;">&#40;</span>macro<span style="color: black;">&#41;</span>
		newBody = <span style="color: black;">&#91;</span>|
			block:
				<span style="color: #ff7700;font-weight:bold;">try</span>:
					$oldBody
				ensure:
					$ensuresBody
		|<span style="color: black;">&#93;</span>.<span style="color: black;">Block</span>
&nbsp;
		ancestor.<span style="color: black;">Body</span> = newBody</pre></td></tr></table></div>

<p>Here we take all the code in the method containing the macro, and surrounds it with a try-clause to ensure that we always run the ensures-part of the code. Notice also that we&#8217;re using the RequireMacro, since it creates the assert-block exactly the way we want it.</p>
<p>A helping hand:<br />
When you explore this, I actually advice you to write some of your macro code in C#, as it gives you a lot more intellisense-help, then when you have found out what you want, write them back in Boo, as it is often much more readable, especially with the nice quasiquotation which you do not have in C#.</p>
<p>Hope it helps!</p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/12/22/boo-astmacros-explained/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Boo AstAttributes explained</title>
		<link>http://tore.vestues.no/2008/12/18/boo-astattributes-explained/</link>
		<comments>http://tore.vestues.no/2008/12/18/boo-astattributes-explained/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 22:31:50 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[Boo]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/12/18/boo-astattributes-explained/</guid>
		<description><![CDATA[Writing extensions for Boo is a very powerful thing. In this post I&#8217;m going to explain how to write AstAttributes in Boo. These attributes are much more than normal .net attributes. They are one of the ways you can extend the Boo language.
Before you read on, these posts might be useful to read:

Boo Getting Started
Boo [...]]]></description>
			<content:encoded><![CDATA[<p>Writing extensions for Boo is a very powerful thing. In this post I&#8217;m going to explain how to write AstAttributes in Boo. These attributes are much more than normal .net attributes. They are one of the ways you can extend the Boo language.</p>
<p>Before you read on, these posts might be useful to read:</p>
<ul>
<li><a href="http://tore.vestues.no/2008/09/09/boo-getting-started/">Boo Getting Started</a></li>
<li><a href="http://tore.vestues.no/2008/12/06/boo-extensions-explained/">Boo extensions explained</a></li>
</ul>
<p>To implement an AstAttribute you have to create a class that inherits the AbstractAstAttribute-class and implements the Apply-method. The class name must be postfixed with &#8220;Attribute&#8221;, the syntax is then &#8220;&#8216;YourAttributeName&#8217;Attribute&#8221;. Like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> DemoAttribute<span style="color: black;">&#40;</span>AbstractAstAttribute<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">def</span> Apply<span style="color: black;">&#40;</span><span style="color: #008000;">type</span> <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>You have now created an AstAttribute named Demo, and you can use it (from another assembly) like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span>Demo<span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">class</span> MyTestClass:
	<span style="color: #ff7700;font-weight:bold;">def</span> constructor<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>For the moment our attribute does absolutely nothing. To make it do something we have to do some work in the Apply-method we implemented. The method takes a Node as an argument. A Node can be just about any type, since alot of classes inherit the Node-class. Here the Node is the actual &#8220;thing&#8221; that you tagged the Attribute with. In our example the Node will be a definition of the MyTestClass. Say you tag a Method you will get the method definition (including all its content) passed into the Apply method. We will examine the possibilities this gives us, but let us first take a look at what sub types the Node-class has, that are relevant for us.</p>
<p><a href='http://vestues.no/tore/wp-content/classdefinitioninheritance.png' title='Boo ClassDefinition inheritance picture'><img src='http://vestues.no/tore/wp-content/classdefinitioninheritance.thumbnail.png' alt='Boo ClassDefinition inheritance picture' /></a></p>
<p>The picture shows the relevant subtypes. In other words these subtypes show where you can use your attribute, meaning you can tag classes, method parameters, methods, constructors, fields and properties with an AstAttribute that you create.</p>
<p>To experiment with this, I&#8217;ve made a little solution for you that you, it will print just about any information that is available at compile-time. (I&#8217;ve added it at the bottom)</p>
<p><strong>NotifyPropertyChanged</strong></p>
<p>I want to demonstrate an AstAttribute that I&#8217;ve been using in quite a few of the presentations I&#8217;ve held on Boo, and that I have also mentioned in this blog earlier (<a href="http://tore.vestues.no/2008/09/28/what-makes-boo-great/">What makes boo great</a>): the NotifyPropertyChanged-attribute. I wrote this attribute about half a year ago, and it really blew my mind on all the possibilities Boo offers.</p>
<p>First let&#8217;s look at how we implement the INotifyPropertyChanged in C#, just to remind us of what parts we want to auto generate.</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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Customer<span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> firstName<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> lastName<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FirstName
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> firstName<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        set
        <span style="color: #000000;">&#123;</span>
            firstName <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            NotifyPropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;FirstName&quot;</span><span style="color: #000000;">&#41;</span><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> LastName
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> lastName<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        set
        <span style="color: #000000;">&#123;</span>
            lastName <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            NotifyPropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;LastName&quot;</span><span style="color: #000000;">&#41;</span><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: #0600FF;">event</span> PropertyChangedEventHandler PropertyChanged<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> NotifyPropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span> info<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>PropertyChanged <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            PropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <span style="color: #008000;">new</span> PropertyChangedEventArgs<span style="color: #000000;">&#40;</span>info<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>That code is as ugly as always. We basically want to write it like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span>NotifyPropertyChanged<span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">class</span> BooCustomer:
	<span style="color: black;">&#91;</span>Property<span style="color: black;">&#40;</span>LastName<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	lastName <span style="color: #ff7700;font-weight:bold;">as</span> String
	<span style="color: black;">&#91;</span>Property<span style="color: black;">&#40;</span>FirstName<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	firstName <span style="color: #ff7700;font-weight:bold;">as</span> String</pre></td></tr></table></div>

<p>Ok, the first thing we have to do is make sure the attribute is used on a class and not somewhere else. Let&#8217;s do it like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> Apply<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span>:
	classDef = GetClassDefinition<span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> GetClassDefinition<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition:
	classDef = target <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition
	<span style="color: #ff7700;font-weight:bold;">if</span> classDef <span style="color: #ff7700;font-weight:bold;">is</span> null:
		<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;The NotifyPropertyChangedAttribute can only be used on classes&quot;</span><span style="color: black;">&#41;</span>		
	<span style="color: #ff7700;font-weight:bold;">return</span> classDef</pre></td></tr></table></div>

<p>We&#8217;re casting the Node to a ClassDefinition, which is the type it must be if the attribute is used on a class. Then we must add the INotifyPropertyChanged to the class definition. Notice that our BooCustomer hasn&#8217;t implemented it, since it&#8217;s expecting us (the attribute) to do it.</p>
<p>Ok, lets add the code for that:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> Apply<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span>:
	classDef = GetClassDefinition<span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>
	AddInterface<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> AddInterface<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
	classDef.<span style="color: black;">BaseTypes</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>SimpleTypeReference<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;System.ComponentModel.INotifyPropertyChanged&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>That was simple! Next we need to add the actual Event:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> Apply<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span>:
	classDef = GetClassDefinition<span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>
	AddInterface<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
	AddEvent<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> AddEvent<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
	notifyEvent = Event<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	notifyEvent.<span style="color: black;">Type</span> = SimpleTypeReference<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;System.ComponentModel.PropertyChangedEventHandler&quot;</span><span style="color: black;">&#41;</span>
	notifyEvent.<span style="color: black;">Name</span> = <span style="color: #483d8b;">&quot;PropertyChanged&quot;</span>
	classDef.<span style="color: black;">Members</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>notifyEvent<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Here we create an event, set the right type, set the name, and simply add it to our classdefinition. </p>
<p>The last thing to do is to find all the properties, and add some code to the setter of those properties. This is how we find all the properties:</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: #ff7700;font-weight:bold;">def</span> AddToAllProperties<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">for</span> member <span style="color: #ff7700;font-weight:bold;">in</span> classDef.<span style="color: black;">Members</span>:
		<span style="color: #008000;">property</span> = member <span style="color: #ff7700;font-weight:bold;">as</span> Property
		<span style="color: #ff7700;font-weight:bold;">continue</span> <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">property</span> <span style="color: #ff7700;font-weight:bold;">is</span> null
		AddNewBody<span style="color: black;">&#40;</span><span style="color: #008000;">property</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>And to conclude it, we must implement the AddNewBody-method:</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="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> AddNewBody<span style="color: black;">&#40;</span><span style="color: #008000;">property</span> <span style="color: #ff7700;font-weight:bold;">as</span> Property<span style="color: black;">&#41;</span>:
	oldBody <span style="color: #ff7700;font-weight:bold;">as</span> Block = <span style="color: #008000;">property</span>.<span style="color: black;">Setter</span>.<span style="color: black;">Body</span>
	<span style="color: #008000;">property</span>.<span style="color: black;">Setter</span>.<span style="color: black;">Body</span> = <span style="color: black;">&#91;</span>|
		$oldBody
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>PropertyChanged <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> null<span style="color: black;">&#41;</span>:
			PropertyChanged<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,System.<span style="color: black;">ComponentModel</span>.<span style="color: black;">PropertyChangedEventArgs</span><span style="color: black;">&#40;</span>$<span style="color: black;">&#40;</span><span style="color: #008000;">property</span>.<span style="color: black;">Name</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	|<span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>Ok, here&#8217;s something new. The [|...|]-tags. You probably can understand what is happening here, but not really why. The syntax is easy to both read and write. It&#8217;s called quasi quotation. I will describe it further in another post.</p>
<p>That concludes our attribute! Here&#8217;s the complete code:</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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> System
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> NotifyPropertyChangedAttribute<span style="color: black;">&#40;</span>AbstractAstAttribute<span style="color: black;">&#41;</span>:
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> Apply<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span>:
		classDef = GetClassDefinition<span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>
		AddInterface<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
		AddEvent<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
		AddToAllProperties<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> GetClassDefinition<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition:
		classDef = target <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition
		<span style="color: #ff7700;font-weight:bold;">if</span> classDef <span style="color: #ff7700;font-weight:bold;">is</span> null:
			<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;The NotifyPropertyChangedAttribute can only be used on classes&quot;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> classDef
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> AddInterface<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
		classDef.<span style="color: black;">BaseTypes</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>SimpleTypeReference<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;System.ComponentModel.INotifyPropertyChanged&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> AddEvent<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
		notifyEvent = Event<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		notifyEvent.<span style="color: black;">Type</span> = SimpleTypeReference<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;System.ComponentModel.PropertyChangedEventHandler&quot;</span><span style="color: black;">&#41;</span>
		notifyEvent.<span style="color: black;">Name</span> = <span style="color: #483d8b;">&quot;PropertyChanged&quot;</span>
		classDef.<span style="color: black;">Members</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>notifyEvent<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> AddToAllProperties<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">for</span> member <span style="color: #ff7700;font-weight:bold;">in</span> classDef.<span style="color: black;">Members</span>:
			<span style="color: #008000;">property</span> = member <span style="color: #ff7700;font-weight:bold;">as</span> Property
			<span style="color: #ff7700;font-weight:bold;">continue</span> <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">property</span> <span style="color: #ff7700;font-weight:bold;">is</span> null
			AddNewBody<span style="color: black;">&#40;</span><span style="color: #008000;">property</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> AddNewBody<span style="color: black;">&#40;</span><span style="color: #008000;">property</span> <span style="color: #ff7700;font-weight:bold;">as</span> Property<span style="color: black;">&#41;</span>:
		oldBody <span style="color: #ff7700;font-weight:bold;">as</span> Block = <span style="color: #008000;">property</span>.<span style="color: black;">Setter</span>.<span style="color: black;">Body</span>
		<span style="color: #008000;">property</span>.<span style="color: black;">Setter</span>.<span style="color: black;">Body</span> = <span style="color: black;">&#91;</span>|
			$oldBody
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>PropertyChanged <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> null<span style="color: black;">&#41;</span>:
				PropertyChanged<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,System.<span style="color: black;">ComponentModel</span>.<span style="color: black;">PropertyChangedEventArgs</span><span style="color: black;">&#40;</span>$<span style="color: black;">&#40;</span><span style="color: #008000;">property</span>.<span style="color: black;">Name</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		|<span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>Using that code, you can now write classes like this that actually implement the complete INotifyPropertyChanged-behaviour:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">&nbsp;
&nbsp;
<span style="color: black;">&#91;</span>NotifyPropertyChanged<span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">class</span> BooCustomer:
	<span style="color: black;">&#91;</span>Property<span style="color: black;">&#40;</span>LastName<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	lastName <span style="color: #ff7700;font-weight:bold;">as</span> String
	<span style="color: black;">&#91;</span>Property<span style="color: black;">&#40;</span>FirstName<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	firstName <span style="color: #ff7700;font-weight:bold;">as</span> String</pre></td></tr></table></div>

<p>Try it yourself and look at the code through reflector to see what is really going on here. Remember to put the attribute and the classes using it in different assemblies.</p>
<p>Any comments on how to improve or clarify any of this will be appreciated.</p>
<p>The solution that prints alot of info on your attribute, and shows where you can place it: <a href='http://vestues.no/tore/wp-content/attributeexplorer.zip' title='Boo AttributeExplorer-solution'>Boo AttributeExplorer-solution</a></p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/12/18/boo-astattributes-explained/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Boo: Getting started</title>
		<link>http://tore.vestues.no/2008/09/09/boo-getting-started/</link>
		<comments>http://tore.vestues.no/2008/09/09/boo-getting-started/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 06:52:07 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[Boo]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/09/09/boo-getting-started/</guid>
		<description><![CDATA[Want to look into Boo? Let me give you a quick guide to getting the tools you need.
Development environment
For the time being I recommend you do your coding in Sharpdevelop. Download and install the latest  here. You should also download the latest Boo-distro (the binaries), and move (and overwrite) the Boo-files into The sharpdevelop [...]]]></description>
			<content:encoded><![CDATA[<p>Want to look into Boo? Let me give you a quick guide to getting the tools you need.</p>
<p><strong>Development environment</strong></p>
<p>For the time being I recommend you do your coding in Sharpdevelop. Download and install the latest  <a href="http://www.icsharpcode.net/OpenSource/SD/Download/">here</a>. You should also download the latest <a href="http://dist.codehaus.org/boo/distributions/?C=M;O=D">Boo-distro</a> (the binaries), and move (and overwrite) the Boo-files into The sharpdevelop install-folder: SharpDevelop\3.0\AddIns\AddIns\BackendBindings\BooBinding. Now your development environment is in place.</p>
<p>For Visual Studio integration, <a href="http://www.codeplex.com/BooLangStudio/Release/ProjectReleases.aspx?ReleaseId=16551">Boo Lang Studio</a> is the thing. But sadly I don&#8217;t find it stable enough yet.</p>
<p><strong>The source</strong></p>
<p>Yes, you should download the <a href="http://dist.codehaus.org/boo/distributions/?C=M;O=D">Boo source code</a>. It&#8217;s helpful to use it to browse class hierarchies, to see how things actually work, and most importantly, the source contains a lot of example code. I actually learned macros by looking at the source code for the built in macros (print, assert etc).</p>
<p><strong>The compiler extensibility</strong></p>
<p>To fully understand how Boo works with the compiler, it&#8217;s very helpful to be able to inspect the result. That is, the assemblies that are produced. There is an excellent tool for doing that, <a href="http://reflector.red-gate.com/download.aspx">Reflector</a>. Download it, and start looking at the compiled assemblies you produce.</p>
<p><strong>Starting points</strong></p>
<p>You should start at <a href="http://boo.codehaus.org">the Boo homepage</a>. Also take a look at <a href="http://www.ayende.com/Blog/category/498.aspx">Ayende&#8217;s blog</a>. He blogs quite a bit about Boo.</p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/09/09/boo-getting-started/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
