<?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>SLAGD</title>
	<atom:link href="http://slagd.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://slagd.com</link>
	<description>Slightly Lucid Association of General Developers</description>
	<lastBuildDate>Fri, 19 Feb 2010 19:09:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Enums Evolved</title>
		<link>http://slagd.com/?p=74</link>
		<comments>http://slagd.com/?p=74#comments</comments>
		<pubDate>Fri, 19 Feb 2010 19:07:56 +0000</pubDate>
		<dc:creator>Daniel Guenter</dc:creator>
				<category><![CDATA[Code ramblings]]></category>

		<guid isPermaLink="false">http://slagd.com/?p=74</guid>
		<description><![CDATA[Keying on a particular value is one of the first things we learn in programming.  As time goes on and we learn more advanced concepts, sometimes the little things tend to get lost in the dust and we keep doing things the same way we learned them in grade one.  I recently experienced a little [...]]]></description>
			<content:encoded><![CDATA[<p>Keying on a particular value is one of the first things we learn in programming.  As time goes on and we learn more advanced concepts, sometimes the little things tend to get lost in the dust and we keep doing things the same way we learned them in grade one.  I recently experienced a little of this pain in a bin packing project I&#8217;d been working one.  It all stared with enums, but for the sake of completeness I&#8217;m going to start all the way back with strings because that is probably what we all cut our teeth on.</p>
<p>The scenario is this. In a cuboid (box) that is axis aligned we will only ever have six surfaces. In writing code to deal with the cuboid we will be working with these surfaces and we need to know which way that surface is pointing.  Using a simple string approach we might represent it like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Surface <span style="color: #008000;">:</span> Rectangle
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> OrientationBack <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Back&quot;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> OrientationFront <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Front&quot;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> OrientationLeft <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Left&quot;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> OrientationRight <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Right&quot;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> OrientationUp <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Up&quot;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> OrientationDown <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Down&quot;</span>;
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Orientation <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now we have a string that tells us the direction that the surface we are working on is pointing.  A better approach to the string method would be to put the strings in their own class.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Orientation
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Back <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Back&quot;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Front <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Front&quot;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Left <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Left&quot;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Right <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Right&quot;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Up <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Up&quot;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Down <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Down&quot;</span>;	
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Surface <span style="color: #008000;">:</span> Rectangle
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Orientation <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The string approach is not optimal for several reasons.  1) We know that the types of orientation are never going to change and so representing orientation as a string is dangerous because if a bad string is used then our logic will not be able to account for it.  2) String comparison while not exactly slow, could be improved by another type.  This type of problem would be much better served by an enum instead.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> Orientation
<span style="color: #000000;">&#123;</span>
	Back,
	Front,
	Left,
	Right,
	Up,
	Down	
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Surface <span style="color: #008000;">:</span> Rectangle
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> Orientation Orientation <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is better because we can guarantee that only our six types will ever be used as an orientation. Plus because enum types are based on integers, comparison operations are fast.</p>
<p>Now we need to do something based on which direction our surface is pointing.  This situation appears because our surface is anchored in 3d space by a point which represents its upper left hand corner. We also have its width and height. However in order to do faster collision detection, rendering and ordering we need to translate those values into absolute coordinates for the axis that our surface is sitting on.  This means that if we have a surface that is facing left,  our depth is going to be on the X axis, our height will be on Z axis, and our width will be Y axis of the 3d container space. This will change depending on which side we are working with.  Don&#8217;t worry if this doesn&#8217;t make sense, it&#8217;s simply an example to help illustrate our problem.  We are going add these properties to our surface class.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// The depth of this plane on the Axis it is aligned to </span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span> Z <span style="color: #000000;">&#123;</span> get; <span style="color: #0600FF;">private</span> set; <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Top of this surface on the axis it is aligned to</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span> Top <span style="color: #000000;">&#123;</span> get; <span style="color: #0600FF;">private</span> set; <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Bottom of this surface on the axis it is aligned to</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span> Bottom <span style="color: #000000;">&#123;</span> get; <span style="color: #0600FF;">private</span> set; <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Left side of this surface on the axis it is aligned to</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span> Left <span style="color: #000000;">&#123;</span> get; <span style="color: #0600FF;">private</span> set; <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Right side of this surface on the axis it is aligned to</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span> Right <span style="color: #000000;">&#123;</span> get; <span style="color: #0600FF;">private</span> set; <span style="color: #000000;">&#125;</span></pre></div></div>

