<?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>Agile at Work &#187; ASP.NET MVC</title>
	<atom:link href="http://www.agileatwork.com/tag/aspnet-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.agileatwork.com</link>
	<description>by Michael Valenty</description>
	<lastBuildDate>Sat, 10 Sep 2011 14:35:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bolt-on Multi-Tenancy in ASP.NET MVC with Unity and NHibernate: Part II &#8211; Commingled Data</title>
		<link>http://www.agileatwork.com/bolt-on-multi-tenancy-in-asp-net-mvc-with-unity-and-nhibernate-part-ii-commingled-data/</link>
		<comments>http://www.agileatwork.com/bolt-on-multi-tenancy-in-asp-net-mvc-with-unity-and-nhibernate-part-ii-commingled-data/#comments</comments>
		<pubDate>Sat, 18 Jun 2011 15:03:05 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Inversion of Control]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Open Closed Principle]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.agileatwork.com/?p=580</guid>
		<description><![CDATA[Last time I went over going from separate web applications per tenant to a shared web application for all tenants, but each tenant still had its own database. Now we’re going to take the next step and let multiple tenants share the same database. After we add tenant_id to most of the tables in our [...]]]></description>
			<content:encoded><![CDATA[<p>Last time I went over going <a href="http://www.agileatwork.com/bolt-on-multi-tenancy-in-asp-net-mvc-with-unity-and-nhibernate/">from separate web applications per tenant to a shared web application for all tenants</a>, but each tenant still had its own database. Now we’re going to take the next step and let multiple tenants share the same database. After we add <font size="2" face="Courier New">tenant_id</font> to most of the tables in our database we’ll need the application to take care of a few things.</p>
<p>First, we need to apply a where clause to all queries to ensure that each tenant sees only their data. This is pretty painless with NHibernate and completely transparent to the existing application! We just have to define a parameterized filter:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">hibernate-mapping</span><span style="color: blue"> </span><span style="color: red">xmlns</span><span style="color: blue">=</span>&quot;<span style="color: blue">urn:nhibernate-mapping-2.2</span>&quot;<span style="color: blue">&gt;</span></pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px"><span style="color: blue">&#160; &lt;</span><span style="color: #a31515">filter-def</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">tenant</span>&quot;<span style="color: blue">&gt;</span></pre>
<pre style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">filter-param</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">id</span>&quot;<span style="color: blue"> </span><span style="color: red">type</span><span style="color: blue">=</span>&quot;<span style="color: blue">System.Int32</span>&quot;<span style="color: blue"> /&gt;</span></pre>
<pre style="margin: 0px"><span style="color: blue">&#160; &lt;/</span><span style="color: #a31515">filter-def</span><span style="color: blue">&gt;</span></pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px"><span style="color: blue">&lt;/</span><span style="color: #a31515">hibernate-mapping</span><span style="color: blue">&gt;</span></pre>
</div>
<p>And then apply it to each entity:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">class</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">User</span>&quot;<span style="color: blue"> </span><span style="color: red">table</span><span style="color: blue">=</span>&quot;<span style="color: blue">[user]</span>&quot;<span style="color: blue">&gt;</span></pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px"><span style="color: blue">&#160; &lt;</span><span style="color: #a31515">id</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">Id</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">user_id</span>&quot;<span style="color: blue">&gt;</span></pre>
<pre style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">generator</span><span style="color: blue"> </span><span style="color: red">class</span><span style="color: blue">=</span>&quot;<span style="color: blue">identity</span>&quot;<span style="color: blue"> /&gt;</span></pre>
<pre style="margin: 0px"><span style="color: blue">&#160; &lt;/</span><span style="color: #a31515">id</span><span style="color: blue">&gt;</span></pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px"><span style="color: blue">&#160; &lt;</span><span style="color: #a31515">property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">Username</span>&quot;<span style="color: blue"> /&gt;</span></pre>
<pre style="margin: 0px"><span style="color: blue">&#160; &lt;</span><span style="color: #a31515">property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">Email</span>&quot;<span style="color: blue"> /&gt;</span></pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="background-color: #ffffc0; margin: 0px"><span style="color: blue">&#160; &lt;</span><span style="color: #a31515">filter</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">tenant</span>&quot;<span style="color: blue"> </span><span style="color: red">condition</span><span style="color: blue">=</span>&quot;<span style="color: blue">tenant_id = :id</span>&quot;<span style="color: blue"> /&gt;</span></pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px"><span style="color: blue">&lt;/</span><span style="color: #a31515">class</span><span style="color: blue">&gt;</span></pre>
</div>
<p>The last step is to set the value of the filter at runtime. This is done on the <font size="2" face="Courier New">ISession</font> like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">Container</pre>
<pre style="margin: 0px">&#160;&#160;&#160; .RegisterType&lt;<span style="color: #2b91af">ISession</span>&gt;(</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">PerRequestLifetimeManager</span>(),</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">InjectionFactory</span>(c =&gt;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> session = c.Resolve&lt;<span style="color: #2b91af">ISessionFactory</span>&gt;().OpenSession();</pre>
<pre style="background-color: #ffffc0; margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; session.EnableFilter(<span style="color: #a31515">&quot;tenant&quot;</span>).SetParameter(<span style="color: #a31515">&quot;id&quot;</span>, c.Resolve&lt;<span style="color: #2b91af">Tenant</span>&gt;().Id);</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> session;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; })</pre>
<pre style="margin: 0px">&#160;&#160;&#160; );</pre>
</div>
<p>The current tenant comes from <font size="2" face="Courier New">c.Resolve&lt;Tenant&gt;()</font>. In order for that to work, you have to tell Unity how to find the current tenant. In ASP.NET MVC, we can look at the host header on the request and find our tenant that way. We could just as easily use another strategy though. Maybe if this were a WCF service, we could use an authentication header to establish the current tenant context. You could build out some interfaces and strategies around establishing the current tenant context, however for this article I’ll just bang it out.</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">Container</pre>
<pre style="margin: 0px">&#160;&#160;&#160; .RegisterType&lt;<span style="color: #2b91af">Tenant</span>&gt;(<span style="color: blue">new</span> <span style="color: #2b91af">InjectionFactory</span>(c =&gt;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> repository = c.Resolve&lt;<span style="color: #2b91af">ITenantRepository</span>&gt;();</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> context = c.Resolve&lt;<span style="color: #2b91af">HttpContextBase</span>&gt;();</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> host = context.Request.Headers[<span style="color: #a31515">&quot;Host&quot;</span>] ?? context.Request.Url.Host;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> repository.FindByHost(host);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }));</pre>
</div>
<p>Second, we have to set the <font size="2" face="Courier New">tenant_id</font> when new entities are saved. This is a bit more complicated with NHibernate and requires a bit of a concession in that we have to add a field to the entity in order for NHibernate to know how to persist the value. I’m using a private nullable int for this.</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">User</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="background-color: #ffffc0; margin: 0px">&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">int</span>? tenantId;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">int</span> Id { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">string</span> Username { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">string</span> Email { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>It’s private because I don’t want the business logic to deal with it and it’s nullable because my tenant table is in a separate database which means I can’t lean on the data model to enforce referential integrity. That’s a problem because the default value for an integer is zero which could be happily saved by the database. By making it nullable I can be sure the database will blow up if the <font size="2" face="Courier New">tenant_id</font> is not set.</p>
<p>So, back to the issue at hand. The <font size="2" face="Courier New">tenant_id</font> needs to be set when the entity is saved. For this, I’m using an interceptor and setting the value in the <font size="2" face="Courier New">OnSave</font> method:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MultiTenantInterceptor</span> : <span style="color: #2b91af">EmptyInterceptor</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">Tenant</span>&gt; tenant;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> MultiTenantInterceptor(<span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">Tenant</span>&gt; tenant)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.tenant = tenant;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">bool</span> OnSave(<span style="color: blue">object</span> entity... <span style="color: blue">object</span>[] state, <span style="color: blue">string</span>[] propertyNames...)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> index = <span style="color: #2b91af">Array</span>.IndexOf(propertyNames, <span style="color: #a31515">&quot;tenantId&quot;</span>);</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (index == -1)</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">false</span>;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> tenantId = tenant().Id;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; state[index] = tenantId;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; entity</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .GetType()</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .GetField(<span style="color: #a31515">&quot;tenantId&quot;</span>, <span style="color: #2b91af">BindingFlags</span>.Instance | <span style="color: #2b91af">BindingFlags</span>.NonPublic)</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .SetValue(entity, tenantId);</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">false</span>;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>This <font size="2" face="Courier New">IInterceptor</font> mechanism is a little wonky. If you change any data, you have to do it in both the entity instance and the <font size="2" face="Courier New">state</font> array that NHibernate uses to hydrate entities. It’s not a big deal, it’s just one of those things you have to accept like the fact that Apple and Google are tracking your every move via your smart phone. Oh, and the interceptor gets wired up like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">Container</pre>
</div>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">&#160;&#160;&#160; .RegisterType&lt;<span style="color: #2b91af">ISessionFactory</span>&gt;(</pre>
</div>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ContainerControlledLifetimeManager</span>(),</pre>
</div>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">InjectionFactory</span>(c =&gt;</pre>
</div>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
</div>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">new</span> NHibernate.Cfg.<span style="color: #2b91af">Configuration</span>()</pre>
</div>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Configure()</pre>
</div>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="background-color: #ffffc0; margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .SetInterceptor(<span style="color: blue">new</span> <span style="color: #2b91af">MultiTenantInterceptor</span>(() =&gt; c.Resolve&lt;<span style="color: #2b91af">Tenant</span>&gt;()))</pre>
</div>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .BuildSessionFactory();</pre>
</div>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; })</pre>
</div>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">&#160;&#160;&#160; );</pre>
</div>
<p>We’re almost done. There is one more case that needs to be handled. When NHibernate loads an entity by its primary key, it doesn’t run through the query engine which means the tenant filter isn’t applied. Fortunately, we can take care of this in the interceptor:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MultiTenantInterceptor</span> : <span style="color: #2b91af">EmptyInterceptor</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; ...</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">bool</span> OnLoad(<span style="color: blue">object</span> entity... <span style="color: blue">object</span>[] state, <span style="color: blue">string</span>[] propertyNames...)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> index = <span style="color: #2b91af">Array</span>.IndexOf(propertyNames, <span style="color: #a31515">&quot;tenantId&quot;</span>);</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (index == -1)</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">false</span>;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> entityTenantId = <span style="color: #2b91af">Convert</span>.ToInt32(state[index]);</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> currentTenantId = tenant().Id;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (entityTenantId != currentTenantId)</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">AuthorizationException</span>(<span style="color: #a31515">&quot;Permission denied to {0}&quot;</span>, entity);</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">false</span>;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>That’s it. Have fun and happy commingling.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/bolt-on-multi-tenancy-in-asp-net-mvc-with-unity-and-nhibernate-part-ii-commingled-data/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bolt-on Multi-Tenancy in ASP.NET MVC with Unity and NHibernate</title>
		<link>http://www.agileatwork.com/bolt-on-multi-tenancy-in-asp-net-mvc-with-unity-and-nhibernate/</link>
		<comments>http://www.agileatwork.com/bolt-on-multi-tenancy-in-asp-net-mvc-with-unity-and-nhibernate/#comments</comments>
		<pubDate>Sat, 14 May 2011 15:59:32 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Inversion of Control]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Open Closed Principle]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.agileatwork.com/?p=564</guid>
		<description><![CDATA[The Mission: 
Build a web application as though it’s for a single customer (tenant) and add multi-tenancy as a bolt-on feature by writing only new code. There are flavors of multi-tenancy, in this case I want each tenant to have its own database but I want all tenants to share the same web application and [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The Mission:</strong> </p>
<p>Build a web application as though it’s for a single customer (tenant) and add multi-tenancy as a bolt-on feature by writing only new code. There are flavors of multi-tenancy, in this case I want each tenant to have its own database but I want all tenants to share the same web application and figure out who’s who by looking at the host header on the http request.</p>
<p><strong>The Plan:</strong></p>
<p>To pull this off, we’re going to have to rely on our SOLID design principles, especially Single Responsibility and Dependency Inversion. We’ll get some help from these frameworks:</p>
<ul>
<li><a href="http://www.asp.net/mvc">ASP.NET MVC</a> </li>
<li><a href="http://unity.codeplex.com/">Unity</a> </li>
<li><a href="http://nhforge.org/Default.aspx">NHibernate</a> </li>
</ul>
<p><strong>Game on:</strong></p>
<p>Let’s take a look at a controller that uses NHibernate to talk to the database. I’m not going to get into whether you should talk directly to NHibernate from the controller or go through a service layer or repository because it doesn’t affect how we’re going to add multi-tenancy. The important thing here is that the <font size="2" face="Courier New">ISession</font> is <em>injected</em> into the controller, and we aren’t using the service locator pattern to request the <font size="2" face="Courier New">ISession</font> from a singleton.</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">UserController</span> : <span style="color: #2b91af">Controller</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">ISession</span> session;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> UserController(<span style="color: #2b91af">ISession</span> session)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.session = session;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">ActionResult</span> Edit(<span style="color: blue">int</span> id)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> user = session.Load&lt;<span style="color: #2b91af">User</span>&gt;(id);</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> View(user);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>Alright, now it’s time to write some new code and make our web application connect to the correct database based on the host header in the http request. First, we’ll need a database to store a list of tenants along with the connection string for that tenant’s database. Here’s my entity:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">Tenant</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">string</span> Name { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">string</span> Host { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">string</span> ConnectionString { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>I’ll use the repository pattern here so there is a crisp consumer of the <font size="2" face="Courier New">ISession</font> that connects to the lookup database rather than one of the tenant shards. This will be important later when we go to configure Unity.</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">NHibernateTenantRepository</span> : <span style="color: #2b91af">ITenantRepository</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">ISession</span> session;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">HttpContextBase</span> context;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> NHibernateTenantRepository(<span style="color: #2b91af">ISession</span> session, <span style="color: #2b91af">HttpContextBase</span> context)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.session = session;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.context = context;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Tenant</span> Current</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">get</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> host = context.Request.Headers[<span style="color: #a31515">&quot;Host&quot;</span>];</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> FindByHost(host);</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Tenant</span> FindByHost(<span style="color: blue">string</span> host)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> session</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Query&lt;<span style="color: #2b91af">Tenant</span>&gt;()</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .SingleOrDefault(t =&gt; t.Host == host);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>So now we need a dedicated <font size="2" face="Courier New">ISessionFactory</font> for the lookup database and make sure that our <font size="2" face="Courier New">NHibernateTenantRepository</font> gets the right <font size="2" face="Courier New">ISession</font> . It’s not too bad, we just need to name them in the container so we can refer to them explicitly.</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">Container</pre>
<pre style="margin: 0px">&#160;&#160;&#160; .RegisterType&lt;<span style="color: #2b91af">ISessionFactory</span>&gt;(</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="background-color: #ffffc0; color: #a31515">&quot;tenant_session_factory&quot;</span>,</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ContainerControlledLifetimeManager</span>(),</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">InjectionFactory</span>(c =&gt;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> NHibernate.Cfg.<span style="color: #2b91af">Configuration</span>().Configure().BuildSessionFactory())</pre>
<pre style="margin: 0px">&#160;&#160;&#160; );</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">Container</pre>
<pre style="margin: 0px">&#160;&#160;&#160; .RegisterType&lt;<span style="color: #2b91af">ISession</span>&gt;(</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="background-color: #ffffc0; color: #a31515">&quot;tenant_session&quot;</span>,</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">PerRequestLifetimeManager</span>(),</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">InjectionFactory</span>(c =&gt;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; c.Resolve&lt;<span style="color: #2b91af">ISessionFactory</span>&gt;(<span style="background-color: #ffffc0; color: #a31515">&quot;tenant_session_factory&quot;</span>).OpenSession())</pre>
<pre style="margin: 0px">&#160;&#160;&#160; );</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">Container</pre>
<pre style="margin: 0px">&#160;&#160;&#160; .RegisterType&lt;<span style="color: #2b91af">ITenantRepository</span>, <span style="color: #2b91af">NHibernateTenantRepository</span>&gt;()</pre>
<pre style="margin: 0px">&#160;&#160;&#160; .RegisterType&lt;<span style="color: #2b91af">NHibernateTenantRepository</span>&gt;(</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">InjectionConstructor</span>(</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ResolvedParameter</span>&lt;<span style="color: #2b91af">ISession</span>&gt;(<span style="background-color: #ffffc0; color: #a31515">&quot;tenant_session&quot;</span>),</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ResolvedParameter</span>&lt;<span style="color: #2b91af">HttpContextBase</span>&gt;()</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</pre>
<pre style="margin: 0px">&#160;&#160;&#160; );</pre>
</div>
<p>Hopefully that’s about what you were expecting since it’s not really the interesting part. The more interesting part is configuring the <font size="2" face="Courier New">ISession</font> that gets injected into the <font size="2" face="Courier New">UserController</font> to connect to a different database based on the host header in the http request. The Unity feature we’re going to leverage for this is the <font size="2" face="Courier New">LifetimeManager</font>. This is an <a href="http://www.agileatwork.com/captcha-and-inversion-of-control/">often overlooked feature</a> of IoC containers.</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">Container</pre>
<pre style="margin: 0px">&#160;&#160;&#160; .RegisterType&lt;<span style="color: #2b91af">ISessionFactory</span>&gt;(</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="background-color: #ffffc0; color: #2b91af">PerHostLifetimeManager</span>(() =&gt; <span style="color: blue">new</span> <span style="color: #2b91af">HttpContextWrapper</span>(<span style="color: #2b91af">HttpContext</span>.Current)),</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">InjectionFactory</span>(c =&gt;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> connString = c</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Resolve&lt;<span style="color: #2b91af">ITenantRepository</span>&gt;()</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Current</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .ConnectionString;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">new</span> NHibernate.Cfg.<span style="color: #2b91af">Configuration</span>()</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Configure()</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .SetProperty(NHibernate.Cfg.<span style="color: #2b91af">Environment</span>.ConnectionString, connString)</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .BuildSessionFactory();</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }));</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">Container</pre>
<pre style="margin: 0px">&#160;&#160;&#160; .RegisterType&lt;<span style="color: #2b91af">ISession</span>&gt;(</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">PerRequestLifetimeManager</span>(),</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">InjectionFactory</span>(c =&gt; c.Resolve&lt;<span style="color: #2b91af">ISessionFactory</span>&gt;().OpenSession())</pre>
<pre style="margin: 0px">&#160;&#160;&#160; );</pre>
</div>
<p>Here we’re using a custom <font size="2" face="Courier New">PerHostLifetimeManager</font>. This tells Unity to maintain a session factory per host. When Unity runs across a host it doesn’t have a session factory for, it will run the <font size="2" face="Courier New">InjectionFactory</font> block to create one using the connection string associated with that tenant.</p>
<p>Since multiple simultaneous requests will be trying to get and set values <em>with the same key</em>, we need to make sure our <font size="2" face="Courier New">PerHostLifetimeManager</font> is thread safe. That’s pretty easy since Unity comes with a <font size="2" face="Courier New">SynchronizedLifetimeManager</font> base class that takes care of the fact that <font size="2" face="Courier New">Dictionary</font> <a href="http://stackoverflow.com/questions/157933/whats-the-best-way-of-implementing-a-thread-safe-dictionary-in-net">isn’t thread safe</a>.</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">PerHostLifetimeManager</span> : <span style="background-color: #ffffc0; color: #2b91af">SynchronizedLifetimeManager</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">HttpContextBase</span>&gt; context;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IDictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; store;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> PerHostLifetimeManager(<span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">HttpContextBase</span>&gt; context)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.context = context;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; store = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">object</span> SynchronizedGetValue()</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> host = GetHost();</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!store.ContainsKey(host))</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">null</span>;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> store[host];</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> SynchronizedSetValue(<span style="color: blue">object</span> newValue)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; store[GetHost()] = newValue;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">string</span> GetHost()</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> context().Request.Headers[<span style="color: #a31515">&quot;Host&quot;</span>];</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
</div>
<p>So what did we accomplish? Well we didn’t touch any of our existing application code. We just wrote new code and through configuration we added multi-tenancy! That’s pretty cool, but was it worth it? Well, the goal in itself isn’t super important, but this exercise can certainly highlight areas of your codebase where you might be violating the single responsibility principle or leaking too many infrastructure concepts into your application logic.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/bolt-on-multi-tenancy-in-asp-net-mvc-with-unity-and-nhibernate/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Inheritance is Evil: The Epic Fail of the DataAnnotationsModelBinder</title>
		<link>http://www.agileatwork.com/inheritance-is-evil-the-story-of-the-epic-fail-of-dataannotationsmodelbinder/</link>
		<comments>http://www.agileatwork.com/inheritance-is-evil-the-story-of-the-epic-fail-of-dataannotationsmodelbinder/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 14:52:10 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Decorator Pattern]]></category>
		<category><![CDATA[Single Responsibility Principle]]></category>

		<guid isPermaLink="false">http://www.agileatwork.com/?p=312</guid>
		<description><![CDATA[The DataAnnotationsModelBinder is a pretty neat little class that enforces validation attributes on models like this one:
public class Product {
    public int Id { get; set; }

    [Required]
    public string Description { get; set; }

    [Required]
    [RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
    [...]]]></description>
			<content:encoded><![CDATA[<p>The <font face="Courier New" size="2">DataAnnotationsModelBinder</font> is a pretty neat little class that enforces validation attributes on models like this one:</p>
<pre class="prettyprint"><code>public class Product {
    public int Id { get; set; }

    [Required]
    public string Description { get; set; }

    [Required]
    [RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
    public decimal UnitPrice { get; set; }
}</code></pre>
<p>Just for the record, this is a <a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471">cool bit of code</a> that was released to the community for backseat drivers like me to use and complain about. I&#8217;m really only bitter because I can&#8217;t use it. Let&#8217;s take a peek at the source code to see why:</p>
<pre class="prettyprint"><code>/// &lt;summary&gt;
/// An implementation of IModelBinder which is designed to be a replacement
/// default model binder, adding the functionality of validation via the validation
/// attributes in System.ComponentModel.DataAnnotations from .NET 4.0.
/// &lt;/summary&gt;
public class DataAnnotationsModelBinder : DefaultModelBinder {
    ...
}</code></pre>
<p>The problem with this class is that it&#8217;s not just about validation. It&#8217;s more about binding with validation sprinkled in. The author even states this as a design goal with the comment &#8220;designed to be a replacement default model binder.&#8221; This is a violation of the Single Responsibility Principle and it&#8217;s the reason I can&#8217;t use it. The <font face="Courier New" size="2">DefaultModelBinder</font> does some basic reflection mapping of form elements to model properties. That&#8217;s cool, but what about a controller action that takes json, xml, or anything slightly more complicated than what the default binder can handle?</p>
<p>I&#8217;ll tell you what happens. You write a <a href="http://www.agileatwork.com/custom-model-binder-in-asp-net-mvc/">custom model binder</a> which is pretty easy and happens to be a pretty rad extension point in the MVC framework. Consider this model binder that deserializes json:</p>
<pre class="prettyprint"><code>public class JsonModelBinder&lt;T&gt; : IModelBinder {
    private string key;

    public JsonModelBinder(string requestKey) {
        this.key = requestKey;
    }

    public object BindModel(ControllerContext controllerContext, ...) {
        var json = controllerContext.HttpContext.Request[key];
        return new JsonSerializer().Deserialize&lt;T&gt;(json);
    }
}</code></pre>
<p>That&#8217;s pretty cool, but now I&#8217;ve lost my validation! This my friend is the evil of inheritance and the epic fail of the <font face="Courier New" size="2">DataAnnotationsModelBinder</font>. So what’s wrong with inheritance, isn’t that what OOP is all about? The short answer is no (the long answer is also no.)</p>
<p><img title="peter-venkman-ghostbusters" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="224" alt="peter-venkman-ghostbusters" src="http://www.agileatwork.com/wp-content/uploads/2010/01/petervenkmanghostbusters.jpg" width="300" border="0" /> </p>
<blockquote>
<p>Human sacrifice, dogs and cats living together&#8230; mass hysteria! &#8212; Dr. Peter Venkman</p>
</blockquote>
<p>Inheritance is pretty enticing especially coming from procedural-land and it often looks deceptively elegant. I mean all I need to do is add this one bit of functionality to another other class, right? Well, one of the problems is that inheritance is probably the worst form of coupling you can have. Your base class breaks encapsulation by exposing implementation details to subclasses in the form of protected members. This makes your system rigid and fragile.</p>
<p>The more tragic flaw however is the new subclass brings with it all the baggage and opinion of the inheritance chain. Why does a model that enforces validation attributes care how the model is constructed? The answer is it shouldn&#8217;t, and to add insult to injury, the better design is actually easier to implement. Listen closely and you may hear the wise whispers of our forefathers&#8230;</p>
<p><img title="Ward Cunningham" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="117" alt="Ward Cunningham" src="http://www.agileatwork.com/wp-content/uploads/2010/01/ward.jpg" width="92" border="0" /> <img title="Martin Fowler" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="117" alt="Martin Fowler" src="http://www.agileatwork.com/wp-content/uploads/2010/01/martin.jpg" width="94" border="0" /> <img title="Kent Beck" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="117" alt="Kent Beck" src="http://www.agileatwork.com/wp-content/uploads/2010/01/kent.jpg" width="78" border="0" /> <img title="Robert Martin" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="117" alt="Robert Martin" src="http://www.agileatwork.com/wp-content/uploads/2010/01/robert.jpg" width="87" border="0" /> </p>
<blockquote>
<p>Use the decorator pattern.</p>
</blockquote>
<p>Is this really going to solve all my problems? The short answer is yes. Let&#8217;s take a look:</p>
<pre class="prettyprint"><code>public class BetterDataAnnotationsModelBinder : IModelBinder {
    private IModelBinder binder;

    public BetterDataAnnotationsModelBinder(IModelBinder binder) {
        this.binder = binder;
    }

    public object BindModel(ControllerContext controllerContext, ...) {
        var model = binder.BindModel(controllerContext, bindingContext);
        AddValidationErrorsToModelState(model, controllerContext);
        return model;
    }

    ...
}</code></pre>
<p>Now all this class does is enforce validation and we can use whatever model binder we want to construct our object. Let&#8217;s wire this up in the Global.asax real quick: </p>
<pre class="prettyprint"><code>var productModelBinder = new JsonModelBinder&lt;Product&gt;("product");

ModelBinders.Binders.Add(
    typeof(Product),
    new BetterDataAnnotationsModelBinder(productModelBinder));</code></pre>
<p>This is cool for the following reasons:</p>
<ol>
<li>We are not coupled to the <font face="Courier New" size="2">DefaultModelBinder</font>. This means we don&#8217;t know or care about the structure and implementation details of it so it&#8217;s free to change in the next version of ASP.NET MVC. </li>
<li>The code is simpler because it is only about validation. </li>
<li>The behavior is composable. This means we can add validation to any model binder (like our json model binder) and bolt on more responsibilities with a chain of decorators. How about converting <font face="Courier New" size="2">DateTime</font> fields to UTC or striping formatting characters from phone numbers? </li>
<li>It&#8217;s easy to test. </li>
</ol>
<p>You can substitute a decorator for a subclass in most cases and in most cases it&#8217;s the right choice. You&#8217;re better off making the decorator your default choice and only settle for inheritance when you really need it.</p>
<p><img title="kip" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="184" alt="kip" src="http://www.agileatwork.com/wp-content/uploads/2010/01/kip.jpg" width="170" border="0" /> </p>
<blockquote>
<p>&#8230;don&#8217;t be jealous that I&#8217;ve been chatting online with babes all day. Besides, we both know that I&#8217;m training to be a cage fighter. &#8212; Kip</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/inheritance-is-evil-the-story-of-the-epic-fail-of-dataannotationsmodelbinder/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Custom Model Binder in ASP.NET MVC</title>
		<link>http://www.agileatwork.com/custom-model-binder-in-asp-net-mvc/</link>
		<comments>http://www.agileatwork.com/custom-model-binder-in-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 22:59:22 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Command Query Separation]]></category>
		<category><![CDATA[Decorator Pattern]]></category>
		<category><![CDATA[Single Responsibility Principle]]></category>

		<guid isPermaLink="false">http://www.agileatwork.com/?p=298</guid>
		<description><![CDATA[The default model binder or DataAnnotationsModelBinder in ASP.NET MVC works well for simple data structures that basically map properties directly to form elements, but it quickly breaks down in a real world application. Take this simple scenario:

The built-in model binder can handle the easy fields like FirstName, LastName, and Email, but it doesn’t know how [...]]]></description>
			<content:encoded><![CDATA[<p>The default model binder or <font face="Courier New" size="2">DataAnnotationsModelBinder</font> in ASP.NET MVC works well for simple data structures that basically map properties directly to form elements, but it quickly breaks down in a real world application. Take this simple scenario:</p>
<p><img title="brimley" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="277" alt="brimley" src="http://www.agileatwork.com/wp-content/uploads/2009/12/brimley.png" width="253" border="0" /></p>
<p>The built-in model binder can handle the easy fields like <font face="Courier New" size="2">FirstName</font>, <font face="Courier New" size="2">LastName</font>, and <font face="Courier New" size="2">Email</font>, but it doesn’t know how to deal with the roles. This is where the custom model binder comes in:</p>
<pre class="prettyprint"><code>public class UserModelBinder : IModelBinder {
    private readonly IModelBinder binder;

    public UserModelBinder(IModelBinder binder) {
        this.binder = binder;
    }

    public object BindModel(ControllerContext controllerContext, ...) {
        var user = (User)binder.BindModel(controllerContext, bindingContext);
        AddRoles(user, controllerContext);
        return user;
    }

    private static void AddRoles(User user, ControllerContext controllerContext) {
        foreach (var role in GetRoles(controllerContext)) {
            user.Add(role);
        }
    }

    private static IEnumerable&lt;Role&gt; GetRoles(ControllerContext controllerContext) {
        var roles = controllerContext.HttpContext.Request["roles"];
        if (roles == null) yield break;
        foreach (var roleId in roles.Split(',')) {
            yield return (Role)Convert.ToInt32(roleId);
        }
    }
}</code></pre>
<p>And it gets wired-up in the Global.asax like this:</p>
<pre class="prettyprint"><code>var defaultBinder = new DataAnnotationsModelBinder();
ModelBinders.Binders.DefaultBinder = defaultBinder;
ModelBinders.Binders.Add(typeof(User), new UserModelBinder(defaultBinder));</code></pre>
<p>Because <font face="Courier New" size="2">UserModelBinder</font> is a decorator, it preserves the base functionality (like validation) of the default binder and only adds the part about mapping the roles. This is good stuff because it keeps construction of the model out of the controller. That&#8217;s the Single-Responsibility Principle in action.</p>
<h3>Command Query Separation</h3>
<p>The two functions <font face="Courier New" size="2">AddRoles</font>, and <font face="Courier New" size="2">GetRoles</font> are an example of command query separation (CQS). It would be easy to combine those two functions together and in fact that&#8217;s how it looked at first. So why bother separating them? Well it doesn&#8217;t <em>really</em> matter in this simple case, but it’s like how I tell my 3 year-old son to move his milk cup away from the edge of the table. I figure it’s a good habit that will prevent a few spills in the long run. The idea with CQS is there is a natural seam between finding objects and doing stuff with them. I buy that. I expect <font face="Courier New" size="2">GetRoles</font> to be more volatile than <font face="Courier New" size="2">AddRoles</font> or at least change for different reasons.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/custom-model-binder-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Unit of Work with Unity and ASP.NET MVC</title>
		<link>http://www.agileatwork.com/unit-of-work-with-unity-and-aspnet-mvc/</link>
		<comments>http://www.agileatwork.com/unit-of-work-with-unity-and-aspnet-mvc/#comments</comments>
		<pubDate>Sun, 10 May 2009 07:58:14 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AOP]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://blog.agileatwork.com/?p=9</guid>
		<description><![CDATA[I was recently asked how I get the context of&#160; “this” in the UoW relating to the current page request.
 
Before I get into the guts, I would like to provide a little context. My application has 20+ databases scattered across 4 machines.

IRepository&#60;Customer&#62; customerRepository; // customer database on server 1
IRepository&#60;Package&#62;  packageRepository; // customer database [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently asked how I get the context of&#160; “this” in the UoW relating to the current page request.</p>
<p><a href="http://blog.agileatwork.com/wp-content/uploads/2009/05/howididit.jpg"><img title="howididit" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="179" alt="howididit" src="http://blog.agileatwork.com/wp-content/uploads/2009/05/howididit-thumb.jpg" width="240" border="0" /></a> </p>
<p>Before I get into the guts, I would like to provide a little context. My application has 20+ databases scattered across 4 machines.</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">IRepository&lt;Customer&gt; customerRepository; <span class="rem">// customer database on server 1</span>
IRepository&lt;Package&gt;  packageRepository; <span class="rem">// customer database on server 1</span>
IRepository&lt;Contact&gt;  contactRepository; <span class="rem">// contact database on server 2</span></pre>
<div>So, I might ask for a Customer object and a Package object and I want to get the same ISession for both and if I ask for the same Customer twice, I want to get the one from the 1st level cache (I’m using NHibernate). If I ask for a Contact object, I will get a different ISession. All opened sessions are managed by my UoW. So, when the page request is complete, I call UoW.Commit and all sessions are committed.</div>
<p>The “magic” if you will, happens in the global.asax. I was nosing around in Rhino.Commons for inspiration and adapted a technique I saw there. This is how it looks:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> GlobalApplication : HttpApplication
{
    <span class="kwrd">private</span> <span class="kwrd">static</span> IUnityContainer container;

    <span class="kwrd">public</span> GlobalApplication()
    {
        BeginRequest += <span class="kwrd">new</span> EventHandler(GlobalApplication_BeginRequest);
        EndRequest += <span class="kwrd">new</span> EventHandler(GlobalApplication_EndRequest);
    }

    <span class="kwrd">protected</span> <span class="kwrd">void</span> Application_Start(<span class="kwrd">object</span> sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);

        container = <span class="kwrd">new</span> UnityContainer();
        container.AddNewExtension&lt;PolicyInjectorContainerExtension&gt;();
        container.AddNewExtension&lt;HttpRequestLifetimeCoreContainerExtension&gt;();
        container.AddNewExtension&lt;WebMvcContainerExtension&gt;();

        ControllerBuilder.Current.SetControllerFactory(<span class="kwrd">new</span> UnityControllerFactory(container));
    }

    <span class="kwrd">void</span> GlobalApplication_BeginRequest(<span class="kwrd">object</span> sender, EventArgs e)
    {
        var unitOfWork = container.Resolve&lt;IUnitOfWork&gt;();
        unitOfWork.Start();
    }

    <span class="kwrd">void</span> GlobalApplication_EndRequest(<span class="kwrd">object</span> sender, EventArgs e)
    {
        var unitOfWork = container.Resolve&lt;IUnitOfWork&gt;();
        unitOfWork.Commit();
    }
}</pre>
<p>I register the UoW with an HttpRequestLifetimeManager so I get a new instance for each request.</p>
<pre class="csharpcode">Container.RegisterType&lt;IUnitOfWork&lt;ISession&gt;,
	NHibernateUnitOfWork&gt;(<span class="kwrd">new</span> HttpRequestLifetimeManager());</pre>
<div>My NHibernateRepository gets injected with the UoW for the current HttpRequest and when the request is complete, the global.asax commits the whole thing.</div>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> NHibernateRepository&lt;T&gt; : IRepository&lt;T&gt;
{
    <span class="kwrd">protected</span> ISession session;

    <span class="kwrd">public</span> NHibernateRepository(IUnitOfWork&lt;ISession&gt; unitOfWork)
    {
        session = unitOfWork.GetContextFor&lt;T&gt;();
    }

    ...

    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> Save(T obj)
    {
        session.Save(obj);
    }
}</pre>
<p>Now, this is all the context of an ASP.NET MVC Controller, but I have a similar issue for other (non-web) services. In that context I am using AOP and decorating a particular method with [UnitOfWork], which looks like:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWorkCallHander : ICallHandler
{
    <span class="kwrd">private</span> IUnitOfWork&lt;ISession&gt; unitOfWork;

    <span class="kwrd">public</span> UnitOfWorkCallHander(IUnitOfWork&lt;ISession&gt; unitOfWork)
    {
        <span class="kwrd">this</span>.unitOfWork = unitOfWork;
    }

    <span class="kwrd">public</span> <span class="kwrd">int</span> Order { get; set; }

    <span class="kwrd">public</span> IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        unitOfWork.Start();

        <span class="kwrd">try</span>
        {
            <span class="kwrd">return</span> getNext()(input, getNext);
        }
        <span class="kwrd">finally</span>
        {
            unitOfWork.Commit();
        }
    }
}</pre>
<p>In that context I use a PerThreadLifetimeManager for the NHibernateUnitOfWork, and code ends up looking like:</p>
<pre class="csharpcode">[UnitOfWork]
<span class="kwrd">public</span> <span class="kwrd">void</span> Process(Job job)
{
    ...
}</pre>
<p>You know, there is really no reason why you couldn’t do the same thing in the MVC context. You could basically ditch the global.asax event hooha and just annotate the Controller task:</p>
<pre class="csharpcode">[UnitOfWork]
<span class="kwrd">public</span> ActionResult ControllerTaskThatRequiresUoW()
{
    ...
}</pre>
<p>It’s more explicit than using the global.asax technique and it would allow you to specify different UoW behavior on each controller task:</p>
<pre class="csharpcode">[UnitOfWork(IsolationLevel.ReadCommitted]
<span class="kwrd">public</span> ActionResult SomeTaskThatShouldNotReadUncommitedData()
{
}

[UnitOfWork(IsolationLevel.ReadUncommitted)]
<span class="kwrd">public</span> ActionResult AnotherTaskWithDifferentRequirements()
{
}</pre>
<p>I’d like to try this out next time I get back into ASP.NET MVC, the global.asax event technique felt a little too magical and I don’t think Uncle Bob would approve.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/unit-of-work-with-unity-and-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An Order Processing Pipeline in ASP.NET MVC</title>
		<link>http://www.agileatwork.com/an-order-processing-pipeline-in-aspnet-mvc/</link>
		<comments>http://www.agileatwork.com/an-order-processing-pipeline-in-aspnet-mvc/#comments</comments>
		<pubDate>Sun, 03 May 2009 07:57:28 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Fluent Interface]]></category>

		<guid isPermaLink="false">http://blog.agileatwork.com/?p=8</guid>
		<description><![CDATA[Lately, I can’t seem to shake the pipeline pattern. It keeps popping up in all my applications. It’s like when I think about buying a new car and then I start seeing them everywhere on road and think – have there always been that many out there?
So, here it is in action. Below is a [...]]]></description>
			<content:encoded><![CDATA[<p>Lately, I can’t seem to shake the pipeline pattern. It keeps popping up in all my applications. It’s like when I think about buying a new car and then I start seeing them everywhere on road and think – have there always been that many out there?</p>
<p>So, here it is in action. Below is a controller from an ASP.NET MVC application. The method accepts an order in the form of xml for provisioning products in our system.</p>
<pre class="prettyprint">
<code>public ActionResult EnqueueOrders(string orderXml)
{
    var context = new OrderContext(orderXml);

    var pipeline = new FilterPipeline&lt;OrderContext&gt;()
        .Add(new RewriteLegacyUsernameFeature(productFinder))
        .Add(new ValidateOrderXml())
        .Add(new ValidateReferenceCode(customerFinder))
        .Add(new IgnoreOnException(new ValidateRemoteAccountUsernames(packageRepository)))
        .Add(new IgnoreOnException(new ValidateRemoteAccountUniqueEmail(packageRepository)))
        .Add(new EnqueueOrders(customerFacade)).When(new OrderHasNoErrors())
        .Execute(context);

    var xml = BuildResponseFromOrderContext(context);

    return new XmlResult(xml);
}</code>
</pre>
<p>Most of it is just validation logic, but there are a few meatier pieces. Take a look a the first filter – RewriteLegacyUsernameFeature. If you’ve ever published an API that accepts xml, then you’ve probably wanted to change your schema 5 minutes later. The pipeline is great way to deal with transforming legacy xml on the way in.</p>
<p>Next is the implementation of the FilterPipeline which is essentially a chain of responsibility with a few convenience features.</p>
<pre class="prettyprint">
<code>public class FilterPipeline&lt;T&gt; : IFilter&lt;T&gt;
{
    private IFilterLink&lt;T&gt; head;

    public void Execute(T input)
    {
        head.Execute(input);
    }

    public IFilterConfiguration&lt;T&gt; Add(IFilterLink&lt;T&gt; link)
    {
        var specificationLink = new SpecificationFilterLink&lt;T&gt;(link);

        AppendToChain(specificationLink);

        return specificationLink;
    }

    public IFilterConfiguration&lt;T&gt; Add(IFilter&lt;T&gt; filter)
    {
        return Add(new FilterLinkAdapter&lt;T&gt;(filter));
    }

    public void AppendToChain(IFilterLink&lt;T&gt; link)
    {
        if (head == null)
        {
            head = link;
            return;
        }

        var successor = head;

        while (successor.Successor != null)
        {
            successor = successor.Successor;
        }

        successor.Successor = link;
    }
}</code>
</pre>
<p>You might be scratching your head and wondering what this business is with both IFilter&lt;T&gt; and IFilterLink&lt;T&gt;. IFilter&lt;T&gt; is just a simpler version of IFilterLink&lt;T&gt; that doesn’t require the implementer to deal with calling the next link in the chain. The subject will always pass through, no short-circuiting, hence the pipeline.</p>
<p>My favorite part  is the SpecificationFilterLink&lt;T&gt; which is a decorator that uses a Specification to decide if the filter should be invoked. So you can do little readable snippets like:</p>
<pre class="prettyprint">
<code>pipeline.Add(new EnqueueOrders(customerFacade)).When(new OrderHasNoErrors());</code>
</pre>
<p>Maybe this post will get it out of my system and I can move on to other solutions. It’s just so handy…</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/an-order-processing-pipeline-in-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

