<?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>Nicholas Blumhardt</title>
	<atom:link href="http://nblumhardt.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://nblumhardt.com</link>
	<description></description>
	<lastBuildDate>Fri, 28 May 2010 22:19:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Simplifying &#8216;Configuration by Exception&#8217;</title>
		<link>http://nblumhardt.com/2010/05/simplifying-configuration-by-exception/</link>
		<comments>http://nblumhardt.com/2010/05/simplifying-configuration-by-exception/#comments</comments>
		<pubDate>Fri, 28 May 2010 22:18:50 +0000</pubDate>
		<dc:creator>Nicholas Blumhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nblumhardt.com/?p=200</guid>
		<description><![CDATA[Given an assembly containing components, most of them will have similar configuration requirements. A simple assembly scanning statement usually applies to 90% of them: Assembly componentsAssembly = // ... var builder = new ContainerBuilder&#40;&#41;; builder.RegisterAssemblyTypes&#40;componentsAssembly&#41; &#160; &#160; .AsImplementedInterfaces&#40;&#41;; Now, the annoying thing is &#8211; what if one of those components needs to be scoped per-HTTP-request? [...]]]></description>
			<content:encoded><![CDATA[<p>Given an assembly containing components, most of them will have similar configuration requirements. A simple assembly scanning statement usually applies to 90% of them:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Assembly componentsAssembly <span style="color: #008000;">=</span> <span style="color: #008080; font-style: italic;">// ...</span><br />
<br />
var builder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContainerBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
builder.<span style="color: #0000FF;">RegisterAssemblyTypes</span><span style="color: #000000;">&#40;</span>componentsAssembly<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0000FF;">AsImplementedInterfaces</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Now, the annoying thing is &#8211; what if one of those components needs to be scoped per-HTTP-request?</p>
<p>In Autofac 2.1, you could exclude it explicitly and then perform configuration separately:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">builder.<span style="color: #0000FF;">RegisterAssemblyTypes</span><span style="color: #000000;">&#40;</span>componentsAssembly<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0000FF;">AsImplementedInterfaces</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0000FF;">Except</span><span style="color: #008000;">&lt;</span>LocalCache<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
builder.<span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&lt;</span>LocalCache<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0600FF;">As</span><span style="color: #008000;">&lt;</span>ICache<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0000FF;">HttpRequestScoped</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>I wouldn&#8217;t say the repetition here is too bad, but over a larger assembly the separation between the <code class="codecolorer text default"><span class="text">Except()</span></code> clause and the subsequent registration makes the code harder to follow and thus maintain.</p>
<p>Autofac&#8217;s scanning feature is new in version 2, and is slowly evolving towards a minimal syntax. In the latest release, <a href="http://nblumhardt.com/2010/05/autofac-2-2-released/">version 2.2</a>, the configuration of an excepted component can be done in-place:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">builder.<span style="color: #0000FF;">RegisterAssemblyTypes</span><span style="color: #000000;">&#40;</span>componentsAssembly<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0000FF;">AsImplementedInterfaces</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0000FF;">Except</span><span style="color: #008000;">&lt;</span>LocalCache<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>cache <span style="color: #008000;">=&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; cache.<span style="color: #0600FF;">As</span><span style="color: #008000;">&lt;</span>ICache<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span style="color: #0000FF;">HttpRequestScoped</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>The configuration for the excepted component doesn&#8217;t inherit any of the configuration from the outer scanning operation, hence it is necessary to specify the services it will provide despite the <code class="codecolorer text default"><span class="text">AsImplementedInterfaces()</span></code> clause in the scanning statement. While I think it would be more concise to specify only the differences between the explicitly-configured component and the rest of the scanning operation, override rules tend to bring a lot of subtle complexities with them and so they&#8217;re avoided here.</p>
]]></content:encoded>
			<wfw:commentRss>http://nblumhardt.com/2010/05/simplifying-configuration-by-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autofac 2.2 Released</title>
		<link>http://nblumhardt.com/2010/05/autofac-2-2-released/</link>
		<comments>http://nblumhardt.com/2010/05/autofac-2-2-released/#comments</comments>
		<pubDate>Fri, 28 May 2010 12:39:52 +0000</pubDate>
		<dc:creator>Nicholas Blumhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nblumhardt.com/?p=195</guid>
		<description><![CDATA[It’s that time again! There are some great features from the original plan still in the pipeline, but the completed work is important to some users so binaries for Autofac 2.2 have been made available on the Autofac site. What’s new? Version 2.2 is highly backwards-compatible with 2.1. Along with many small improvements, the significant [...]]]></description>
			<content:encoded><![CDATA[<p>It’s that time again!  There are some great features from the original plan still in the pipeline, but the completed work is important to some users so binaries for Autofac 2.2 have been made available on the <a href="http://autofac.org">Autofac site</a>.</p>
<h3>What’s new?</h3>
<p>Version 2.2 is highly backwards-compatible with 2.1. Along with many small improvements, the significant additions in the new version are the return of a mutable container model, support for ASP.NET MVC 2.0 and lightweight adapters.</p>
<h4>ContainerBuilder.Update() and full container mutability</h4>
<p>One of the most controversial changes to Autofac in the 2.1 release was the deliberate omission of an easy way to add more components to an already-built container. The reasons for and against this are worth a blog post on their own, but in the end it turns out that integration into third-party frameworks is especially tough without this feature.</p>
<p>To add components to an existing container, use <code class="codecolorer text default"><span class="text">ContainerBuilder.Update()</span></code>:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var container <span style="color: #008000;">=</span> <span style="color: #008080; font-style: italic;">// something already built</span><br />
<br />
var updater <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContainerBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
updater.<span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&lt;</span>A<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
updater.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> B<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">As</span><span style="color: #008000;">&lt;</span>IB<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Add the registrations to the container</span><br />
updater.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span>container<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Configuration this way is often much harder to follow than a simple &#8216;register then build&#8217; style, so again, use this feature only where necessary.</p>
<h4>ASP.NET MVC 2.0 support</h4>
<p>The new release is built against the <a href="http://asp.net/mvc">ASP.NET MVC 2</a> binaries; you&#8217;ll have to compile from source if you want to target ASP.NET MVC 1. There can&#8217;t be many projects out there that can upgrade to Autofac 2.2, but not to the new release of ASP.NET MVC, so we expect this will be convenient for most users.</p>
<p>Out of the box, Autofac now supports ASP.NET MVC Areas, and integration with other ASP.NET MVC features is getting richer.</p>
<p>The <code class="codecolorer text default"><span class="text">AutofacControllerFactory</span></code> policies have changed in this release, so if you previously added controllers by hand (as named services) you will need to update your code to register controllers by their concrete type (without an <code class="codecolorer text default"><span class="text">As()</span></code> or <code class="codecolorer text default"><span class="text">Named()</span></code> configuration clause..)</p>
<h4>Lightweight adapters</h4>
<p>Lightweight adapters are an easy way of expressing <em>&#8220;for every registered <code class="codecolorer text default"><span class="text">X</span></code>, provide an adapter of type <code class="codecolorer text default"><span class="text">Y</span></code>.&#8221;</em><br />
<a href="http://nblumhardt.com/wp-content/uploads/2010/05/adapters.png"><img src="http://nblumhardt.com/wp-content/uploads/2010/05/adapters.png" alt="adapters" title="adapters" width="487" height="261" class="aligncenter size-full wp-image-196" /></a></p>
<p>The <a href="http://nblumhardt.com/2010/04/lightweight-adaptation-%E2%80%93-coming-soon/">introductory article</a> is the best place to find out how this feature works.</p>
<h4>Other changes</h4>
<ul>
<li>COM interfaces using <a href="http://blogs.msdn.com/b/mshneer/archive/2010/01/29/nopia-blog-posts.aspx">No-PIA</a> (or &#8216;<a href="http://msdn.microsoft.com/en-us/library/dd997297.aspx"> embedded interop types</a>&#8216;) can now be exposed as services</li>
<li>Simplified <a href="http://alexmg.com/post/2010/05/16/Making-Self-Hosting-with-Autofac-WCF-Integration-easier.aspx">WCF configuration for self-hosted services</a></li>
<li>Scoped registration improvements: <code class="codecolorer text default"><span class="text">SingleInstance()</span></code> now behaves as expected and binds the instance&#8217;s lifetime to the nested scope</li>
<li>Generic registration constraint checking improvements &#8211; more complex type constraints can now be handled when determining the suitability of an open generic type to satisfy a closed generic service</li>
<li>Additional <code class="codecolorer text default"><span class="text">Resolve()</span></code>/<code class="codecolorer text default"><span class="text">TryResolve()</span></code> convenience overloads have been added to fill some gaps</li>
<li>The same type can now be registered multiple times in a single XML configuration block</li>
<li>Several usability enhancements have been made to the scanning feature</li>
</ul>
<h3>Credits</h3>
<p>This release, as with every Autofac release, is the product of the lively (and growing) community. There have been some especially fine patches and contributions this time around &#8211; thank you everyone for your hard work!</p>
]]></content:encoded>
			<wfw:commentRss>http://nblumhardt.com/2010/05/autofac-2-2-released/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Lightweight Adaptation – Coming Soon</title>
		<link>http://nblumhardt.com/2010/04/lightweight-adaptation-%e2%80%93-coming-soon/</link>
		<comments>http://nblumhardt.com/2010/04/lightweight-adaptation-%e2%80%93-coming-soon/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 21:24:29 +0000</pubDate>
		<dc:creator>Nicholas Blumhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nblumhardt.com/?p=183</guid>
		<description><![CDATA[The type-to-type mapping of an IoC container obscures the fact that in reality, IoC configuration describes object graphs. Perhaps this is why it is sometimes difficult to reason about how component instances will come together at runtime? The problem dealt with in this article fits this description – a simple object graph that is unintuitive [...]]]></description>
			<content:encoded><![CDATA[<p>The <em>type-to-type</em> mapping of an IoC container obscures the fact that in reality, IoC configuration describes <em>object graphs</em>.</p>
<p>Perhaps this is why it is sometimes difficult to reason about how component instances will come together at runtime?</p>
<p>The problem dealt with in this article fits this description – a simple object graph that is unintuitive to build with a typical container.</p>
<h3>Back to the Gang of Four…</h3>
<p>The classic “Gang of Four”-style Adapter pattern ‘adapts the interface of one class to another’.<br />
Common scenarios where adapter-like structures appear are, for example:</p>
<ul>
<li>where a wrapper is needed in order to work efficiently with an underlying abstraction;</li>
<li>between different representations of the same thing, e.g. the endless variations on ILog; or,</li>
<li>between corresponding elements in different logical models</li>
</ul>
<p>We’ll use a fairly broad definition of the pattern, in the context of an image editing application.</p>
<p><a href="http://nblumhardt.com/wp-content/uploads/2010/04/onetoone.png"><img src="http://nblumhardt.com/wp-content/uploads/2010/04/onetoone.png" alt="one-to-one" width="404" height="101" class="aligncenter size-full wp-image-187" /></a></p>
<p>A <code class="codecolorer text default"><span class="text">ToolbarButton</span></code> has an <code class="codecolorer text default"><span class="text">ICommand</span></code> that is invoked when the button is clicked. <code class="codecolorer text default"><span class="text">ToolbarButton</span></code> is an adapter for <code class="codecolorer text default"><span class="text">ICommand</span></code> that allows the command to be invoked from the user interface.</p>
<p>Here we’ve attached an instance of <code class="codecolorer text default"><span class="text">ToolbarButton</span></code> to an instance of <code class="codecolorer text default"><span class="text">SaveCommand</span></code>, which implements <code class="codecolorer text default"><span class="text">ICommand</span></code>. The classes are:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> ICommand<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">void</span> Execute<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SaveCommand <span style="color: #008000;">:</span> ICommand<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Execute<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Save the current image</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ToolbarButton<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; ICommand _command<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> ToolbarButton<span style="color: #000000;">&#40;</span>ICommand command<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _command <span style="color: #008000;">=</span> command<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Click<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _command.<span style="color: #0000FF;">Execute</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>So… from an IoC perspective, what is interesting about this example?</p>
<h3>Adapting Multiple Implementations</h3>
<p>An application has more than one kind of command. An image editor may have Save, Open and a whole host of other commands:</p>
<p><a href="http://nblumhardt.com/wp-content/uploads/2010/04/PaintDotNETToolbar.PNG"><img src="http://nblumhardt.com/wp-content/uploads/2010/04/PaintDotNETToolbar.PNG" alt="PaintDotNETToolbar" width="230" height="109" class="aligncenter size-full wp-image-188" /></a><br />
But, even though all of the command implementations are the different, the widget representing them on the user interface is implemented by the same component.</p>
<p>To bring the example together let’s add an <code class="codecolorer text default"><span class="text">EditorWindow</span></code> that accepts all of the toolbar buttons as an enumerable dependency.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> EditorWindow<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> EditorWindow<span style="color: #000000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span>ToolbarButton<span style="color: #008000;">&gt;</span> toolbarButtons<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Left to the reader’s imagination</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Our intention here is that for each <code class="codecolorer text default"><span class="text">ICommand</span></code> in the container, a <code class="codecolorer text default"><span class="text">ToolbarButton</span></code> will be created to wrap it, and all of these will be passed along to the <code class="codecolorer text default"><span class="text">EditorWindow</span></code> via the <code class="codecolorer text default"><span class="text">IEnumerable&lt;T&gt;</span></code> <a href="http://nblumhardt.com/2010/01/the-relationship-zoo/">relationship type</a> .</p>
<p>These might be registered with Autofac in the following way:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var builder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContainerBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
builder.<span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&lt;</span>SaveCommand<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">As</span><span style="color: #008000;">&lt;</span>ICommand<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
builder.<span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&lt;</span>OpenCommand<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">As</span><span style="color: #008000;">&lt;</span>ICommand<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
builder.<span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&lt;</span>ToolbarButton<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
builder.<span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&lt;</span>EditorWindow<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var container <span style="color: #008000;">=</span> builder.<span style="color: #0000FF;">Build</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; var window <span style="color: #008000;">=</span> container.<span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&lt;</span>EditorWindow<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; window.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Yet, there is something wrong. The static structure of our code is correct, but as I lamented earlier, the object graph isn’t what we expect.</p>
<p>The problem is that, as far as the container is concerned, there is only one <code class="codecolorer text default"><span class="text">ToolbarButton</span></code> component. This will be initialised with whatever happens to be the default implementation of <code class="codecolorer text default"><span class="text">ICommand</span></code>. Autofac’s last-in-wins policy means that <code class="codecolorer text default"><span class="text">OpenCommand</span></code> will be used as the default implementation of <code class="codecolorer text default"><span class="text">ICommand</span></code>, and <code class="codecolorer text default"><span class="text">SaveCommand</span></code> will be ignored.</p>
<p><a href="http://nblumhardt.com/wp-content/uploads/2010/04/nonadapted.png"><img src="http://nblumhardt.com/wp-content/uploads/2010/04/nonadapted.png" alt="nonadapted" width="430" height="238" class="aligncenter size-full wp-image-189" /></a></p>
<p>This happens because the container builds object graphs top-down; first it looks for <code class="codecolorer text default"><span class="text">ToolbarButton</span></code>, then it looks for an <code class="codecolorer text default"><span class="text">ICommand</span></code> to satisfy its dependencies.</p>
<h3>Registering Adapters</h3>
<p>What we’d really like is to create multiple <code class="codecolorer text default"><span class="text">ToolbarButtons</span></code>, each attached to a different underlying <code class="codecolorer text default"><span class="text">ICommand</span></code> implementation.</p>
<p><a href="http://nblumhardt.com/wp-content/uploads/2010/04/adapters.png"><img src="http://nblumhardt.com/wp-content/uploads/2010/04/adapters.png" alt="adapters" width="487" height="261" class="aligncenter size-full wp-image-190" /></a></p>
<p>To configure this, Autofac <strong>2.2</strong> essentially lets us switch the composition of <code class="codecolorer text default"><span class="text">ToolbarButton</span></code> from top-down to bottom-up. First we find all of the <code class="codecolorer text default"><span class="text">ICommand</span></code> implementations, and then we construct a <code class="codecolorer text default"><span class="text">ToolbarButton</span></code> for each.</p>
<p>Configuration of the container is the same as before, except we change:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">builder.<span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&lt;</span>ToolbarButton<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>to:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">builder.<span style="color: #0000FF;">RegisterAdapter</span><span style="color: #008000;">&lt;</span>ICommand, ToolbarButton<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>cmd <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> ToolbarButton<span style="color: #000000;">&#40;</span>cmd<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>The <code class="codecolorer text default"><span class="text">RegisterAdapter()</span></code> method takes two types as parameters: the service to adapt <em>from</em>, and the component type to adapt <em>to</em>.</p>
<p>Autowiring isn’t supported for the adapter component in this scenario (yet), so we include a lambda expression describing how the adapter is constructed given one of the adapted instances.</p>
<p>Overloads of <code class="codecolorer text default"><span class="text">RegisterAdapter()</span></code> exist for passing in parameters and an <code class="codecolorer text default"><span class="text">IComponentContext</span></code> from which the adapter&#8217;s other dependencies can be resolved if necessary.</p>
<p><em>Aside: <a href="http://twitter.com/joshuamck">@joshuamck</a> suggested an alternative syntax that I&#8217;d also considered, along the lines of <code class="codecolorer csharp default"><span class="csharp">builder.<span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&lt;</span>ToolbarButton<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Adapting</span><span style="color: #008000;">&lt;</span>ICommand<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></span></code>. I think this is more in line with the intent of Autofac&#8217;s builder syntax, so depending on how the implementation goes we might see this in the 2.2 release version.</em></p>
<h3>Composing with Metadata</h3>
<p>In the example, commands can be given metadata to allow the command name to be displayed on the button.</p>
<p>First the commands are given names:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">builder.<span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&lt;</span>SaveCommand<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0600FF;">As</span><span style="color: #008000;">&lt;</span>ICommand<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0000FF;">WithMetadata</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Name&quot;</span>, <span style="color: #666666;">&quot;Save File&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
builder.<span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&lt;</span>OpenCommand<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0600FF;">As</span><span style="color: #008000;">&lt;</span>ICommand<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0000FF;">WithMetadata</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Name&quot;</span>, <span style="color: #666666;">&quot;Open File&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Then, the name parameter is added to the ToolbarButton constructor and the metadata is consumed:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">builder.<span style="color: #0000FF;">RegisterAdapter</span><span style="color: #008000;">&lt;</span>Meta<span style="color: #008000;">&lt;</span>ICommand<span style="color: #008000;">&gt;</span>, ToolbarButton<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>cmd <span style="color: #008000;">=&gt;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">new</span> ToolbarButton<span style="color: #000000;">&#40;</span>cmd.<span style="color: #0000FF;">Value</span>, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>cmd.<span style="color: #0000FF;">Metadata</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Name&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Adapters, like other relationship types in Autofac, compose nicely with each other as the use of <code class="codecolorer text default"><span class="text">Meta&lt;T&gt;</span></code> shows.</p>
<h3>Wrapping Up*</h3>
<p><em>*Excuse this terrible pun.</em></p>
<p>Lightweight adapters are a nice little helper coming in Autofac 2.2. While these scenarios don’t arise every day, when they do, container support is very convenient.</p>
<p>Download an example (including an Autofac 2.2 preview build) <a href='http://nblumhardt.com/wp-content/uploads/2010/04/Autofac2-LightweightAdapterDemo.zip'>here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nblumhardt.com/2010/04/lightweight-adaptation-%e2%80%93-coming-soon/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Autofac 2.1 on Talking Shop Down Under</title>
		<link>http://nblumhardt.com/2010/04/autofac-2-1-on-talking-shop-down-under/</link>
		<comments>http://nblumhardt.com/2010/04/autofac-2-1-on-talking-shop-down-under/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 21:53:22 +0000</pubDate>
		<dc:creator>Nicholas Blumhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nblumhardt.com/?p=178</guid>
		<description><![CDATA[Richard Banks was kind enough to interview me on Episode 8 of Talking Shop Down Under. If you&#8217;re interested in some of the ideas behind the new Autofac 2.1 release, be sure to check out the podcast!]]></description>
			<content:encoded><![CDATA[<p><a href="http://richardsbraindump.blogspot.com/">Richard Banks</a> was kind enough to interview me on <a href="http://www.talkingshopdownunder.com/2010/04/episode-8-nick-blumhardt-and-autofac-20.html">Episode 8 of Talking Shop Down Under</a>.</p>
<p>If you&#8217;re interested in some of the ideas behind the new <a href="http://nblumhardt.com/2010/04/introducing-autofac-2-1-rtw/">Autofac 2.1 release</a>, be sure to check out the podcast!</p>
]]></content:encoded>
			<wfw:commentRss>http://nblumhardt.com/2010/04/autofac-2-1-on-talking-shop-down-under/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing Autofac 2.1 RTW</title>
		<link>http://nblumhardt.com/2010/04/introducing-autofac-2-1-rtw/</link>
		<comments>http://nblumhardt.com/2010/04/introducing-autofac-2-1-rtw/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 21:34:09 +0000</pubDate>
		<dc:creator>Nicholas Blumhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nblumhardt.com/?p=151</guid>
		<description><![CDATA[After nearly two years of experimentation, design and development, Autofac&#8217;s second major release is here! Autofac 2.1 is still the IoC container you know and love, but reorganised so that Autofac 1.4&#8242;s strengths &#8212; especially in providing a low-friction developer experience &#8212; really shine. You can download the binaries here, or read on for some [...]]]></description>
			<content:encoded><![CDATA[<p>After nearly two years of experimentation, design and development, Autofac&#8217;s second major release is here!</p>
<p>Autofac 2.1 is still the IoC container you know and love, but reorganised so that Autofac 1.4&#8242;s strengths &#8212; especially in providing a low-friction developer experience &#8212; really shine.</p>
<p>You can download the binaries <a href="http://autofac.org">here</a>, or read on for some of the feature highlights.</p>
<h2>Component Discovery</h2>
<p>Otherwise known as convention-driven registration or scanning, Autofac 2 can register a set of types from an assembly according to user-specified rules:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var dataAccess <span style="color: #008000;">=</span> Assembly.<span style="color: #0000FF;">GetExecutingAssembly</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
builder.<span style="color: #0000FF;">RegisterAssemblyTypes</span><span style="color: #000000;">&#40;</span>dataAccess<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>t <span style="color: #008000;">=&gt;</span> t.<span style="color: #0000FF;">Name</span>.<span style="color: #0000FF;">EndsWith</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Repository&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0000FF;">AsImplementedInterfaces</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>The registration syntax for <code class="codecolorer csharp default"><span class="csharp">RegisterAssemblyTypes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></span></code> is a superset of the registration syntax for single types, so methods like <code class="codecolorer csharp default"><span class="csharp"><span style="color: #0600FF;">As</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></span></code> all work with assemblies as well, and there&#8217;s very little extra API to learn.</p>
<h2>Relationship Types</h2>
<p>When working with IoC you frequently hear advice against passing the container around or resolving components from it directly. Where dynamic relationships are concerned, for example deferred creation, selection from alternatives or parameterisation, there has historically been very little guidance on the alternatives.</p>
<p>Autofac addresses this by automatically supporting small, focused, strongly-typed wrappers that express dynamic dependencies.</p>
<p>For example, instead of calling <code class="codecolorer text default"><span class="text">IContainer.Resolve&lt;IDownloader&gt;()</span></code>, a <code class="codecolorer text default"><span class="text">WebCrawler</span></code> component that needs to create instances of a downloader on-the-fly can take a dependency on <code class="codecolorer text default"><span class="text">Func&lt;IDownloader&gt;</span></code> and the container will provide it automatically so long as <code class="codecolorer text default"><span class="text">IDownloader</span></code> is registered.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #FF0000;">class</span> WebCrawler <span style="color: #008000;">:</span> IWebCrawler<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Func<span style="color: #008000;">&lt;</span>Uri, IDownloader<span style="color: #008000;">&gt;</span> _downloaderFactory<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> WebCrawler <span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>Uri, IDownloader<span style="color: #008000;">&gt;</span> downloaderFactory<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _downloaderFactory <span style="color: #008000;">=</span> downloaderFactory<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Crawl<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var downloader <span style="color: #008000;">=</span> _downloaderFactory<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Uri<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;http://autofac.org&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var link <span style="color: #0600FF;">in</span> downloader.<span style="color: #0000FF;">OutboundLinks</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// ...</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>As the example shows, you can pass parameters (in this case a Uri) that will be forwarded to the target component&#8217;s constructor.</p>
<p>Autofac 2 supports an extensive vocabulary of <em>relationship types</em> that the container understands and provides automatically based on the other available components.</p>
<table>
<tr>
<th>Relationship</th>
<th>Adapter Type</th>
<th>Meaning</th>
</th>
<tr>
<td><em>A</em> needs a <em>B</em></td>
<td><em>None</em></td>
<td>Dependency</td>
</tr>
<tr>
<td><em>A</em> needs a <em>B</em> at some point in the future</td>
<td><code class="codecolorer text default"><span class="text">Lazy&lt;B&gt;</span></code></td>
<td>Delayed instantiation</td>
</tr>
<tr>
<td><em>A needs a <em>B</em> until some point in the future</td>
<td><code class="codecolorer text default"><span class="text">Owned&lt;B&gt;</span></code></td>
<td>Controlled lifetime</td>
</tr>
<tr>
<td><em>A</em> needs to create instances of <em>B</em></td>
<td><code class="codecolorer text default"><span class="text">Func&lt;B&gt;</span></code></td>
<td>Dynamic instantiation</td>
</tr>
<tr>
<td><em>A</em> provides parameters of types <em>X</em> and <em>Y</em> to <em>B</em></td>
<td><code class="codecolorer text default"><span class="text">Func&lt;X,Y,B&gt;</span></code></td>
<td>Parameterisation</td>
</tr>
<tr>
<td><em>A</em> needs all the kinds of <em>B</em></td>
<td><code class="codecolorer text default"><span class="text">IEnumerable&lt;B&gt;</span></code></td>
<td>Enumeration</td>
</tr>
<tr>
<td><em>A</em> needs to know <em>X</em> about <em>B</em> before using it</td>
<td><code class="codecolorer text default"><span class="text">Meta&lt;T&gt;</span></code> and <code class="codecolorer text default"><span class="text">Meta&lt;B,X&gt;</span></code></td>
<td>Metadata interrogation</td>
</tr>
</table>
<p>For more information on relationship types, see the <a href="http://nblumhardt.com/2010/01/the-relationship-zoo/">introductory article</a>.</p>
<h2>Component Metadata</h2>
<p>If you&#8217;re familiar with the <a href="http://mef.codeplex.com">Managed Extensibility Framework</a> (MEF) you have probably seen examples using component metadata.</p>
<p>Autofac uses the underlying support in .NET 4.0 to provide similar functionality. Metadata is associated with a component either in code:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">builder.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> ScreenAppender<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0600FF;">As</span><span style="color: #008000;">&lt;</span>ILogAppender<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #0000FF;">WithMetadata</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;AppenderName&quot;</span>, <span style="color: #666666;">&quot;screen&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Or in XML:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;component</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;MyApp.Components.Logging.ScreenAppender, MyApp&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">service</span>=<span style="color: #ff0000;">&quot;MyApp.Services.Logging.ILogAppender, MyApp&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;metadata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AppenderName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;System.String&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/metadata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/component<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>Unlike a regular property, a metadata item can be queried and used without requiring an instance of the component to be created.</p>
<p>This makes it useful when selecting one of many components based on runtime criteria; or, where the metadata isn&#8217;t intrinsic to the component implementation. Metadata could represent the time that an <code class="codecolorer text default"><span class="text">ITask</span></code> should run, or the button caption for an <code class="codecolorer text default"><span class="text">ICommand</span></code>.</p>
<p>Other components can consume metadata using the <code class="codecolorer text default"><span class="text">System.Lazy&lt;T,TMetadata&gt;</span></code> type.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Log<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">readonly</span> IEnumerable<span style="color: #008000;">&lt;</span>Lazy<span style="color: #008000;">&lt;</span>ILogAppender, ILogAppenderMetadata<span style="color: #008000;">&gt;&gt;</span> _appenders<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> Log<span style="color: #000000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span>Lazy<span style="color: #008000;">&lt;</span>ILogAppender, ILogAppenderMetadata<span style="color: #008000;">&gt;&gt;</span> appenders<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _appenders <span style="color: #008000;">=</span> appenders<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Write<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> destination, <span style="color: #FF0000;">string</span> message<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var appender <span style="color: #008000;">=</span> _appenders.<span style="color: #0000FF;">First</span><span style="color: #000000;">&#40;</span>a <span style="color: #008000;">=&gt;</span> a.<span style="color: #0000FF;">Metadata</span>.<span style="color: #0000FF;">AppenderName</span> <span style="color: #008000;">==</span> destination<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; appender.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>message<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>In .NET 3.5 (as well as 4.0) Autofac 2 provides the weakly typed <code class="codecolorer text default"><span class="text">Meta&lt;T&gt;</span></code> class for consuming metadata as a dictionary.</p>
<h2>Managed Extensibilty Framework Integration</h2>
<p>MEF introduces a standard API for creating extensible applications in .NET 4.0. If your Autofac-based application has extensibility points, plugin developers can use the MEF attributes to mark up their extensions, while internally they can be hosted in the Autofac container just like any other Autofac component.</p>
<p>Any MEF catalog type can be registered directly with the container, so to use MEF&#8217;s directory scanning ability for example, a <code class="codecolorer text default"><span class="text">System.ComponentModel.Composition.DirectoryCatalog</span></code> can be used.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var catalog <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DirectoryCatalog<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Extensions&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
builder.<span style="color: #0000FF;">RegisterComposablePartCatalog</span><span style="color: #000000;">&#40;</span>catalog<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>For documentation on this API see the <a href="http://code.google.com/p/autofac/wiki/MefIntegration">Autofac MEF integration wiki page</a>.</p>
<p>There&#8217;s a deeper discussion of the underlying architecture <a href="http://blogs.msdn.com/nblumhardt/archive/2009/03/16/hosting-mef-extensions-in-an-ioc-container.aspx">here</a>.</p>
<h2>Other Improvements</h2>
<p>Although Autofac 2 introduces some new features, a lot more work went into making the core architecture and API of Autofac more consistent, robust and explicit.</p>
<p>To list a very small selection:</p>
<ul>
<li>Generic type constraints are now respected &#8211; Autofac won&#8217;t try to use unsuitable generic types when resolving references to generic services.</li>
<li>The instance parameters to activation events are strongly-typed, for example <code class="codecolorer text default"><span class="text">builder.RegisterType&lt;Foo&gt;().OnActivating(e =&gt; e.Instance.Start())</span></code>.</li>
<li>ASP.NET MVC <a href="http://nblumhardt.com/2010/01/mvc-integration-changes-in-autofac-beta-2-1-6/">controller registration</a> is more flexible and simpler via the <code class="codecolorer text default"><span class="text">RegisterControllers()</span></code> method.</li>
<li>Non-shared (&#8216;factory&#8217;) scope is the default, rather than Singleton.</li>
<li><a href="http://nblumhardt.com/2010/01/resolve-anything/">&#8220;Resolve Anything&#8221;</a> support
<li>API documentation is <a href="http://www.google.com.au/search?q=site:api.autofac.org+ILifetimeScope">seachable on Google</a>.</li>
</ul>
<h2>Upgrading from Autofac 1.4</h2>
<p>If you&#8217;re upgrading from an earlier Autofac release, see the <a href="http://code.google.com/p/autofac/wiki/NewInV2">New in V2</a> wiki page for more detailed information.</p>
<p>There are many breaking changes for users of Autofac 1.4 who want to upgrade. Most of these are just name changes, but some less commonly-used features will cause non-trivial rework. This is the price of keeping Autofac clean, supportable and relevant &#8211; if you&#8217;re affected by breaking changes you can find consolation in knowing that Autofac is not on the path to becoming a legacy framework <img src='http://nblumhardt.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>If things do get tough, help is only ever <a href="http://groups.google.com/group/autofac">an email away</a>.</p>
<h2>Where to Next?</h2>
<p>The 2.1 release is largely <em>foundational</em> &#8212; its focus is on improving the core container. There are plenty of new features in it, but next in the pipeline is a 2.2 version including, for example, <a href="http://groups.google.com/group/autofac/browse_thread/thread/cc714738899ea200">broader ASP.NET MVC support</a> and no doubt some fixes and improvements based on learnings from 2.1.</p>
<h2>Acknowledgements and Thanks</h2>
<p>Autofac 2 draws on many inspirations and input from a large community. Direct credit for Autofac 2 belongs to the <a href="http://code.google.com/p/autofac/people/list">project members</a>, contributors to the <a href="http://groups.google.com/group/autofac">mailing list</a>, and supporters in the <a href="http://blogsearch.google.com/blogsearch?hl=en&#038;ie=UTF-8&#038;q=autofac&#038;btnG=Search+Blogs">blogosphere</a>, on <a href="http://stackoverflow.com/search?q=autofac">stackoverflow</a> and on <a href="http://twitter.com/#search?q=%23Autofac">Twitter</a>. </p>
<p>Many of the ideas in Autofac 2, just as with the version before it, derive from other projects like <a href="http://castleproject.org">Windsor</a>, <a href="http://mef.codeplex.com">MEF</a>, <a href="http://funq.me">Funq</a>, <a href="http://unity.codeplex.com">Unity</a>, <a href="http://ninject.org">Ninject</a>, <a href="http://structuremap.sourceforge.net">StructureMap</a> and their precursors.</p>
<p>Extra thanks is due to the MEF team, whose tireless work is making the .NET a friendlier place for IoC every day.</p>
]]></content:encoded>
			<wfw:commentRss>http://nblumhardt.com/2010/04/introducing-autofac-2-1-rtw/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Must-Have Tablet for 2010</title>
		<link>http://nblumhardt.com/2010/04/the-must-have-tablet-for-2010/</link>
		<comments>http://nblumhardt.com/2010/04/the-must-have-tablet-for-2010/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 21:27:07 +0000</pubDate>
		<dc:creator>Nicholas Blumhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nblumhardt.com/?p=154</guid>
		<description><![CDATA[If you&#8217;re a fan of index cards or Post-it notes for work item tracking, here&#8217;s another simple helper you might enjoy. The Quartet Cube is a tiny, portable metallic whiteboard. On our recent project we&#8217;ve kept three in the project room and found them perfect for: Taking notes during meetings Small focused to-do lists (we [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_159" class="wp-caption alignright" style="width: 310px"><a href="http://nblumhardt.com/wp-content/uploads/2010/04/tablet-300px.JPG"><img src="http://nblumhardt.com/wp-content/uploads/2010/04/tablet-300px.JPG" alt="Quartet Cube" title="Quartet Cube" width="300" height="225" class="size-full wp-image-159" /></a><p class="wp-caption-text">Quartet Cube</p></div>
<p>If you&#8217;re a fan of index cards or <a href="http://www.3m.com/us/office/postit/products/prod_notes.html?WT.ac=TopNav-Products-Notes">Post-it</a> notes for work item tracking, here&#8217;s another simple helper you might enjoy.</p>
<p>The <a href="http://www.officeworks.com.au/retail/products/Office-Supplies/Conference-and-Presentation/Boards-and-Easels/Whiteboards/Magnetic-Whiteboards/PEQTTSQ12B">Quartet Cube</a> is a tiny, portable metallic whiteboard. </p>
<p>On our recent project we&#8217;ve kept three in the project room and found them perfect for:</p>
<ul>
<li>Taking notes during meetings</li>
<li>Small focused to-do lists (we often use a pink one to track things we&#8217;d like to refactor)</li>
<li>Sketching ideas</li>
<li>Communicating designs (as demonstrated in the photo by the remarkable <a href="http://azure.snagy.name/blog/">Steven Nagy</a>)</li>
</ul>
<p>The added portability makes it easy to take one to your desk &#8211; or to somebody else&#8217;s.</p>
]]></content:encoded>
			<wfw:commentRss>http://nblumhardt.com/2010/04/the-must-have-tablet-for-2010/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Autofac 2.1 goes RC (the &#8220;me too!&#8221; release)</title>
		<link>http://nblumhardt.com/2010/02/autofac-2-1-goes-rc-the-me-too-release/</link>
		<comments>http://nblumhardt.com/2010/02/autofac-2-1-goes-rc-the-me-too-release/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 13:07:14 +0000</pubDate>
		<dc:creator>Nicholas Blumhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nblumhardt.com/?p=146</guid>
		<description><![CDATA[Today brings a new build of Autofac, now designated &#8220;Release Candidate&#8221;. Autofac 2.1.9 includes the last of the critical changes for a full release. Since 2.1.8: Many bug fixes and small usability enhancements BeginLifetimeScope() now allows additional components to be configured for the child scope An AutofacContrib build for both .NET 3.5 and 4.0 is [...]]]></description>
			<content:encoded><![CDATA[<p>Today brings a new build of Autofac, now designated &#8220;Release Candidate&#8221;.</p>
<p>Autofac 2.1.9 includes the last of the critical changes for a full release. Since 2.1.8:</p>
<ul>
<li>Many bug fixes and small usability enhancements</li>
<li><code class="codecolorer text default"><span class="text">BeginLifetimeScope()</span></code> now allows additional components to be configured for the child scope</li>
<li>An AutofacContrib build for both .NET 3.5 and 4.0 is available (AutofacContrib.DynamicProxy2 not yet included)</li>
<li>Two new adapters for <a href="http://nblumhardt.com/2010/01/the-relationship-zoo/">the zoo</a>:
<ul>
<li><code class="codecolorer text default"><span class="text">Indexed&lt;TKey,TValue&gt;</span></code> for dictionary-like dependencies</li>
<li><code class="codecolorer text default"><span class="text">Meta&lt;T&gt;</span></code> for un-typed metadata, available on .NET 3.5</li>
</ul>
</li>
<li>API documentation can be viewed online at <a href="http://api.autofac.org">api.autofac.org</a> (this reflects the .NET 3.5-compatible version.)</li>
</ul>
<p>Autofac 2.1 RC is now the recommended version for new projects. Please bear with us while the wiki is updated to reflect the new API.</p>
<p>Many thanks are due to the contributors and user community who have made this release possible, as well as my teammates at <a href="http://readify.net">Readify</a> who have put up with a bombardment of Autofac 2 material in the last few months <img src='http://nblumhardt.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Get the downloads for .NET 3.5 and .NET 4.0 <a href="http://autofac.org">here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nblumhardt.com/2010/02/autofac-2-1-goes-rc-the-me-too-release/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Relationship Zoo</title>
		<link>http://nblumhardt.com/2010/01/the-relationship-zoo/</link>
		<comments>http://nblumhardt.com/2010/01/the-relationship-zoo/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 12:11:25 +0000</pubDate>
		<dc:creator>Nicholas Blumhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nblumhardt.com/?p=112</guid>
		<description><![CDATA[Dependency Injection, unsurprisingly, is all about the relationships between components. Components rarely exist or perform any useful task alone. A system that achieves a business function may use tens, hundreds or even thousands of different components, all working in collaboration to get a job done. Early IoC containers understood one kind of relationship: &#8220;A needs [...]]]></description>
			<content:encoded><![CDATA[<p>Dependency Injection, unsurprisingly, is all about the relationships between components.</p>
<p>Components rarely exist or perform any useful task alone. A system that achieves a business function may use tens, hundreds or even thousands of different components, all working in collaboration to get a job done.</p>
<p>Early IoC containers understood one kind of relationship: &#8220;<em>A</em> needs a <em>B</em>.&#8221;</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #FF0000;">interface</span> B <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #FF0000;">class</span> A<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> A<span style="color: #000000;">&#40;</span>B b<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> ... <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>More complex relationships, like &#8220;<em>A</em> creates instances of <em>B</em>&#8221; required the author of component <em>A</em> to deal directly with the container to get things done, by calling <code class="codecolorer text default"><span class="text">container.Resolve&lt;B&gt;()</span></code>, for example.</p>
<p>Since a majority of relationships are, in fact, simple direct dependencies, this was an acceptable point from which to start developing IoC containers as a technology. Recently, more powerful, declarative ways of specifying &#8220;higher-order&#8221; dependencies have emerged. In this article we&#8217;ll look at a selection of them, using <a href="http://autofac.org">Autofac 2</a> as an example implementation.</p>
<p><em>I was first introduced to the term &#8220;higher-order dependencies&#8221; by Mircea Trofin at Microsoft. I&#8217;m not sure I&#8217;m using it here in precisely the same sense. The .NET Composition Primitives, which Mircea was instrumental in creating, have some elegant mechanisms for handling them &#8211; in a future article I hope to cover this in more detail.</em></p>
<h2>A Catalog of Relationships</h2>
<p>It turns out that there are a lot of different ways that one component may relate to another.</p>
<p><a href="http://nblumhardt.com/wp-content/uploads/2010/01/A-depends-on-B.PNG"><img src="http://nblumhardt.com/wp-content/uploads/2010/01/A-depends-on-B.PNG" alt="A-depends-on-B" title="A-depends-on-B" width="475" height="176" class="alignnone size-full wp-image-113" /></a></p>
<p>Here&#8217;s a partial list to fill in the question mark above:</p>
<ol>
<li><em>A</em> needs a <em>B</em></li>
<li><em>A</em> needs a <em>B</em> at some point in the future</li>
<li><em>A</em> needs a <em>B</em> until some point in the future</li>
<li>
  <em>A</em> needs to create instances of <em>B</em></p>
<ul>
<li>and provides parameters of types <em>X</em> and <em>Y</em></li>
</ul>
</li>
<li><em>A</em> needs all the kinds of <em>B</em></li>
<li><em>A</em> needs to know <em>X</em> about <em>B</em> before using it</li>
<li>&#8230;</li>
</ol>
<p>This list is chosen carefully, because each of these relationships can be expressed declaratively through simple <a href="http://blogs.msdn.com/nblumhardt/archive/2009/01/04/declarative-context-adapters.aspx">adapter types</a>.</p>
<table>
<tr>
<th>Relationship</th>
<th>Adapter Type</th>
<th>Meaning</th>
</th>
<tr>
<td><em>A</em> needs a <em>B</em></td>
<td><em>None</em></td>
<td>Dependency</td>
</tr>
<tr>
<td><em>A</em> needs a <em>B</em> at some point in the future</td>
<td><code class="codecolorer text default"><span class="text">Lazy&lt;B&gt;</span></code></td>
<td>Delayed instantiation</td>
</tr>
<tr>
<td><em>A needs a <em>B</em> until some point in the future</td>
<td><code class="codecolorer text default"><span class="text">Owned&lt;B&gt;</span></code></td>
<td>Controlled lifetime</td>
</tr>
<tr>
<td><em>A</em> needs to create instances of <em>B</em></td>
<td><code class="codecolorer text default"><span class="text">Func&lt;B&gt;</span></code></td>
<td>Dynamic instantiation</td>
</tr>
<tr>
<td><em>A</em> provides parameters of types <em>X</em> and <em>Y</em> to <em>B</em></td>
<td><code class="codecolorer text default"><span class="text">Func&lt;X,Y,B&gt;</span></code></td>
<td>Parameterisation</td>
</tr>
<tr>
<td><em>A</em> needs all the kinds of <em>B</em></td>
<td><code class="codecolorer text default"><span class="text">IEnumerable&lt;B&gt;</span></code></td>
<td>Enumeration</td>
</tr>
<tr>
<td><em>A</em> needs to know <em>X</em> about <em>B</em> before using it</td>
<td><code class="codecolorer text default"><span class="text">Meta&lt;B,X&gt;</span></code></td>
<td>Metadata interrogation</td>
</tr>
</table>
<p>Some of the types above will be familiar: <code class="codecolorer text default"><span class="text">Lazy&lt;T&gt;</span></code>, <code class="codecolorer text default"><span class="text">Func&lt;T&gt;</span></code> and <code class="codecolorer text default"><span class="text">IEnumerable&lt;T&gt;</span></code> are all .NET BCL types, and are used here with their standard semantics.</p>
<p><code class="codecolorer text default"><span class="text">Owned&lt;T&gt;</span></code> and <code class="codecolorer text default"><span class="text">Meta&lt;T&gt;</span></code> are introduced by Autofac, and fill some of the gaps in the BCL. They&#8217;re very simple types that could be replicated in an application that wanted to use them independently.</p>
<h3>Lazy&lt;T&gt;</h3>
<p>A lazy dependency is not instantiated until its first use. This appears where the dependency is infrequently used, or expensive to construct.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #FF0000;">class</span> A<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Lazy<span style="color: #008000;">&lt;</span>B<span style="color: #008000;">&gt;</span> _b<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> A<span style="color: #000000;">&#40;</span>Lazy<span style="color: #008000;">&lt;</span>B<span style="color: #008000;">&gt;</span> b<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> _b <span style="color: #008000;">=</span> b <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> M<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// The component implementing B is created the</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// first time M() is called</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _b.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">DoSomething</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<h3>Owned&lt;T&gt;</h3>
<p>An owned dependency can be released by the owner when it is no longer required. Owned dependencies usually correspond to some unit of work performed by the dependent component.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #FF0000;">class</span> A<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Owned<span style="color: #008000;">&lt;</span>B<span style="color: #008000;">&gt;</span> _b<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> A<span style="color: #000000;">&#40;</span>Owned<span style="color: #008000;">&lt;</span>B<span style="color: #008000;">&gt;</span> b<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> _b <span style="color: #008000;">=</span> b <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> M<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// _b is used for some task</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _b.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">DoSomething</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Here _b is no longer needed, so</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// it is released</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _b.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p><em>In an IoC container, there&#8217;s often a subtle difference between releasing and disposing a component: releasing and owned component goes further than disposing the component itself. Any of the dependencies of the component will also be disposed. Releasing a shared component is usually a no-op, as other components will continue to use its services.</em></p>
<h3>Func&lt;T&gt; and Func&lt;X,Y,T&gt;</h3>
<p>The &#8216;factory&#8217; adapters imply creation of individual instances of the dependency. The parameterised versions allow initialisation data to be passed to the implementing component.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #FF0000;">class</span> A<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span>, <span style="color: #FF0000;">string</span>, B<span style="color: #008000;">&gt;</span> _b<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> A<span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span>, <span style="color: #FF0000;">string</span>, B<span style="color: #008000;">&gt;</span> b<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> _b <span style="color: #008000;">=</span> b <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> M<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var b <span style="color: #008000;">=</span> _b<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">42</span>, <span style="color: #666666;">&quot;http://hel.owr.ld&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; b.<span style="color: #0000FF;">DoSomething</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<h3>IEnumerable&lt;T&gt;</h3>
<p>Dependencies of an enumerable type provide multiple implementations of the same service (interface).</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #FF0000;">class</span> A<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; IEnumerable<span style="color: #008000;">&lt;</span>B<span style="color: #008000;">&gt;</span> _bs<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> A<span style="color: #000000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span>B<span style="color: #008000;">&gt;</span> bs<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> _bs <span style="color: #008000;">=</span> bs <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> M<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var b <span style="color: #0600FF;">in</span> bs<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.<span style="color: #0000FF;">DoSomething</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<h3>Meta&lt;T,M&gt;</h3>
<p>Metadata is data-about-data. Requesting metadata with a dependency allows the dependent component to get infomation about the provider of a service without invoking it.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #FF0000;">class</span> A<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Meta<span style="color: #008000;">&lt;</span>B,BMetadata<span style="color: #008000;">&gt;</span> _b<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> A<span style="color: #000000;">&#40;</span>Meta<span style="color: #008000;">&lt;</span>B,BMetadata<span style="color: #008000;">&gt;</span> b<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> _b <span style="color: #008000;">=</span> b <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> M<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_b.<span style="color: #0000FF;">Metadata</span>.<span style="color: #0000FF;">SomeValue</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;yes&quot;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _b.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">DoSomething</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<h2>The Relationship Zoo</h2>
<p>If you&#8217;ve read this far, you&#8217;re probably desperate for an example that doesn&#8217;t use the components <em>A</em> and <em>B</em>. I feel your pain! The truth is, while these dependencies are useful in their own right, really interesting dependencies often combine multiple adapters!</p>
<h3>Choosing a File Viewer</h3>
<p>Metadata is excellent when there are multiple implementations of a service, and one must be selected to perform a task. The selection criterion mightn&#8217;t be inherent in the component itself, so properties make awkward choices for representing this data.</p>
<p>Let&#8217;s say we have a number of file viewers, and, via configuration, we associate each viewer with the file types that it should be used to view.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #FF0000;">interface</span> IViewer<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">void</span> View<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> filename<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #FF0000;">interface</span> IViewerMetadata<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> FileTypes <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #FF0000;">class</span> Browser<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; IEnumerable<span style="color: #008000;">&lt;</span>Meta<span style="color: #008000;">&lt;</span>IViewer, IViewerMetadata<span style="color: #008000;">&gt;&gt;</span> _viewers<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> Browser<span style="color: #000000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span>Meta<span style="color: #008000;">&lt;</span>IViewer, IViewerMetadata<span style="color: #008000;">&gt;&gt;</span> viewers<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _viewers <span style="color: #008000;">=</span> viewers<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> OnViewFile<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> filename<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var ext <span style="color: #008000;">=</span> Path.<span style="color: #0000FF;">GetExtension</span><span style="color: #000000;">&#40;</span>filename<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var viewer <span style="color: #008000;">=</span> _viewers.<span style="color: #0000FF;">Single</span><span style="color: #000000;">&#40;</span>v <span style="color: #008000;">=&gt;</span> v.<span style="color: #0000FF;">FileTypes</span>.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>ext<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; viewer.<span style="color: #0000FF;">View</span><span style="color: #000000;">&#40;</span>filename<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>In this example, <code class="codecolorer text default"><span class="text">IEnumerable&lt;T&gt;</span></code> and <code class="codecolorer text default"><span class="text">Meta&lt;T&gt;</span></code> are combined to express the two features of the relationship between <code class="codecolorer text default"><span class="text">Browser</span></code> and <code class="codecolorer text default"><span class="text">IViewer</span></code>.</p>
<p>Mixing in dynamic creation, controlled lifetime or lazy initialization would often also make sense in this kind of scenario.</p>
<p>You can see how metadata is associated with an Autofac component <a href="http://nblumhardt.com/2009/12/lazing-around-with-autofac2/">here</a>, or examine one of the many examples based on the MEF APIs <a href="http://mef.codeplex.com/wikipage?title=Exports%20and%20Metadata">here</a>.</p>
<h3>Creating and Releasing a Message Handler</h3>
<p>Another common combination is that of dynamic instantiation and ownership of the instances. A message pump may create, use, then release a handler for each incoming message:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #FF0000;">interface</span> IMessageHandler<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">void</span> Handle<span style="color: #000000;">&#40;</span>Message message<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #FF0000;">class</span> MessagePump<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Func<span style="color: #008000;">&lt;</span>Owned<span style="color: #008000;">&lt;</span>IMessageHandler<span style="color: #008000;">&gt;&gt;</span> _handlerFactory<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> MessagePump<span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>Owned<span style="color: #008000;">&lt;</span>IMessageHandler<span style="color: #008000;">&gt;&gt;</span> handlerFactory<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _handlerFactory <span style="color: #008000;">=</span> handlerFactory<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Go<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var message <span style="color: #008000;">=</span> NextMessage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var handler <span style="color: #008000;">=</span> _handlerFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">Handle</span><span style="color: #000000;">&#40;</span>message<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<h3>Many instances, with dynamic creation and controlled lifetime and parameters and and and whoah!</h3>
<p>Yes, <code class="codecolorer text default"><span class="text">IEnumerable&lt;Meta&lt;Func&lt;X,Y,Owned&lt;T&gt;&gt;,M&gt;&gt;</span></code> is in fact a valid relationship type. Glad you asked?</p>
<p>Common combinations can be eaiser to work with when they are baked into a single class. In MEF, for example, <code class="codecolorer text default"><span class="text">Meta&lt;Lazy&lt;T&gt;&gt;</span></code> is represented as <code class="codecolorer text default"><span class="text">Lazy&lt;T,M&gt;</span></code>. Likewise <code class="codecolorer text default"><span class="text">Func&lt;Owned&lt;T&gt;&gt;</span></code> is represented as <code class="codecolorer text default"><span class="text">PartCreator&lt;T&gt;</span></code>. You can plug these composite adapter types into Autofac via a custom registration source if you need them.</p>
<p>Custom delegate types can also ease angle-bracket-eyestrain if you use the parameterised factories a lot.</p>
<p>The core of Autofac 2 doesn&#8217;t have any special knowledge of any of these adapter types. They&#8217;re all plugged in as <a href="http://nblumhardt.com/2010/01/declarative-context-adapters-autofac2/">IRegistrationSource implementations</a>, meaning that you are free to change or extend the set of adapter types as your needs dictate.</p>
<h2>Muahahahahhahahaahhh!!!!</h2>
<p>I heard that maniac laugh just then! Let me just slip in the disclaimer now: just because you <em>can</em> model almost any conceivable relationship type this way, doesn&#8217;t mean that you should. Where a simple direct dependency will suffice, don&#8217;t gouge out your eyes on six layers of nested angle brackets, or invest in new adapter types. Like all sharp tools, use context adapters with discretion.</p>
<p>At the same time, there&#8217;s practically no excuse to use <code class="codecolorer text default"><span class="text">IContainer</span></code> or <code class="codecolorer text default"><span class="text">IComponentContext</span></code> in your components anymore.</p>
<h2>Conclusions and a Suggestion</h2>
<p>You might not be using Autofac today, and might not have any desire to. That&#8217;s okay! Anything new and useful you see here will no doubt make its way back into the container you prefer &#8211; this is the magic of being part of an Open Source community.</p>
<p>If you do use Autofac, or are curious, download the .NET 4.0 version from <a href="http://autofac.org">the project site</a>.</p>
<p><strong>A note to container developers&#8230;</strong></p>
<p>Although this is an Autofac-centric article, it draws on the combined experience of many passionate people who are driving .NET composition technology forward.</p>
<p>I&#8217;m not sure yet that we share a common vision of the &#8220;v.Next&#8221; IoC container &#8211; one thing we do seem to agree on is that it is harmful to have the container abstraction leak through into components. Perhaps in the future, IoC containers will be completely transparent, but we have some way yet to go.</p>
<p><a href="http://codeplex.com/commonservicelocator">Common Service Locator</a> (CSL) was a step forward in interoperability. A good range of applications and frameworks that need dynamic or lazy instantiation no longer have to depend on a specific IoC container; I think this is one way we&#8217;ve maintained diversity and a healthy ecosystem.</p>
<p>CSL is a stopgap though &#8211; a service locator that could handle all of the scenarios above would be a monster of an interface indeed! CSL also breaks the declarative nature of components that use it.</p>
<p>I&#8217;d be very interested to hear thoughts on a new project, <em>Common Context Adapters</em>, which would include types like <code class="codecolorer text default"><span class="text">Owned&lt;T&gt;</span></code> and <code class="codecolorer text default"><span class="text">Meta&lt;T,M&gt;</span></code> to fill gaps in the BCL while it evolves. It would also provide baseline documentation on how wiring of <code class="codecolorer text default"><span class="text">Func&lt;T&gt;</span></code> etc. should work.</p>
<p><em>Common Context Adapters</em> would play a similar role to CSL by isolating applications and frameworks from the specifics of an IoC container, but in a way that is compatible with declarative dependency wiring and inversion of control.</p>
<p>I&#8217;m prepared to put effort into making this happen if there is enough interest.</p>
]]></content:encoded>
			<wfw:commentRss>http://nblumhardt.com/2010/01/the-relationship-zoo/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Building an External DSL in C#</title>
		<link>http://nblumhardt.com/2010/01/building-an-external-dsl-in-c/</link>
		<comments>http://nblumhardt.com/2010/01/building-an-external-dsl-in-c/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 14:12:51 +0000</pubDate>
		<dc:creator>Nicholas Blumhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nblumhardt.com/?p=95</guid>
		<description><![CDATA[Download the example as a VS2010 solution. Our current project includes a small workflow for submission and approval of user account creation requests. This makes a good example to discuss domain-specific languages and Sprache, so I&#8217;ll paraphrase some of the requirements here. The set of user account types is open-ended; currently “employee”, “contractor”, “temporary” and [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://nblumhardt.com/wp-content/uploads/2010/01/Questionnaire-DSL-Example.zip'>Download the example as a VS2010 solution.</a></p>
<p>Our current project includes a small workflow for submission and approval of user account creation requests. This makes a good example to discuss domain-specific languages and <a href="http://sprache.googlecode.com">Sprache</a>, so I&#8217;ll paraphrase some of the requirements here.</p>
<p>The set of user account types is open-ended; currently “employee”, “contractor”, “temporary” and so on. To request a particular kind of account, a user must fill in a questionnaire associated with that account type.</p>
<p>In the data collection and approval parts of the system, the content of the questionnaire is irrelevant so long as the necessary information is recorded and presented to the administrator, who will ultimately approve or decline the request.</p>
<h2>The Challenge</h2>
<p>Much of the system design is driven by the fact that the set of possible account types (and thus different questionnaires) is open. Creating new questionnaires should be possible without redeploying the application. Furthermore, the content of a particular questionnaire needs to be easily extended and modified, possibly by end-users themselves.</p>
<p>There are many possible ways to represent the questionnaires:</p>
<ul>
<li>Map a questionnaire domain model to relational database tables</li>
<li>Create an XML-based questionnaire format</li>
<li>Use Windows Workflow Foundation with loose XAML files</li>
<li>Read the questionnaires from CSV files or spreadsheets</li>
</ul>
<p>Each has its pros and cons with regard to ease of implementation, maintainability, user-friendliness and flexibility.</p>
<p>In this article, we&#8217;ll examine another appealing option: building a user-friendly mini-language to represent questionnaires.</p>
<h2>Questionnaire Definition Language</h2>
<p>You may have read some discussion of the differences between internal and external DSLs.</p>
<p>An <em>internal DSL</em> is a specially-constructed API in a general-purpose language like C# that, when used, reads <a href="http://stateless.googlecode.com">more like a definition of the problem</a> than a program to solve it.</p>
<p>An <em>external DSL</em> is a standalone language that must be parsed from source text before a program can work with it. Importantly for this example, an external DSL encourages a minimum of syntactic noise, and can be read without any program compilation.</p>
<p>The example questionnaire DSL looks like:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">identification&nbsp; &quot;Personal Details&quot;<br />
[<br />
&nbsp; &nbsp; name&nbsp; &nbsp; &nbsp; &nbsp; &quot;Full Name&quot;<br />
&nbsp; &nbsp; department&nbsp; &quot;Department&quot;<br />
]<br />
<br />
employment&nbsp; &nbsp; &nbsp; &quot;Current Employer&quot;<br />
[<br />
&nbsp; &nbsp; name&nbsp; &nbsp; &nbsp; &nbsp; &quot;Your Employer&quot;<br />
&nbsp; &nbsp; contact &nbsp; &nbsp; &quot;Contact Number&quot;<br />
&nbsp; &nbsp; #months &nbsp; &nbsp; &quot;Total Months Employed&quot;<br />
]</div></div>
<p>Above is a two-step questionnaire that collects personal and employment details.</p>
<ul>
<li>Each section has an id, a title, and a list of questions.</li>
<li>Each question has an id and some prompting text.</li>
<li>The declaration of a question id (e.g. #months) can be prefixed with a symbol to indicate what kind of data is being collected – the pound sign/hash in this case denotes a natural number.</li>
</ul>
<p>The application will read the questionnaire definition associated with a particular kind of account request, and present the steps to the user in a wizard-like interface.</p>
<h2>Approaches to Parsing the Questionnaire Definitions</h2>
<p>Parsing is the process of taking text in a source language, like the questionnaire above, and converting it into a representation &#8211; usually some kind of object model &#8211; that a program can work with. For the C# programmer, there are several ways to achieve this.</p>
<h3>Hand-built Parsers</h3>
<p>The very simplest as well as the most complex parsers are often built by hand. The simple ones because the solution is readily evident to the programmer (e.g. “split the string by finding commas in a loop”) and the most complex because the programmer needs an extreme level of control (e.g. the C# compiler.) Parsing anything in between by hand is usually more trouble than it is worth, unless you&#8217;re really into languages and know what you&#8217;re doing (that definitely rules me out!)</p>
<h3>Regular Expressions</h3>
<p>These are a convenient way of matching and extracting patterns from text. .NET includes the built-in <code class="codecolorer text default"><span class="text">System.Text.Regex</span></code> class for working efficiently with regular expressions, so they&#8217;re usually the first option to consider when faced with a parsing task. Beyond quite simple grammars, regular expressions rapidly become hard to read and maintain. This is perhaps the biggest of their shortcomings, but there are also many grammars that regular expressions are unable to parse (starting with those that allow nesting.)</p>
<h3>Parser Construction Toolkits</h3>
<p>Industrial-strength parser construction toolkits and &#8216;language workbenches&#8217; allow a grammar to be specified in a declarative format. The toolkit includes build-time tools to generate classes in the target programming language (e.g. C#) that can parse the grammar. Using such a toolkit requires an investment in both learning how the toolkit works, and integrating it into a project&#8217;s build process. For small parsing tasks this can be overkill, but for anything of significant complexity or requiring high parsing throughput, learning and using to use such a toolkit is highly recommended.</p>
<h3>Parser Combinators</h3>
<p>This function-based technique is often used by default in functional languages like Haskell and F#, both of which have high-quality parser combinator libraries. C# also has a fledgling combinator library called <a href="http://sprache.googlecode.com">Sprache</a>, built by yours truly and used in the rest of the article. Sprache makes it very easy to write and maintain simple parsers, without a steep learning curve or build-time tasks. It fits very nicely with a test-driven development process. The drawbacks are currently speed, efficiency and sometimes the quality of error messages – none of which is a big deal when parsing simple DSLs.</p>
<h2>Getting Started</h2>
<p>First, <a href="http://sprache.googlecode.com">download Sprache.dll</a>. This article is organised in such a way that you should be able to follow along with the text by creating and testing parsers in Visual Studio with <a href="http://nunit.org">NUnit</a> or similar.</p>
<p>Grammars are built from the bottom up. First, parsers are created for the low-level syntax elements like identifiers, strings and numbers. We then work upwards, combining these basic units into more complex ones until we have the complete language.</p>
<h2>Parsing an Identifier</h2>
<p>In our questionnaire definition language, the most-nested significant element is the question:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">name&nbsp; &nbsp; &quot;Full Name&quot;</div></div>
<p>The basic parts here are an identifier an some quoted text. The first task for our parser will be to parse the identifier, in this case &#8216;name&#8217;.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AnIdentifierIsASequenceOfCharacters<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; var input <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;name&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var id <span style="color: #008000;">=</span> QuestionnaireGrammar.<span style="color: #0000FF;">Identifier</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>input<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;name&quot;</span>, id<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Parsers in Sprache are static methods on a class representing the grammar. <code class="codecolorer text default"><span class="text">QuestionnaireGrammar.Identifier</span></code> is of type <code class="codecolorer text default"><span class="text">Parser&lt;string&gt;</span></code>, i.e. it returns values of type <code class="codecolorer text default"><span class="text">string</span></code> from the input.</p>
<p>The definition of the parser is:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> Parser<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> Identifier <span style="color: #008000;">=</span> Parse.<span style="color: #0000FF;">Letter</span>.<span style="color: #0000FF;">AtLeastOnce</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Text</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Token</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>This reads fairly well – we&#8217;re going to parse a non-empty sequence of letters and return the text representation of those letters. The elements of the parser here are:</p>
<p><strong>Parse.Letter</strong> – the Sprache Parse class contains helper methods and properties for common parsing tasks. Letter is a simple parser of type <code class="codecolorer text default"><span class="text">Parser&lt;char&gt;</span></code>, that reads a letter from the input and returns it as a <code class="codecolorer text default"><span class="text">char</span></code>. If the next character on the input isn&#8217;t a letter, this parser won&#8217;t match.</p>
<p><strong>AtLeastOnce()</strong> – all parsers created by Sprache support repetition this way. AtLeastOnce() takes a parser for a single element of type <code class="codecolorer text default"><span class="text">T</span></code> and returns a new parser that will parse a sequence of elements, returning <code class="codecolorer text default"><span class="text">IEnumerable&lt;T&gt;</span></code>.</p>
<p><strong>Text()</strong> &#8211; the AtLeastOnce() modifier took our <code class="codecolorer text default"><span class="text">Parser&lt;char&gt;</span></code> and converted it into a parser of type <code class="codecolorer text default"><span class="text">Parser&lt;IEnumerable&lt;char&gt;&gt;</span></code>. The <code class="codecolorer text default"><span class="text">Text()</span></code> helper function takes a parser of <code class="codecolorer text default"><span class="text">IEnumerable&lt;char&gt;</span></code> and converts it into a <code class="codecolorer text default"><span class="text">Parser&lt;string&gt;</span></code>, so that the result is more convenient to work with.</p>
<p><strong>Token()</strong> &#8211; finally, the token modifier accepts then discards leading and trailing whitespace.</p>
<p>Simple?</p>
<p>There are a couple more tests we might be interested in for the Identifier parser.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AnIdentifierDoesNotIncludeSpace<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; var input <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;a b&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var parsed <span style="color: #008000;">=</span> QuestionnaireGrammar.<span style="color: #0000FF;">Identifier</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>input<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>“a”, parsed<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>A Sprache parser will parse as much as it can before returning. In this test case the parser will succeed, but won&#8217;t consume all of the input. (Later we&#8217;ll see how to make assertions about end-of-input.)</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AnIdentifierCannotStartWithQuote<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; var input <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>name&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; Assert.<span style="color: #0000FF;">Throws</span><span style="color: #008000;">&lt;</span>ParseException<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> QuestionnaireGrammar.<span style="color: #0000FF;">Identifier</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>input<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>The <code class="codecolorer text default"><span class="text">Parse()</span></code> extension method throws <code class="codecolorer text default"><span class="text">ParseException</span></code> when the parser doesn&#8217;t match. You can alternatively use the non-throwing <code class="codecolorer text default"><span class="text">TryParse()</span></code>.</p>
<p>Once we&#8217;re happy that identifiers are being parsed correctly we can move on.</p>
<h2>Parsing Quoted Text</h2>
<p>Quoted text isn&#8217;t much harder to parse than an identifier – our basic version doesn&#8217;t support escaped characters or anything fancy.</p>
<p>Looking again at the input:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">name&nbsp; &nbsp; &quot;Full Name&quot;</div></div>
<p>To parse the quoted text, we need to match:</p>
<ol>
<li>The opening quote</li>
<li>Anything except another quote</li>
<li>The closing quote</li>
</ol>
<p>The quotes themselves aren&#8217;t particularly interesting to the program, so we&#8217;ll return only the text between them.</p>
<p>One test for the parser will look like:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> QuotedTextReturnsAValueBetweenQuotes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; var input <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>this is text<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var content <span style="color: #008000;">=</span> QuestionnaireGrammar.<span style="color: #0000FF;">QuotedText</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>input<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;this is text&quot;</span>, content<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Jumping straight to the parser:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> Parser<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> QuotedText <span style="color: #008000;">=</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#40;</span>from open <span style="color: #0600FF;">in</span> Parse.<span style="color: #FF0000;">Char</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'&quot;'</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;from content <span style="color: #0600FF;">in</span> Parse.<span style="color: #0000FF;">CharExcept</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'&quot;'</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Many</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Text</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;from close <span style="color: #0600FF;">in</span> Parse.<span style="color: #FF0000;">Char</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'&quot;'</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;select content<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Token</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>This opportunistic repurposing of the Linq query comprehension syntax was first described (AFAIK) by <a href="http://blogs.msdn.com/lukeh">Luke Hoban</a> from the F# team. The from clauses lay out the individual units of the syntax, while the select clause transforms them into the return value of the overall parser.</p>
<h2>Parsing the Question</h2>
<p>You&#8217;ve probably noticed by now that parsers are strongly-typed: a parser for a character returns <code class="codecolorer text default"><span class="text">char</span></code>, and a parser for text returns <code class="codecolorer text default"><span class="text">string</span></code>. A parser for a question, will return – <code class="codecolorer text default"><span class="text">Question</span></code>!</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Question<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> Question<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> id, <span style="color: #FF0000;">string</span> prompt<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Id <span style="color: #008000;">=</span> id<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Prompt <span style="color: #008000;">=</span> prompt<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</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><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Prompt <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><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>This is a great strength of combinator-based parsing when constructing DSLs. Once a <a href="http://martinfowler.com/dslwip/SemanticModel.html">semantic model</a> for the problem has been built, the parser can translate input directly into it.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> Parser<span style="color: #008000;">&lt;</span>Question<span style="color: #008000;">&gt;</span> Question <span style="color: #008000;">=</span><br />
&nbsp; &nbsp; from id <span style="color: #0600FF;">in</span> Identifier<br />
&nbsp; &nbsp; from prompt <span style="color: #0600FF;">in</span> QuotedText<br />
&nbsp; &nbsp; select <span style="color: #008000;">new</span> Question<span style="color: #000000;">&#40;</span>id, prompt<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>A unit test for a basic question now passes:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AQuestionIsAnIdentifierFollowedByAPrompt<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; var input <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;name <span style="color: #008080; font-weight: bold;">\&quot;</span>Full Name<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var question <span style="color: #008000;">=</span> QuestionnaireGrammar.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>input<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;name&quot;</span>, question.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Full Name&quot;</span>, question.<span style="color: #0000FF;">Prompt</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<h2>Parsing a Section</h2>
<p>Parsing a section is just like parsing a question: first we build the semantic model, then use the existing parsers to translate the source text.</p>
<p>Remember a section looks like:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">identification &quot;Personal Details&quot;<br />
[<br />
&nbsp; &nbsp; name&nbsp; &nbsp; &nbsp; &nbsp; &quot;Full Name&quot;<br />
&nbsp; &nbsp; department&nbsp; &quot;Department&quot;<br />
]</div></div>
<p>We can represent this in the object model as:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Section<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> Section<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> id, <span style="color: #FF0000;">string</span> title, IEnumerable<span style="color: #008000;">&lt;</span>Question<span style="color: #008000;">&gt;</span> questions<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Id <span style="color: #008000;">=</span> id<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Title <span style="color: #008000;">=</span> title<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Questions <span style="color: #008000;">=</span> questions<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</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><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Prompt <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><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> IEnumerable<span style="color: #008000;">&lt;</span>Question<span style="color: #008000;">&gt;</span> Questions <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><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Building a parser is as easy as building the object model:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> Parser<span style="color: #008000;">&lt;</span>Section<span style="color: #008000;">&gt;</span> Section <span style="color: #008000;">=</span><br />
&nbsp; &nbsp; from id <span style="color: #0600FF;">in</span> Identifier<br />
&nbsp; &nbsp; from title <span style="color: #0600FF;">in</span> QuotedText<br />
&nbsp; &nbsp; from lbracket <span style="color: #0600FF;">in</span> Parse.<span style="color: #FF0000;">Char</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'['</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Token</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; from questions <span style="color: #0600FF;">in</span> Question.<span style="color: #0000FF;">Many</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; from rbracket <span style="color: #0600FF;">in</span> Parse.<span style="color: #FF0000;">Char</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">']'</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Token</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; select <span style="color: #008000;">new</span> Section<span style="color: #000000;">&#40;</span>id, title, questions<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>To complete the example, we have one more model class to define:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Questionnaire<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> Questionnaire<span style="color: #000000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span>Section<span style="color: #008000;">&gt;</span> sections<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Sections <span style="color: #008000;">=</span> sections<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> IEnumerable<span style="color: #008000;">&lt;</span>Section<span style="color: #008000;">&gt;</span> Sections <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><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Its corresponding parser (this time without the comprehension syntax):</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Parser<span style="color: #008000;">&lt;</span>Questionnaire<span style="color: #008000;">&gt;</span> Questionnaire <span style="color: #008000;">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Section.<span style="color: #0000FF;">Many</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>sections <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> Questionnaire<span style="color: #000000;">&#40;</span>sections<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>By affixing <code class="codecolorer text default"><span class="text">.End()</span></code> to the parser, we require that the full input is parsed (i.e. there&#8217;s no trailing garbage.)</p>
<p>That&#8217;s all we need for the example without any data type qualifiers.</p>
<h2>Supporting Answer Data Types</h2>
<p>The finishing touches of our grammar add support for the answer type qualifiers.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">#months &quot;Total Months Employed&quot;</div></div>
<p>We can use an enumeration for the possible answer types.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> AnswerType<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Natural,<br />
&nbsp; &nbsp; Number,<br />
&nbsp; &nbsp; Date,<br />
&nbsp; &nbsp; YesNo<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>This is a pretty limited set, so by brute force we&#8217;ll check for each possible qualifier.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Parser<span style="color: #008000;">&lt;</span>AnswerType<span style="color: #008000;">&gt;</span> AnswerTypeIndicator <span style="color: #008000;">=</span><br />
&nbsp; &nbsp; Parse.<span style="color: #FF0000;">Char</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'#'</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">Return</span><span style="color: #000000;">&#40;</span>AnswerType.<span style="color: #0000FF;">Natural</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #0000FF;">Or</span><span style="color: #000000;">&#40;</span>Parse.<span style="color: #FF0000;">Char</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'$'</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">Return</span><span style="color: #000000;">&#40;</span>AnswerType.<span style="color: #0000FF;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #0000FF;">Or</span><span style="color: #000000;">&#40;</span>Parse.<span style="color: #FF0000;">Char</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'%'</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">Return</span><span style="color: #000000;">&#40;</span>AnswerType.<span style="color: #0000FF;">Date</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #0000FF;">Or</span><span style="color: #000000;">&#40;</span>Parse.<span style="color: #FF0000;">Char</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'?'</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">Return</span><span style="color: #000000;">&#40;</span>AnswerType.<span style="color: #0000FF;">YesNo</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>The <code class="codecolorer text default"><span class="text">Question</span></code> object is modified to accept an <code class="codecolorer text default"><span class="text">AnswerType</span></code> as a constructor parameter, and a simple modification of the <code class="codecolorer text default"><span class="text">Question</span></code> parser completes our work.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Parser<span style="color: #008000;">&lt;</span>Question<span style="color: #008000;">&gt;</span> Question <span style="color: #008000;">=</span><br />
&nbsp; &nbsp; from at <span style="color: #0600FF;">in</span> AnswerTypeIndicator.<span style="color: #0000FF;">Or</span><span style="color: #000000;">&#40;</span>Parse.<span style="color: #0600FF;">Return</span><span style="color: #000000;">&#40;</span>AnswerType.<span style="color: #0000FF;">Text</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; from id <span style="color: #0600FF;">in</span> Identifier<br />
&nbsp; &nbsp; from prompt <span style="color: #0600FF;">in</span> QuotedText<br />
&nbsp; &nbsp; select <span style="color: #008000;">new</span> Question<span style="color: #000000;">&#40;</span>id, prompt, at<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<h2>Summary</h2>
<p>The complete parser is just six rules, totaling about 25 nicely-formatted lines of code.</p>
<p>While robust parsing is a non-trivial task in the real world, I hope this article shows that there are simple, low-friction options that fill some of the gaps between regular expressions and language workbenches.</p>
]]></content:encoded>
			<wfw:commentRss>http://nblumhardt.com/2010/01/building-an-external-dsl-in-c/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>MVC Integration Changes in Autofac Beta 2.1.6</title>
		<link>http://nblumhardt.com/2010/01/mvc-integration-changes-in-autofac-beta-2-1-6/</link>
		<comments>http://nblumhardt.com/2010/01/mvc-integration-changes-in-autofac-beta-2-1-6/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 11:05:23 +0000</pubDate>
		<dc:creator>Nicholas Blumhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nblumhardt.com/?p=85</guid>
		<description><![CDATA[Autofac 1.4 and earlier 2.1 versions used AutofacControllerModule as a way of finding and registering ASP.NET MVC controllers with the container. In the Autofac 2.1 MVC integration, there is a new ContainerBuilder extension method called RegisterControllers. var builder = new ContainerBuilder&#40;&#41;; // Was: // builder.RegisterModule(new AutofacControllerModule(...)) builder.RegisterControllers&#40;Assembly.GetExecutingAssembly&#40;&#41;&#41;; _containerProvider = new ContainerProvider&#40;builder.Build&#40;&#41;&#41;; ControllerBuilder.Current.SetControllerFactory&#40; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Autofac 1.4 and earlier 2.1 versions used <code class="codecolorer text default"><span class="text">AutofacControllerModule</span></code> as a way of finding and registering ASP.NET MVC controllers with the container.</p>
<p>In the Autofac 2.1 MVC integration, there is a new <code class="codecolorer text default"><span class="text">ContainerBuilder</span></code> extension method called <code class="codecolorer text default"><span class="text">RegisterControllers</span></code>.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var builder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContainerBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Was:</span><br />
<span style="color: #008080; font-style: italic;">// builder.RegisterModule(new AutofacControllerModule(...))</span><br />
builder.<span style="color: #0000FF;">RegisterControllers</span><span style="color: #000000;">&#40;</span>Assembly.<span style="color: #0000FF;">GetExecutingAssembly</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
_containerProvider <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContainerProvider<span style="color: #000000;">&#40;</span>builder.<span style="color: #0000FF;">Build</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
ControllerBuilder.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">SetControllerFactory</span><span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> AutofacControllerFactory<span style="color: #000000;">&#40;</span>_containerProvider<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>The reason for the switch is that <code class="codecolorer text default"><span class="text">RegisterControllers</span></code> is a thin wrapper around the new assembly scanner, and supports the same syntax as the rest of Autofac&#8217;s registration methods:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:800px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">builder.<span style="color: #0000FF;">RegisterControllers</span><span style="color: #000000;">&#40;</span>assembly<span style="color: #000000;">&#41;</span><br />
&nbsp; .<span style="color: #0000FF;">InjectProperties</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; .<span style="color: #0000FF;">OnActivated</span><span style="color: #000000;">&#40;</span>e <span style="color: #008000;">=&gt;</span> Log.<span style="color: #0000FF;">Information</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Controller created: &quot;</span> <span style="color: #008000;">+</span> e.<span style="color: #0000FF;">Instance</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://nblumhardt.com/2010/01/mvc-integration-changes-in-autofac-beta-2-1-6/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