<p>We need to populate those values if our orientation changes or if our point changes.  Using the string, or enum method we would end up with something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> TranslatePoint<span style="color: #000000;">&#40;</span>Point point<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">switch</span><span style="color: #000000;">&#40;</span>Orientation<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">case</span> Orientation.<span style="color: #0000FF;">Back</span><span style="color: #008000;">:</span>
			Z <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Y</span>;
			Left <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">X</span>;
			Top <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Z</span>;
			Right <span style="color: #008000;">=</span> Left <span style="color: #008000;">-</span> Width;
			Bottom <span style="color: #008000;">=</span> Top <span style="color: #008000;">-</span> Height;
			break;
		<span style="color: #0600FF;">case</span> Orientation.<span style="color: #0000FF;">Front</span><span style="color: #008000;">:</span>
			Z <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Y</span>;
			Left <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">X</span>;
			Top <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Z</span>;
			Right <span style="color: #008000;">=</span> Left <span style="color: #008000;">+</span> Width;
			Bottom <span style="color: #008000;">=</span> Top <span style="color: #008000;">-</span> Height;
			break;
		<span style="color: #0600FF;">case</span> Orientation.<span style="color: #0000FF;">Left</span><span style="color: #008000;">:</span>
			Z <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">X</span>;
			Left <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Y</span>;
			Top <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Z</span>;
			Right <span style="color: #008000;">=</span> Left <span style="color: #008000;">+</span> Width;
			Bottom <span style="color: #008000;">=</span> Top <span style="color: #008000;">-</span> Height;
			break;
		<span style="color: #0600FF;">case</span> Orientation.<span style="color: #0000FF;">Right</span><span style="color: #008000;">:</span>
			Z <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">X</span>;
			Left <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Y</span>;
			Top <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Z</span>;
			Right <span style="color: #008000;">=</span> Left <span style="color: #008000;">-</span> Width;
			Bottom <span style="color: #008000;">=</span> Top <span style="color: #008000;">-</span> Height;
			break;
		<span style="color: #0600FF;">case</span> Orientation.<span style="color: #0000FF;">Up</span><span style="color: #008000;">:</span>
			Z <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Z</span>;
			Left <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">X</span>;
			Top <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Y</span>;
			Right <span style="color: #008000;">=</span> Left <span style="color: #008000;">+</span> Width;
			Bottom <span style="color: #008000;">=</span> Top <span style="color: #008000;">+</span> Height;
			break;
		<span style="color: #0600FF;">case</span> Orientation.<span style="color: #0000FF;">Down</span><span style="color: #008000;">:</span>
			Z <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Z</span>;
			Left <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">X</span>;
			Top <span style="color: #008000;">=</span> point.<span style="color: #0000FF;">Y</span>;
			Right <span style="color: #008000;">=</span> Left <span style="color: #008000;">+</span> Width;
			Bottom <span style="color: #008000;">=</span> Top <span style="color: #008000;">-</span> Height;
			break;		
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Wow, that&#8217;s a lot of bulk.  Now there are probably certain things that could be done to trim that method,  but it would still be bulky and hard to read.  Plus I am sure that I will be running into the same problem in other places. When you see a method like this you know that it is time to look for refactoring options.  There must be a way to move the logic out of this method into the Orientation where it belongs. </p>
<p>There is, and it is called the <a href="http://www.c-sharpcorner.com/uploadfile/rmcochran/strategypattern08072006095804am/strategypattern.aspx">strategy pattern.</a>  Using the strategy pattern we can move the logic into an Orientation class.  However because we only want a set amount of values we are going to make some modifications to make it more &#8220;enum&#8221; like.</p>
<p>Let&#8217;s create a new class called Orientation.  It will be used both to represent the orientation and to translate global points to that orientation or perform operations that pertain to that orientation.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Orientation
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> Func<span style="color: #008000;">&lt;</span>Point, Point<span style="color: #008000;">&gt;</span> _translator;
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> MathOp _increaseX;
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> MathOp _increaseY;
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> MathOp _increaseZ;
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> Func<span style="color: #008000;">&lt;</span>Orientation<span style="color: #008000;">&gt;</span> _flipside;
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Creates a new orientation</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;translator&quot;&gt;Method to translate a global point into a local point&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;increaseX&quot;&gt;Math operation necessary increase X values&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;increaseY&quot;&gt;Math operation necessary increase X values&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;increaseZ&quot;&gt;Math operation necessary increase X values&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;flipside&quot;&gt;The flipside of this orientation&lt;/param&gt;</span>
	<span style="color: #0600FF;">private</span> Orientation<span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>Point, Point<span style="color: #008000;">&gt;</span> translator, 
		MathOp increaseX, 
		MathOp increaseY, 
		MathOp increaseZ,
		Func<span style="color: #008000;">&lt;</span>Orientation<span style="color: #008000;">&gt;</span> flipside<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>translator <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;translator&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>increaseX <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;increaseX&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>increaseY <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;increaseY&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>increaseZ <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;increaseZ&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>flipside <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;flipside&quot;</span><span style="color: #000000;">&#41;</span>;
		_translator <span style="color: #008000;">=</span> translator;
		_increaseX <span style="color: #008000;">=</span> increaseX;
		_increaseY <span style="color: #008000;">=</span> increaseY;
		_increaseZ <span style="color: #008000;">=</span> increaseZ;
		_flipside <span style="color: #008000;">=</span> flipside;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// The flipside of this orientation</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #0600FF;">public</span> Orientation FlipSide
	<span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _flipside<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Translates a global point to this orientation</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;point&quot;&gt;&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
	<span style="color: #0600FF;">public</span> Point Translate<span style="color: #000000;">&#40;</span>Point point<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> _translator<span style="color: #000000;">&#40;</span>point<span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> MathOp IncreaseX
	<span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _increaseX; <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> MathOp IncreaseY
	<span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _increaseY; <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> MathOp IncreaseZ
	<span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _increaseZ; <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Notice how the constructor is private so that we can control its instantiation. We are also making use of delegates in the constructor so we can easily create logic in the constructor without having to resort to making a subclass for each different orientation. If you notice that we havn&#8217;t defined the MathOp class anywhere, that&#8217;s because it&#8217;s simply this exact same pattern repeated except for a math operation.</p>
<p>Next we are going to infuse the strategy pattern with enum like functionality through the use of static fields.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Orientation
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Orientation Back <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Orientation<span style="color: #000000;">&#40;</span>
		p <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> Point<span style="color: #000000;">&#40;</span>p.<span style="color: #0000FF;">X</span>, p.<span style="color: #0000FF;">Z</span>, p.<span style="color: #0000FF;">Y</span><span style="color: #000000;">&#41;</span>, 
		MathOp.<span style="color: #0000FF;">Subtract</span>, 
		MathOp.<span style="color: #0000FF;">Subtract</span>, 
		MathOp.<span style="color: #0000FF;">Subtract</span>, 
		<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Front<span style="color: #000000;">&#41;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Orientation Front <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Orientation<span style="color: #000000;">&#40;</span>
		p <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> Point<span style="color: #000000;">&#40;</span>p.<span style="color: #0000FF;">X</span>, p.<span style="color: #0000FF;">Z</span>, p.<span style="color: #0000FF;">Y</span><span style="color: #000000;">&#41;</span>, 
		MathOp.<span style="color: #0000FF;">Add</span>, 
		MathOp.<span style="color: #0000FF;">Subtract</span>, 
		MathOp.<span style="color: #0000FF;">Add</span>, 
		<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Back<span style="color: #000000;">&#41;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Orientation Left <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Orientation<span style="color: #000000;">&#40;</span>
		p <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> Point<span style="color: #000000;">&#40;</span>p.<span style="color: #0000FF;">Y</span>, p.<span style="color: #0000FF;">Z</span>, p.<span style="color: #0000FF;">X</span><span style="color: #000000;">&#41;</span>, 
		MathOp.<span style="color: #0000FF;">Add</span>, 
		MathOp.<span style="color: #0000FF;">Subtract</span>, 
		MathOp.<span style="color: #0000FF;">Subtract</span>, 
		<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Right<span style="color: #000000;">&#41;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Orientation Right <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Orientation<span style="color: #000000;">&#40;</span>
		p <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> Point<span style="color: #000000;">&#40;</span>p.<span style="color: #0000FF;">Y</span>, p.<span style="color: #0000FF;">Z</span>, p.<span style="color: #0000FF;">X</span><span style="color: #000000;">&#41;</span>, 
		MathOp.<span style="color: #0000FF;">Subtract</span>, 
		MathOp.<span style="color: #0000FF;">Subtract</span>, 
		MathOp.<span style="color: #0000FF;">Add</span>, 
		<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Left<span style="color: #000000;">&#41;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Orientation Up <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Orientation<span style="color: #000000;">&#40;</span>
		p <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> Point<span style="color: #000000;">&#40;</span>p.<span style="color: #0000FF;">X</span>, p.<span style="color: #0000FF;">Y</span>, p.<span style="color: #0000FF;">Z</span><span style="color: #000000;">&#41;</span>, 
		MathOp.<span style="color: #0000FF;">Add</span>, 
		MathOp.<span style="color: #0000FF;">Add</span>, 
		MathOp.<span style="color: #0000FF;">Add</span>, 
		<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Down<span style="color: #000000;">&#41;</span>;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Orientation Down <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Orientation<span style="color: #000000;">&#40;</span>
		p <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> Point<span style="color: #000000;">&#40;</span>p.<span style="color: #0000FF;">X</span>, p.<span style="color: #0000FF;">Y</span>, p.<span style="color: #0000FF;">Z</span><span style="color: #000000;">&#41;</span>, 
		MathOp.<span style="color: #0000FF;">Add</span>, 
		MathOp.<span style="color: #0000FF;">Subtract</span>, 
		MathOp.<span style="color: #0000FF;">Subtract</span>, 
		<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Up<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>We have now ended up with an enum-like class that incorporates so much more functionality.  Assigning an orientation to a variable is as simple as&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">surface.<span style="color: #0000FF;">Orientation</span> <span style="color: #008000;">=</span> Orientation.<span style="color: #0000FF;">Up</span>;</pre></div></div>

<p>Comparison works the same way.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>Orientation <span style="color: #008000;">==</span> Orientation.<span style="color: #0000FF;">Up</span><span style="color: #000000;">&#41;</span>
	<span style="color: #008080; font-style: italic;">//do something</span></pre></div></div>

<p>The only thing that we are lacking is the ability to switch on our Orientation class, but with the new functionality we shouldn&#8217;t need to.  Just look at what our bulky &#8220;TranslatePoint&#8221; method has turned into.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> TranslatePoint<span style="color: #000000;">&#40;</span>Point point<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Point translated <span style="color: #008000;">=</span> Orientation.<span style="color: #0000FF;">Translate</span><span style="color: #000000;">&#40;</span>point<span style="color: #000000;">&#41;</span>;
	Z <span style="color: #008000;">=</span> translated.<span style="color: #0000FF;">Z</span>;
	Left <span style="color: #008000;">=</span> translated.<span style="color: #0000FF;">X</span>;
	Top <span style="color: #008000;">=</span> translated.<span style="color: #0000FF;">Y</span>;
	Right <span style="color: #008000;">=</span> Orientation.<span style="color: #0000FF;">IncreaseX</span>.<span style="color: #0000FF;">Calculate</span><span style="color: #000000;">&#40;</span>Left, Width<span style="color: #000000;">&#41;</span>;
	Bottom <span style="color: #008000;">=</span> Orientation.<span style="color: #0000FF;">IncreaseY</span>.<span style="color: #0000FF;">Calculate</span><span style="color: #000000;">&#40;</span>Top, Height<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>By applying an appropriate pattern we have made our code more readable, less bulky and insured that it has a stable growth plan.  </p>
<p>Remember when refactoring don&#8217;t forget the simple things.</p>
]]></content:encoded>
			<wfw:commentRss>http://slagd.com/?feed=rss2&amp;p=74</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rusty spikes</title>
		<link>http://slagd.com/?p=70</link>
		<comments>http://slagd.com/?p=70#comments</comments>
		<pubDate>Wed, 15 Jul 2009 17:52:55 +0000</pubDate>
		<dc:creator>Daniel Guenter</dc:creator>
				<category><![CDATA[NHibernate.Remote]]></category>

		<guid isPermaLink="false">http://slagd.com/?p=70</guid>
		<description><![CDATA[NHibernate.Remote was one of my favorite projects.  Please note the word &#8220;was&#8221;.  The initial concept was exciting and early forays into coding provided positive results, however as with all concepts the devil is in the details.  In the end it was the lack of being able to extend the NetDataContractSerializer that really killed the project.  [...]]]></description>
			<content:encoded><![CDATA[<p>NHibernate.Remote was one of my favorite projects.  Please note the word &#8220;was&#8221;.  The initial concept was exciting and early forays into coding provided positive results, however as with all concepts the devil is in the details.  In the end it was the lack of being able to extend the NetDataContractSerializer that really killed the project.  Not being able to analyze objects as they came over the wire led to a large amount of brittle reflection.  This led to problems synchronizing objects bidirectionally.  NHibernate.Remote was in reality a leaky abstraction on top of another not quite as leaky abstraction.  As I don&#8217;t have infinite time at work I have shifted focus to other ways of getting data out to clients.  I will probably leave the source online in case someone else wants to check out the concept but as far as I am concerned the project is dead.</p>
]]></content:encoded>
			<wfw:commentRss>http://slagd.com/?feed=rss2&amp;p=70</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>You can do crazy things with WCF</title>
		<link>http://slagd.com/?p=67</link>
		<comments>http://slagd.com/?p=67#comments</comments>
		<pubDate>Fri, 06 Mar 2009 22:50:37 +0000</pubDate>
		<dc:creator>Daniel Guenter</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://slagd.com/?p=67</guid>
		<description><![CDATA[Have you ever wanted to code something that wasn&#8217;t exactly best practices, but would make for a neat concept?  I know that I have.  I seem to be constantly pushing against the edges of best practices hoping to find the next &#8220;Dependency Injection&#8221; concept that makes everyones lives just a little bit easier.  While my [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever wanted to code something that wasn&#8217;t exactly best practices, but would make for a neat concept?  I know that I have.  I seem to be constantly pushing against the edges of best practices hoping to find the next &#8220;Dependency Injection&#8221; concept that makes everyones lives just a little bit easier.  While my current spike project is not of that caliber, it is pushing the boundaries in a weird sort of way.  So instead of just laughing at myself, I thought that I&#8217;d open up access to it and let you guys laugh at me as well.  So what is this spike that I&#8217;m talking about?  This is a database driver for NHibernate that works accross the network, or across WCF specifically.</p>
<p>The concept is very simple.  NHibernate, at its very base relies on an IDbConnection to go out and fetch the data from the database.  Normally this connects rather directly to the db in a low latency connection.  Being an intensely curious programmer I wondered if it would be possible to route this raw db connection over top of a WCF connection.  The cool things is that it works, although introducing more latency than is optimal of course.  It works by opening up the actual db connection on the server and then reading the data associated with a command into a dataset which is transmitted accross WCF.  On the client side a datareader is created from the dataset thus allowing the client to read the database and perform other operations.</p>
<p>How reliable is this concept.  Well I haven&#8217;t had the chance to use it in any production apps.  (Not sure that I&#8217;d want to) but it does pass all of NHibernate 2.0.1GA&#8217;s test save one that deals with schemas. Let&#8217;s say that this piques your interest a little bit and you would like to try it out.  I&#8217;m not going to be releasing binaries and a big how to, but here&#8217;s the SVN url and you can give it a try for yourselves.</p>
<p><a href="http://slagd.com/svn/NHibernate.WCFDriver/Trunk">http://slagd.com/svn/NHibernate.WCFDriver/Trunk</a></p>
<p>There is a project in the source called &#8220;TestServer&#8221; which is simply a console project that will host the server.  The server is the only part that has a custom configuration element in the app.config.  Both the server and the client rely on standard WCF configuration to create a connection.  You can find examples of this configuration in both the app.config of the NHibernate.Test-2.0 project and the app.config of the TestServer project.  It&#8217;s also important to note that when you are using the WCFDbDriver on the client, the NHibernate connection string setting is ignored.  You can put whatever you want there but you will have to give it a value or NHibernate will complain.  Actual client-server connection information is located in the WCF configuration elements.</p>
<p>This is definitely not in keeping with best-practices but who says you can&#8217;t have fun with programming sometimes.</p>
]]></content:encoded>
			<wfw:commentRss>http://slagd.com/?feed=rss2&amp;p=67</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GenericMessaging updates via SVN</title>
		<link>http://slagd.com/?p=65</link>
		<comments>http://slagd.com/?p=65#comments</comments>
		<pubDate>Wed, 04 Feb 2009 00:48:27 +0000</pubDate>
		<dc:creator>Daniel Guenter</dc:creator>
				<category><![CDATA[Generic Messaging]]></category>

		<guid isPermaLink="false">http://slagd.com/?p=65</guid>
		<description><![CDATA[It&#8217;s been a while since I&#8217;ve done anything with the GenericMessaging project which is too bad since I consider it to be the most useful of the projects that I have been working on lately.  Last week I ran into a painful brick wall regarding the security system I&#8217;ve been working on, so I [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since I&#8217;ve done anything with the GenericMessaging project which is too bad since I consider it to be the most useful of the projects that I have been working on lately.  Last week I ran into a painful brick wall regarding the security system I&#8217;ve been working on, so I decided to do some reworking on GenericMessaging while my brain sorts itself out and develops new neural pathways to replace the fried ones.  There have been several comments on the forum about GenericMessaging, mostly regarding its dependence on PostSharp.  While I think that PostSharp is great, it&#8217;s probably not something that needs to be intimately linked with the GenericMessaging layer.  I&#8217;ve abstracted PostSharp support out into its own separate library.  I&#8217;ve also added a new library that contains a class that will dynamically create a proxy for you using the Castle Dynamic Proxy Generator.  This should give users more flexibility.  I&#8217;ve uploaded these changes to the svn repositories but I won&#8217;t update the binaries quite yet as there is some more refactoring that needs to take place.  So go grab the latest version from svn and give it a whirl.</p>
<p>Oh and a big thanks to those people who have posted on the forum with suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://slagd.com/?feed=rss2&amp;p=65</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NHibernate and dynamic(sorta) table names.</title>
		<link>http://slagd.com/?p=62</link>
		<comments>http://slagd.com/?p=62#comments</comments>
		<pubDate>Thu, 08 Jan 2009 23:41:58 +0000</pubDate>
		<dc:creator>Daniel Guenter</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://slagd.com/?p=62</guid>
		<description><![CDATA[I have had the very bad fortune to be working with some very old database structures at the moment. Upgrading those structures is not possible, however there are times when it would be nice to map against them.  Recently I have been banging my head against a scenario where an entity may exist in one [...]]]></description>
			<content:encoded><![CDATA[<p>I have had the very bad fortune to be working with some very old database structures at the moment. Upgrading those structures is not possible, however there are times when it would be nice to map against them.  Recently I have been banging my head against a scenario where an entity may exist in one database at &#8220;X&#8221; location and in an entirely different database at &#8220;Y&#8221; location.  This works fine as long as this database is the only one that you are connecting and mapping too.  You can simply change the database name in the connection string and life is good.  However there are situations &#8211; mine in particular &#8211; where you have to map relations between your entities across databases, ouch.  In this situation a person needs to prefix the names of their table mappings with the database name as well.  This seems to work at least until you need to change the name of the database that you are connecting to.</p>
<p>So is this situation even possible with NHiberante(2.0.1GA)?  It turns out that yes it is, but first you have to get NHibernate to perform some gymnastics.  This seems to work fine for me but that is not a guarantee that your experience will be similar.  There is an additional caveat as well, that is that this only works for specifying the db names before you start an NH session factory.  Once you start the session the names are finalized and this method will not work to change them.   This method actually works by putting placholder values in your mapping files instead of actually database names.  What we need to do then is to strip out the placeholders and replace them with real db names when the NH session factory is being created.  We do this by looking through the configuration file that is generated by NH and replacing our values before the session factory is created.  In theory this would also work with table names as well as db names, but that is not something that I have tested with.</p>
<p>Lets start out with a very simple mapping.  Remember that we are not exactly sure of the database name that this will be residing in.</p>

<div class="wp_syntax"><div class="code"><pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hibernate-mapping</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;urn:nhibernate-mapping-2.2&quot;</span> <span style="color: #000066;">assembly</span>=<span style="color: #ff0000;">&quot;MemX.Data.Entities&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;MemX.Data.Entities.RMS&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Item&quot;</span> <span style="color: #000066;">table</span>=<span style="color: #ff0000;">&quot;$RmsDB.dbo.Item&quot;</span> <span style="color: #000066;">lazy</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ID&quot;</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;ID&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Int32&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;generator</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;native&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hibernate-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>What you can see, when you look at the attribute containing the table name, is that we&#8217;ve put a placeholder there instead of the actual table name.</p>
<p>Now that we have a placeholder there instead of a database name we need a class that can help us to easily modify NH&#8217;s configuration.  Lets start out with a dictionary to contain our placholder to value mappings.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Helps to map table symbols in the NH mappings to actual table/database names</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NHTableMappings
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Maps from the placeholder string to the real table/database name</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">public</span> IDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> Mappings <span style="color: #000000;">&#123;</span> get; <span style="color: #0600FF;">protected</span> set; <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Creates a set of table/database mappings</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">public</span> NHTableMappings<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Mappings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And to push those mappings into the NH configuration we would need to do something like this inside our NHTableMappings class.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// maps table symbols to actual table/database names</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;configuration&quot;&gt;&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> MapTables<span style="color: #000000;">&#40;</span>Configuration configuration<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>configuration <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;configuration&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #008080; font-style: italic;">//get the tables using reflection</span>
    FieldInfo tablesField <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>Configuration<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetField</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;tables&quot;</span>, BindingFlags.<span style="color: #0000FF;">Instance</span> | BindingFlags.<span style="color: #0000FF;">NonPublic</span><span style="color: #000000;">&#41;</span>;
    IDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, Table<span style="color: #008000;">&gt;</span> tables <span style="color: #008000;">=</span> tablesField.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>configuration<span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> IDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, Table&gt;;
&nbsp;
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>tables <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't retrieve table information from NHibernate configuration.&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #008080; font-style: italic;">//look through NHTableMappings</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>KeyValuePair<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> key_tableName <span style="color: #0600FF;">in</span> Mappings<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">string</span> tableKey <span style="color: #008000;">=</span> key_tableName.<span style="color: #0000FF;">Key</span>;
        <span style="color: #FF0000;">string</span> tableName <span style="color: #008000;">=</span> key_tableName.<span style="color: #0000FF;">Value</span>;
&nbsp;
        IEnumerable<span style="color: #008000;">&lt;</span>Table<span style="color: #008000;">&gt;</span> tablesToChange <span style="color: #008000;">=</span> tables.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Key</span>.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>tableKey<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Table table <span style="color: #0600FF;">in</span> tablesToChange<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            table.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> table.<span style="color: #0000FF;">Name</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span>tableKey, tableName<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It&#8217;s pretty simple really.  We use reflection to get the tables field from the NH configuration because it is not public.  We scan through the table names looking for keys that are in our mapping and we do the old switcheroo.  Once this has been done we can start up a session factory and our entity will now be mapped against the correct database.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//Build the NH configuration</span>
Configuration nhConfiguration <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Configuration<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Configure</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008080; font-style: italic;">//add your mappings</span>
NHTableMappings tableMappings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NHTableMappings<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
tableMappings.<span style="color: #0000FF;">Mappings</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;$RmsDB&quot;</span>, <span style="color: #666666;">&quot;MyRmsDatabaseName&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008080; font-style: italic;">//modify the NH configuration</span>
tableMappings.<span style="color: #0000FF;">MapTables</span><span style="color: #000000;">&#40;</span>nhConfiguration<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008080; font-style: italic;">//build your session factory</span>
ISessionFactory sessionFactory <span style="color: #008000;">=</span> nhConfiguration.<span style="color: #0000FF;">BuildSessionFactory</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>I hope this helps someone who is trying solve the same problem that I was.</p>
<p>I&#8217;ve included some complete classes here in case you just want to copy and paste (plus some includes).</p>
<p><strong>The main class.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Configuration</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Reflection</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Xml</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">log4net</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">NHibernate.Mapping</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Configuration</span><span style="color: #008000;">=</span>NHibernate.<span style="color: #0000FF;">Cfg</span>.<span style="color: #0000FF;">Configuration</span>;
&nbsp;
<span style="color: #0600FF;">namespace</span> MemX.<span style="color: #0000FF;">Data</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Helps to map table symbols in the NH mappings to actual table/database names</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NHTableMappings
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> ILog log <span style="color: #008000;">=</span> LogManager.<span style="color: #0000FF;">GetLogger</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>NHTableMappings<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> <span style="color: #FF0000;">string</span> ConfigSection <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;nh-table-mappings&quot;</span>;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> <span style="color: #FF0000;">string</span> ConfigKey <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;mapping&quot;</span>;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> <span style="color: #FF0000;">string</span> KeyAttribute <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;table-key&quot;</span>;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> <span style="color: #FF0000;">string</span> NameAttribute <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;table-name&quot;</span>;
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Maps from the placeholder string to the real table/database name</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF;">public</span> IDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> Mappings <span style="color: #000000;">&#123;</span> get; <span style="color: #0600FF;">protected</span> set; <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Creates a set of table/database mappings</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF;">public</span> NHTableMappings<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Mappings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Adds a mapping from a key to an actual name</span>
        <span style="color: #008080; font-style: italic;">/// &lt;para&gt;Can be used for table names or DB names&lt;/para&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;tableKey&quot;&gt;&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;tableName&quot;&gt;&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;overwrite&quot;&gt;Do we want to overwrite an existing value&lt;/param&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddMapping<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> tableKey, <span style="color: #FF0000;">string</span> tableName, <span style="color: #FF0000;">bool</span> overwrite<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>tableKey<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;tableKey&quot;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>tableName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;tableName&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>Mappings.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span>tableKey<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                Mappings.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>tableKey, tableName<span style="color: #000000;">&#41;</span>;
            <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>overwrite<span style="color: #000000;">&#41;</span>
                Mappings<span style="color: #000000;">&#91;</span>tableKey<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> tableName;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// maps table symbols to actual table/database names</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;configuration&quot;&gt;&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> MapTables<span style="color: #000000;">&#40;</span>Configuration configuration<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>configuration <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;configuration&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #008080; font-style: italic;">//get the tables using reflection</span>
            FieldInfo tablesField <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>Configuration<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetField</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;tables&quot;</span>, BindingFlags.<span style="color: #0000FF;">Instance</span> | BindingFlags.<span style="color: #0000FF;">NonPublic</span><span style="color: #000000;">&#41;</span>;
            IDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, Table<span style="color: #008000;">&gt;</span> tables <span style="color: #008000;">=</span> tablesField.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>configuration<span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> IDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, Table&gt;;
&nbsp;
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>tables <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't retrieve table information from NHibernate configuration.&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #008080; font-style: italic;">//look through NHTableMappings</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>KeyValuePair<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> key_tableName <span style="color: #0600FF;">in</span> Mappings<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">string</span> tableKey <span style="color: #008000;">=</span> key_tableName.<span style="color: #0000FF;">Key</span>;
                <span style="color: #FF0000;">string</span> tableName <span style="color: #008000;">=</span> key_tableName.<span style="color: #0000FF;">Value</span>;
&nbsp;
                IEnumerable<span style="color: #008000;">&lt;</span>Table<span style="color: #008000;">&gt;</span> tablesToChange <span style="color: #008000;">=</span> tables.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Key</span>.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>tableKey<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Table table <span style="color: #0600FF;">in</span> tablesToChange<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    table.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> table.<span style="color: #0000FF;">Name</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span>tableKey, tableName<span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Reads the configuration from xml</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;section&quot;&gt;&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;overwrite&quot;&gt;Do we want to overwrite existing configuration&lt;/param&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ReadFromXml<span style="color: #000000;">&#40;</span>XmlNode section, <span style="color: #FF0000;">bool</span> overwrite<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            log.<span style="color: #0000FF;">Debug</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Reading table mappings from xml&quot;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>section <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;section&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>section.<span style="color: #0000FF;">LocalName</span> <span style="color: #008000;">!=</span> ConfigSection<span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ConfigurationErrorsException<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Invalid section {0} in NHTableMappings configuration&quot;</span>, section.<span style="color: #0000FF;">LocalName</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>XmlNode node <span style="color: #0600FF;">in</span> section.<span style="color: #0000FF;">ChildNodes</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>node.<span style="color: #0000FF;">LocalName</span> <span style="color: #008000;">!=</span> ConfigKey<span style="color: #000000;">&#41;</span>
                    <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ConfigurationErrorsException<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Invalid section {0} in NHTableMappings configuration&quot;</span>, node.<span style="color: #0000FF;">LocalName</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
                <span style="color: #FF0000;">string</span> tableKey <span style="color: #008000;">=</span> node.<span style="color: #0000FF;">Attributes</span><span style="color: #000000;">&#91;</span>KeyAttribute<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span>;
                <span style="color: #FF0000;">string</span> tableName <span style="color: #008000;">=</span> node.<span style="color: #0000FF;">Attributes</span><span style="color: #000000;">&#91;</span>NameAttribute<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span>;
&nbsp;
                AddMapping<span style="color: #000000;">&#40;</span>tableKey, tableName, overwrite<span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Creates the mappings from an xml section</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;section&quot;&gt;&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;overwrite&quot;&gt;Do we want to overwrite existing configuration&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> NHTableMappings CreateFromXml<span style="color: #000000;">&#40;</span>XmlNode section, <span style="color: #FF0000;">bool</span> overwrite<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            NHTableMappings tableMappings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NHTableMappings<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            tableMappings.<span style="color: #0000FF;">ReadFromXml</span><span style="color: #000000;">&#40;</span>section, overwrite<span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">return</span> tableMappings;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Creates the mappings from a config section</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> NHTableMappings CreateFromConfig<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            NHTableMappings tableMappings <span style="color: #008000;">=</span> ConfigurationManager.<span style="color: #0000FF;">GetSection</span><span style="color: #000000;">&#40;</span>ConfigSection<span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> NHTableMappings;
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>tableMappings <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ConfigurationErrorsException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Could not load table mappings confugration from app/web.config&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">return</span> tableMappings;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Creates the mappings from a config section if it exists, or defaults to an empty mappings file</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> NHTableMappings CreateFromConfigOrDefault<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            NHTableMappings mappings;
&nbsp;
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                mappings <span style="color: #008000;">=</span> CreateFromConfig<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ConfigurationErrorsException<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                mappings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NHTableMappings<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> mappings;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>A configuration section handler.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Configuration</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Xml</span>;
&nbsp;
<span style="color: #0600FF;">namespace</span> MemX.<span style="color: #0000FF;">Data</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NHTableMappingsConfigurationSectionHandler <span style="color: #008000;">:</span> IConfigurationSectionHandler
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080;">#region IConfigurationSectionHandler Members</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> Create<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> parent, <span style="color: #FF0000;">object</span> configContext, XmlNode section<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> NHTableMappings.<span style="color: #0000FF;">CreateFromXml</span><span style="color: #000000;">&#40;</span>section, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The usage looks something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configSections<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;nh-table-mappings&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;MemX.Data.NHTableMappingsConfigurationSectionHandler, MemX.Data&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configSections<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nh-table-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mapping</span> <span style="color: #000066;">table-key</span>=<span style="color: #ff0000;">&quot;$RmsDB&quot;</span> <span style="color: #000066;">table-name</span>=<span style="color: #ff0000;">&quot;RMSCal3&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/nh-table-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configruation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong> And even an extension method</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> NHTableMappingsExtension
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Configuration MapTables<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> Configuration configuration<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            NHTableMappings tableMappings <span style="color: #008000;">=</span> NHTableMappings.<span style="color: #0000FF;">CreateFromConfig</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            tableMappings.<span style="color: #0000FF;">MapTables</span><span style="color: #000000;">&#40;</span>configuration<span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">return</span> configuration;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Using the configuration and the extension it would looking something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">            ISessionFactory sessionFactory <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Configuration<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                .<span style="color: #0000FF;">Configure</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                .<span style="color: #0000FF;">MapTables</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                .<span style="color: #0000FF;">BuildSessionFactory</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>You could also include some default mappings and include them as a resource along with your mappings and only override when needed as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://slagd.com/?feed=rss2&amp;p=62</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>StructureMap and the Singleton Scenario</title>
		<link>http://slagd.com/?p=51</link>
		<comments>http://slagd.com/?p=51#comments</comments>
		<pubDate>Sat, 06 Dec 2008 00:18:54 +0000</pubDate>
		<dc:creator>Daniel Guenter</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[StructureMap]]></category>

		<guid isPermaLink="false">http://slagd.com/?p=51</guid>
		<description><![CDATA[A few months ago I made the switch from CastleWindsor to StructureMap for my dependency injection needs.  It&#8217;s a more active project, more up to date with the times, more extensible, and definitely has a better interface for configuring through code rather than xml.  After a few growing pains I&#8217;ve grown to like [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago I made the switch from CastleWindsor to StructureMap for my dependency injection needs.  It&#8217;s a more active project, more up to date with the times, more extensible, and definitely has a better interface for configuring through code rather than xml.  After a few growing pains I&#8217;ve grown to like it a lot ( I&#8217;m using version 2.5).   However in the last few days I&#8217;ve run into a roadblock it seems, and it all has to do with the lifecycle/scope of objects.</p>
<p>A lot of dependency objects in the typical application could be singletons if designed right (covers head from flames).  However there is a point at which that ceases to be the case, that&#8217;s where unit of work patterns come into play.  A unit of work has only a finite lifespan and should then be disposed.  In keeping with good testability and design patterns you will probably also want to have a ready made unit of work object injected into the classes that need them.  Every time that you ask for a unit of work you should be supplied with a fresh new copy. That&#8217;s where configuring the lifecycles properly in your DI tool should come into play.</p>
<p>As code is a common language, I will code out what I am talking about as simply as possible.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//Think of this as a data session</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Session
<span style="color: #000000;">&#123;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Model1
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> Session Session <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> Model1<span style="color: #000000;">&#40;</span>Session session<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Session <span style="color: #008000;">=</span> session;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Model2
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> Session Session <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> Model2<span style="color: #000000;">&#40;</span>Session session<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Session <span style="color: #008000;">=</span> session;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//this is my shell, it needs some models</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Shell
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> Model1 Model1 <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> Model2 Model2 <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> Shell<span style="color: #000000;">&#40;</span>Model1 model1, Model2 model2<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Model1 <span style="color: #008000;">=</span> model1;
        Model2 <span style="color: #008000;">=</span> model2;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is a pretty straight forward scenario.  We have a shell (entry point) that contains a couple of models, each of these models requires a unique data session so that it can pull and work with its data independently.</p>
<p>Let&#8217;s use StructureMap to wire these together so that we just create our shell and have its dependencies filled for us automatically.  Remember that by default StructureMap does not create singletons, so using the default config should result in transient, per-request objects.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">ObjectFactory.<span style="color: #0000FF;">Initialize</span><span style="color: #000000;">&#40;</span>
    x <span style="color: #008000;">=&amp;</span>gt; <span style="color: #000000;">&#123;</span>
             x.<span style="color: #0000FF;">BuildInstancesOf</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                 .<span style="color: #0000FF;">TheDefaultIsConcreteType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
             x.<span style="color: #0000FF;">BuildInstancesOf</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                 .<span style="color: #0000FF;">TheDefaultIsConcreteType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
             x.<span style="color: #0000FF;">BuildInstancesOf</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                 .<span style="color: #0000FF;">TheDefaultIsConcreteType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
             x.<span style="color: #0000FF;">BuildInstancesOf</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                 .<span style="color: #0000FF;">TheDefaultIsConcreteType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Now let&#8217;s test to make sure that we have a unique data session.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">Shell shell <span style="color: #008000;">=</span> ObjectFactory.<span style="color: #0000FF;">GetInstance</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
Assert.<span style="color: #0000FF;">AreNotEqual</span><span style="color: #000000;">&#40;</span>shell.<span style="color: #0000FF;">Model1</span>.<span style="color: #0000FF;">Session</span>, shell.<span style="color: #0000FF;">Model2</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>And this is where we come to the fail part.  The data session contained in each model is identical, the same as if we had declared it as a singleton.   We know that the configuration is right because doing it this way works.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">Assert.<span style="color: #0000FF;">AreNotEqual</span><span style="color: #000000;">&#40;</span>ObjectFactory.<span style="color: #0000FF;">GetInstance</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, ObjectFactory.<span style="color: #0000FF;">GetInstance</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Why this happens has been a matter of great curiosity to me, and even though I&#8217;m not a great StructureMap expert I&#8217;ll hazard an educated guess.  Each time that you make a declarative request for the root of an object graph, StructureMap creates a unique BuildSession.  Inside that build session there is a cache of instantiated objects.  The session then builds <strong>one</strong> object for each dependency in the graph no matter how many times it is requested in that graph.  The objects may be built or pulled using a variety of methods but once they are built and stored in the graph then they are not requested to be built again for the lifetime of that BuildSession.  This means that for a given declarative request you will only ever have one object per type instantiated.  If, like me, you are starting your application from a bootstrapper this effectively turns all of your objects into singletons.</p>
<p>Lets try the same scenraio in CastleWindsor.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">WindsorContainer container <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WindsorContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
container.<span style="color: #0000FF;">AddComponentWithLifestyle</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Session&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Session<span style="color: #000000;">&#41;</span>, LifestyleType.<span style="color: #0000FF;">Transient</span><span style="color: #000000;">&#41;</span>;
container.<span style="color: #0000FF;">AddComponentWithLifestyle</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Model1&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Model1<span style="color: #000000;">&#41;</span>, LifestyleType.<span style="color: #0000FF;">Transient</span><span style="color: #000000;">&#41;</span>;
container.<span style="color: #0000FF;">AddComponentWithLifestyle</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Model2&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Model2<span style="color: #000000;">&#41;</span>, LifestyleType.<span style="color: #0000FF;">Transient</span><span style="color: #000000;">&#41;</span>;
container.<span style="color: #0000FF;">AddComponentWithLifestyle</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Shell&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Shell<span style="color: #000000;">&#41;</span>, LifestyleType.<span style="color: #0000FF;">Transient</span><span style="color: #000000;">&#41;</span>;
&nbsp;
Shell shell <span style="color: #008000;">=</span> container.<span style="color: #0000FF;">Resolve</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
Assert.<span style="color: #0000FF;">AreNotEqual</span><span style="color: #000000;">&#40;</span>shell.<span style="color: #0000FF;">Model1</span>.<span style="color: #0000FF;">Session</span>, shell.<span style="color: #0000FF;">Model2</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>This works, both data sessions are unique.   Let&#8217;s try AutoFac.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">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>;
&nbsp;
builder.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FactoryScoped</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
builder.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FactoryScoped</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
builder.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FactoryScoped</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
builder.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FactoryScoped</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
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>;
&nbsp;
Shell shell <span style="color: #008000;">=</span> container.<span style="color: #0000FF;">Resolve</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
Assert.<span style="color: #0000FF;">AreNotEqual</span><span style="color: #000000;">&#40;</span>shell.<span style="color: #0000FF;">Model1</span>.<span style="color: #0000FF;">Session</span>, shell.<span style="color: #0000FF;">Model2</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Again this works as well.</p>
<p>How can this problem be solved by mere mortals?  Well the most obvious solution would be to pass factories that can create your transient objects instead of just passing the transient objects themselves.  However this is not ideal as it just leads to more code noise that should be handled by the DI component.</p>
<p>Can we use an interceptor?  Unfortunately this approach does not work as your custom interceptor will be called only once per BuildSession resulting in the same problem.  The same is true for using a ConstructorInstance.  As this post ends I have been searching for a way of getting true instanced objects out of structuremap for most of a day with no success.  In ending let me say that I am open to any expertise you may offer on this subject, and if I do find the answer I will most definitely post an update to this article.</p>
<p><strong>Update</strong><br />
It would appear that there is no good way to do this using StructureMap. There is a workaround that though arduous could work for more limited scenarios.  The trick is to define the dependency for the object that needs a transient type.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">x.<span style="color: #0000FF;">BuildInstancesOf</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    .<span style="color: #0000FF;">TheDefault</span>.<span style="color: #008000;">Is</span>.<span style="color: #0000FF;">OfConcreteType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">CtorDependency</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #008000;">Is</span><span style="color: #000000;">&#40;</span> x.<span style="color: #0000FF;">OfConcreteType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Though this works it wouldn&#8217;t be really practical if your dependency is used in numerous places.  The reasons that the author chose to limit transients to only once per session are valid and advantageous for his situation but causes some problems elsewhere.  What I would have preferred was that the BuildSession caching was done with an interceptor the same way as singleton caching.  That way not every instance would be limited by the BuildSession caching and other implementations could be provided.</p>
<p><strong>Update</strong></p>
<p>As Bart Deleye pointed out in the comments the latest version of StructureMap now properly supports this scenario with the following syntax.</p>
<p>.ForRequestedType().TheDefaultIsConcreteType().AlwaysUnique()</p>
<p>Thus resulting in a very happy ending due to the flexibility and collaborative nature of open source software.  Thanks Jeremy</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fslagd.com%2f%3fpage_id%3d51"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fslagd.com%2f%3fpage_id%3d51" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://slagd.com/?feed=rss2&amp;p=51</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Dependency Injection in WPF Bindings</title>
		<link>http://slagd.com/?p=38</link>
		<comments>http://slagd.com/?p=38#comments</comments>
		<pubDate>Mon, 01 Dec 2008 22:05:12 +0000</pubDate>
		<dc:creator>Daniel Guenter</dc:creator>
				<category><![CDATA[Code ramblings]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://slagd.com/?p=38</guid>
		<description><![CDATA[Creating clean testable code is one of the major challenges when creating complicated, composite user interfaces.  To help with this a variety of patterns have cropped up including MVC, MVP, and most notably for WPF the MVVM pattern.   These patterns work fine for the most part at least until your view gets a little bit [...]]]></description>
			<content:encoded><![CDATA[<p>Creating clean testable code is one of the major challenges when creating complicated, composite user interfaces.  To help with this a variety of patterns have cropped up including MVC, MVP, and most notably for WPF the MVVM pattern.   These patterns work fine for the most part at least until your view gets a little bit too complicated.  What is a programmer supposed to do with the added complexity?  You should be separating common logic out into self contained controls, each with their own view models.  This is easier said then done, as you need to make sure that you provide view models for each of the controls your view implements.  While this is probably the more accepted way of doing things, it leads to a lot of code noise and increased complexity when writing tests for your main view model.</p>
<p>There is another way and you can get there by using the Markup Extensions that are available to exend WPF.  Because I&#8217;m a bit to lazy to create my own custom binding extension, I used one that I found here <a href="http://www.hardcodet.net/2008/04/wpf-custom-binding-class ">http://www.hardcodet.net/2008/04/wpf-custom-binding-class</a> .  This fellow has already explored custom bindings and has created an easy to use base class for creating and manipulating custom WPF bindings.</p>
<p>With this class in place it is a fairly trivial operation to create a databinding class that utilizes your favorite dependency injector.  For this sample I am using StructureMap.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="csharp csharp" style="font-family:monospace;">    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Binds to a dependency injected object</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DIBinding <span style="color: #008000;">:</span> BindingDecoratorBase
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// The type we are looking for</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF;">public</span> Type Type <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// The key if applicable</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Key <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> ProvideValue<span style="color: #000000;">&#40;</span>IServiceProvider provider<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//use DI to get the value</span>
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>Type <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;You must specify a type for this binding&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            Source <span style="color: #008000;">=</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>Key<span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> ObjectFactory.<span style="color: #0000FF;">GetInstance</span><span style="color: #000000;">&#40;</span>Type<span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> ObjectFactory.<span style="color: #0000FF;">GetNamedInstance</span><span style="color: #000000;">&#40;</span>Type, Key<span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">ProvideValue</span><span style="color: #000000;">&#40;</span>provider<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Once we have created this binding helper we can use it in any xaml file provided that we include the proper namespaces of course.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TreeView</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{DataBinding:DIBinding Type=MainNav:IMainNavigationMenu, Path=MenuItems}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>Use of dependency injection in your xaml files should be limited to where it is appropriate.  Of course it is not appropriate to do you whole application in this manner, but for certain controls it can lead to a much cleaner more testable view model for rest of your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://slagd.com/?feed=rss2&amp;p=38</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Version 0.2.0.0 is Out!</title>
		<link>http://slagd.com/?p=36</link>
		<comments>http://slagd.com/?p=36#comments</comments>
		<pubDate>Fri, 24 Oct 2008 21:02:26 +0000</pubDate>
		<dc:creator>Daniel Guenter</dc:creator>
				<category><![CDATA[NHibernate.Remote]]></category>

		<guid isPermaLink="false">http://slagd.com/?p=36</guid>
		<description><![CDATA[Ok I&#8217;ll admit that it&#8217;s a bit later than I would have liked, but the next version of NHibernate.Remote is out.  While I&#8217;m sure that you&#8217;d like to read a lengthy article about blah this, blah that, and how hard it was with all of the other demands at work; I just don&#8217;t feel like [...]]]></description>
			<content:encoded><![CDATA[<p>Ok I&#8217;ll admit that it&#8217;s a bit later than I would have liked, but the next version of NHibernate.Remote is out.  While I&#8217;m sure that you&#8217;d like to read a lengthy article about blah this, blah that, and how hard it was with all of the other demands at work; I just don&#8217;t feel like writing all that out here.  So let&#8217;s just start of with a list of things that have changed.</p>
<ul>
<li>Linq queries are now supported</li>
<li>Criteria queries are now supported</li>
<li>All new enities can now be saved</li>
<li>Entities on the client are change tracked and the changes are automatically submitted with a flush</li>
<li>Many more methods of ISession are now working</li>
<li>Better session concurrency</li>
</ul>
<p>However there are still things that are being worked on, most noteably.</p>
<ul>
<li>Transactions</li>
<li>Filters</li>
<li>Named queries</li>
<li>Sql queries</li>
<li>Change tracking on collections</li>
<li>Attatching transient entities</li>
</ul>
<p>For a more complete list check out the <a href="http://slagd.com/files/NHibernateRemote.pdf">documentation</a>.</p>
<p>The underlying mechanisms have changed and evolved considerably since the last version.  However bear in mind that because of the vast number of entity combinations and queries, I haven&#8217;t had a chance to write tests for every senario, especially the more complex ones.</p>
<p>If you are adventuresome, grab the binaries or the sample project from the <a href="http://slagd.com/?page_id=26">downloads</a> page and give it a try. This version requires <a href="http://sourceforge.net/project/showfiles.php?group_id=73818">NHibernate 2.0.1GA</a> and if you are using the binaries only, you will also need <a href="http://www.postsharp.org/">Post Sharp</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://slagd.com/?feed=rss2&amp;p=36</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Forums are here</title>
		<link>http://slagd.com/?p=24</link>
		<comments>http://slagd.com/?p=24#comments</comments>
		<pubDate>Thu, 23 Oct 2008 17:12:03 +0000</pubDate>
		<dc:creator>Daniel Guenter</dc:creator>
				<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://slagd.com/?p=24</guid>
		<description><![CDATA[Due to the number of comments that I have been getting on this blog and the emails that I have been receiving,  I have decided to setup a small forum for questions and comments.  Feel free to drop by and add your 2 cents on NHibernate.Remote, Generic Messaging or just anything in general.  I will [...]]]></description>
			<content:encoded><![CDATA[<p>Due to the number of comments that I have been getting on this blog and the emails that I have been receiving,  I have decided to setup a small forum for questions and comments.  Feel free to drop by and add your 2 cents on NHibernate.Remote, Generic Messaging or just anything in general.  I will try to answer any questions directed to me in a timely fashion (I hope).</p>
]]></content:encoded>
			<wfw:commentRss>http://slagd.com/?feed=rss2&amp;p=24</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Serialization bites again.</title>
		<link>http://slagd.com/?p=22</link>
		<comments>http://slagd.com/?p=22#comments</comments>
		<pubDate>Thu, 09 Oct 2008 18:36:09 +0000</pubDate>
		<dc:creator>Daniel Guenter</dc:creator>
				<category><![CDATA[Code ramblings]]></category>
		<category><![CDATA[NHibernate.Remote]]></category>

		<guid isPermaLink="false">http://slagd.com/?p=22</guid>
		<description><![CDATA[I&#8217;ve been back working on NHibernate.Remote for the last week since I finished some of the more urgent updates for the company where I&#8217;m working.  I&#8217;ve come to the sad realization that the path I&#8217;ve been treading with the initial NHibernate.Remote codebase just won&#8217;t cut it for several reasons.
1) NHibernate needs a session factory.  You [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been back working on NHibernate.Remote for the last week since I finished some of the more urgent updates for the company where I&#8217;m working.  I&#8217;ve come to the sad realization that the path I&#8217;ve been treading with the initial NHibernate.Remote codebase just won&#8217;t cut it for several reasons.</p>
<p>1) NHibernate needs a session factory.  You just can&#8217;t operate remotely or locally without a session factory.  Initially my goal was to transfer the required factory information across the wire to the remote client. However this approach doesn&#8217;t work very well as SessionFactory information is deeply nested and not conducive to serialization.  So I&#8217;ve opted for the next best thing which is to create an identical session factory on both ends of the wire.  That way both the server and the client can be aware of Entities, Collections and Proxies.  The only thing that the remote session factory does not get is a connection string, so it cannot be used to open a direct database connection.  So far this has been working fairly well and has enabled me to get both Criteria queries and NHibernate.Linq queries working over the wire.</p>
<p>2) That was the good news, now for the bad news.  The remote client simply needs to be aware of the entities that it is holding.  That means that every time an entity or a collection comes in over the wire it needs to affect the persistence context.  If this can be implemented flushing the remote session will become much easier.  I thought that the ObjectGraphWalker that I had created would be up to this task but once again I appear to be mistaken.   One of the requirements  I need is that I need to be able to replace an entity as I walk the object graph.  Let me explain further.  In a local NHibernate session if you query an object, change some properties and then query that object again, NHibernate is smart enough to detect that it already has that entity and will return to you the original changed object.  I need to do that remotely, thus the requirement for substituting objects when I walk an incoming graph.  That is simply impractical with the way that the ObjectGraphWalker works.</p>
<p>So I&#8217;m back to needing custom access to the inner workings of a serializer. I&#8217;ve tried to hack my way into the NetDataContractSerializer but with little success.  Surely my requirement is neither original or unique and Microsoft would do well to provide more open access to their serialization techniques.  Most likely I&#8217;ll just have to use reflector to extract the functionality that I need.  Sigh.</p>
]]></content:encoded>
			<wfw:commentRss>http://slagd.com/?feed=rss2&amp;p=22</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
