<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>The Inquisitive Coder - Davy Brion's Blog</title>
	
	<link>http://davybrion.com/blog</link>
	<description>Trying to walk that thin line between intelligence and ignorance</description>
	<lastBuildDate>Mon, 08 Mar 2010 15:36:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/davybrion" /><feedburner:info uri="davybrion" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Using Copy-On-Write In Multithreaded Code To Reduce Locking Overhead</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/wAIs9EfSuv8/</link>
		<comments>http://davybrion.com/blog/2010/03/using-copy-on-write-in-multithreaded-code-to-reduce-locking-overhead/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 15:36:35 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Multithreading]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/03/using-copy-on-write-in-multithreaded-code-to-reduce-locking-overhead/</guid>
		<description><![CDATA[I recently posted some code that i asked you to review.&#160; When i posted it, the code had never even executed (that’s right, not even through a test) and i only thought it would do what i needed it to do.&#160; I consider the actual implementation non-obvious (at least for those who don’t know the [...]]]></description>
			<content:encoded><![CDATA[<p>I recently <a href="http://davybrion.com/blog/2010/02/wanna-review-my-code/" target="_blank">posted some code</a> that i asked you to review.&#160; When i posted it, the code had never even executed (that’s right, not even through a test) and i only <em>thought</em> it would do what i needed it to do.&#160; I consider the actual implementation non-obvious (at least for those who don’t know the copy-on-write approach to avoid traditional locking) so i just wanted to hear some reactions to the code from people who didn’t knew the context.&#160; I promised to do a follow-up post to discuss the code in its entirety so here it is.</p>
<p>First, i’ll show the whole class again:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">TenantSessionFactoryManager</span> : <span style="color: #2b91af">ITenantSessionFactoryManager</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">ITenantContext</span> tenantContext;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">ITenantInfoHolder</span> tenantInfoHolder;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">string</span> mappingAssemblyName;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">object</span> writeLock = <span style="color: blue">new</span> <span style="color: blue">object</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Guid</span>, <span style="color: #2b91af">ISessionFactory</span>&gt; sessionFactories;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> TenantSessionFactoryManager(<span style="color: #2b91af">ITenantContext</span> tenantContext, <span style="color: #2b91af">ITenantInfoHolder</span> tenantInfoHolder, <span style="color: blue">string</span> mappingAssemblyName)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.tenantContext = tenantContext;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.tenantInfoHolder = tenantInfoHolder;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.mappingAssemblyName = mappingAssemblyName;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactories = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Guid</span>, <span style="color: #2b91af">ISessionFactory</span>&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">ISession</span> CreateSessionForCurrentTenant()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> tenantId = tenantContext.CurrentTenantId;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateSessionFactoryForCurrentTenant();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> sessionFactories[tenantId].OpenSession();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">void</span> CreateSessionFactoryForCurrentTenant()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">lock</span> (writeLock)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> tenantId = tenantContext.CurrentTenantId;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> connectionString = tenantInfoHolder.GetDatabaseConnectionString(tenantId);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> sessionFactory = <span style="color: blue">new</span> <span style="color: #2b91af">Configuration</span>()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Configure()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .AddProperties(<span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">string</span>&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style="color: #a31515">&quot;connection.connection_string&quot;</span>, connectionString },</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style="color: #a31515">&quot;cache.region_prefix&quot;</span>, <span style="color: #a31515">&quot;Tenant_&quot;</span> + tenantId }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; })</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .AddAssembly(mappingAssemblyName)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .BuildSessionFactory();</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> newDictionary = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Guid</span>, <span style="color: #2b91af">ISessionFactory</span>&gt;(sessionFactories);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; newDictionary[tenantId] = sessionFactory;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactories = newDictionary;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> RemoveSessionFactoryForTenant(<span style="color: #2b91af">Guid</span> tenantId)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">lock</span> (writeLock)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> sessionFactory = sessionFactories[tenantId];</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> newDictionary = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Guid</span>, <span style="color: #2b91af">ISessionFactory</span>&gt;(sessionFactories);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; newDictionary.Remove(tenantId);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactories = newDictionary;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactory.Dispose();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Basically, the purpose of this class is to hold a set of ISessionFactory instances, each of which belongs to a particular tenant in a multi-tenant application.&#160; Tenants can be added on the fly (without restarting the application) and when an ISessionFactory doesn’t exist yet for a particular tenant, it must be created when the first request for an ISession for that tenant comes in.&#160; Obviously, access to the sessionFactories dictionary must be thread-safe since multiple threads will be reading from the dictionary as well as occasionally writing to it.</p>
<p>I considered 3 options to make sure access to the dictionary would be thread-safe:</p>
<ol>
<li>Traditional locking (through the lock statement or the Monitor class) </li>
<li>Using the <a href="http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx" target="_blank">ReadWriterLockSlim</a> class </li>
<li>Using the copy-on-write pattern </li>
</ol>
<p>Traditional locking was quickly scratched from the list because that would require me to lock for every read of the dictionary as well as every write.&#160; Now, pretty much every single request requires an NHibernate session which means that pretty much every single request results in a lookup in the sessionFactories dictionary.&#160; If i need to lock for every read, this significantly hurts overall throughput of the system.&#160; </p>
<p>The ReadWriterLockSlim might be a good solution here… after all, the short description of this class in MSDN says this:</p>
<blockquote><p>Represents a lock that is used to manage access to a resource, allowing multiple threads for reading or exclusive access for writing.</p>
</blockquote>
<p>Sounds like what i need, right?&#160; But the thing is, i’ve never used the ReadWriterLockSlim class before and it hasn’t really gained my trust yet.&#160; I know that’s a terrible excuse for not using it, but here me out.&#160; While the ReadWriterLockSlim likely reduces locking overhead over traditional locking substantially, there still has to be <em>some</em> overhead for read operations, even if it is small.&#160; In most situations, that small overhead wouldn’t bother me but in this case, that little overhead would be added to pretty much <em>every single request</em> in the system.&#160; Now, writing to a dictionary implies that a new tenant has been added to the system.&#160; In the context of this system, that’s not even gonna happen on a daily basis.&#160; Hell, once a week is probably a best-case estimation and even that is highly optimistic.&#160; So i really don’t want any kind of overhead on read operations when the write operation is only going to happen very occasionally.</p>
<p>That leaves the copy-on-write pattern.&#160; I’ve used it <a href="http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/" target="_blank">before</a> with success (though at the time, i didn’t know it was a known pattern) so this approach has already gained my trust.&#160; It basically implies that we don’t do <em>any</em> locking on the read operations, but whenever a write operation occurs we copy the original set of objects, perform the write on the newly copied set and then set the reference of the original set to the newly created and modified instance.&#160; During this whole time, every <em>single</em> read is safe.&#160; Successive reads within the same logical operation however aren’t, so the following code would not be thread-safe:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> sessionFactories[tenantId].OpenSession();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Because there’s no locking on the reads, the code within the if-block could fail because the sessionFactories reference could be pointing to a new dictionary which no longer contains the element for that key.&#160; </p>
<p>Of course, if you have frequent writes, the overhead of copying the set of objects every time you need to add/remove one might be bigger than you want, so this isn’t a pattern that you should use whenever you need to protect access to a shared resource. For this situation however, i think it’s ideal… though i’d obviously like to hear about better solutions <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Now, let’s take a closer look at the pieces of code that perform the write operations.&#160; First, adding a new ISessionFactory to the dictionary:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">void</span> CreateSessionFactoryForCurrentTenant()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">lock</span> (writeLock)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> tenantId = tenantContext.CurrentTenantId;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> connectionString = tenantInfoHolder.GetDatabaseConnectionString(tenantId);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> sessionFactory = <span style="color: blue">new</span> <span style="color: #2b91af">Configuration</span>()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Configure()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .AddProperties(<span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">string</span>&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style="color: #a31515">&quot;connection.connection_string&quot;</span>, connectionString },</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style="color: #a31515">&quot;cache.region_prefix&quot;</span>, <span style="color: #a31515">&quot;Tenant_&quot;</span> + tenantId }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; })</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .AddAssembly(mappingAssemblyName)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .BuildSessionFactory();</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> newDictionary = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Guid</span>, <span style="color: #2b91af">ISessionFactory</span>&gt;(sessionFactories);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; newDictionary[tenantId] = sessionFactory;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactories = newDictionary;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>As you can see, the entire operation is put between a lock on the writeLock object instance.&#160; The downside of this is that creating an ISessionFactory instance is an expensive operation, which means the lock will be held for a long time (could easily be one or more <em>seconds</em>).&#160; Then again, i don’t anticipate this happening frequently so it’s not that big of an issue… especially since reads aren’t being blocked by this anyway.&#160; This approach also prevents the creation of 2 ISessionFactory instances for the same tenant.&#160; Well, unless i missed a bug here :p</p>
<p>Now, once the ISessionFactory instance is created, we create a new Dictionary based on the contents of the old one and then we add the new ISessionFactory instance to it.&#160; After that, we replace the sessionFactories references with the new dictionary and from that point on, every read will use the new dictionary instance.&#160; During this entire operation, no read operation was impacted negatively.&#160; </p>
<p>Now lets take a look at the other write operation, removing an ISessionFactory instance from the dictionary:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> RemoveSessionFactoryForTenant(<span style="color: #2b91af">Guid</span> tenantId)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">lock</span> (writeLock)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> sessionFactory = sessionFactories[tenantId];</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> newDictionary = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Guid</span>, <span style="color: #2b91af">ISessionFactory</span>&gt;(sessionFactories);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; newDictionary.Remove(tenantId);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactories = newDictionary;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactory.Dispose();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>The first if-check, which happens outside of the lock is a bug that i missed but that was pointed out in the comments of the original post.&#160; If CreateSessionFactoryForCurrentTenant and RemoveSessionFactoryForTenant would execute concurrently for the same tenant, it’s possible that the ISessionFactory instance of that tenant is never removed from the dictionary (and also never disposed of…) since the check happens outside of the lock and could be executed before the ISessionFactory of the tenant was added to the dictionary.&#160; In that case, the ISessionFactory instance would stay in the dictionary as long as the application stays up.&#160; This is definitely a race condition that you want to avoid in every other situation though in this case, the odds that we’re simultaneously adding and removing the same tenant are slim to none.&#160; Nevertheless, i don’t want to be accused of promoting race conditions so we’ll make the change anyway <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> RemoveSessionFactoryForTenant(<span style="color: #2b91af">Guid</span> tenantId)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">lock</span> (writeLock)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> sessionFactory = sessionFactories[tenantId];</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> newDictionary = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Guid</span>, <span style="color: #2b91af">ISessionFactory</span>&gt;(sessionFactories);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; newDictionary.Remove(tenantId);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactories = newDictionary;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactory.Dispose();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Now, as you can see we once again create a new dictionary based on the previous one, then remove the ISessionFactory instance for the current tenant and then we overwrite the sessionFactories instance once again. </p>
<p>Finally, there’s the read operation that i specifically didn’t want suffering from locking overhead:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">ISession</span> CreateSessionForCurrentTenant()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> tenantId = tenantContext.CurrentTenantId;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateSessionFactoryForCurrentTenant();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> sessionFactories[tenantId].OpenSession();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>The only time this code will block is when a new ISessionFactory for the current tenant needs to be created.&#160; Luckily, that only happens once for each tenant.&#160; As i mentioned earlier in the post, using this pattern doesn’t guarantee that successive reads within the same logical operation are thread safe, so there is a bug in here.&#160; If a tenant already has an ISessionFactory instance, it’s possible that the RemoveSessionFactoryForTenant method has been executed between the if-check and accessing the ISessionFactory based on the tenantId.&#160; In that particular scenario, the ISessionFactory instance is no longer in the dictionary which will cause this code to throw an exception.</p>
<p>That’s a bug that i don’t feel like fixing though… Once a tenant has been removed, they are no longer a paying customer.&#160; If they are no longer paying for the software, there is no reason whatsoever why i should care about any possible exceptions they could get while running the software <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Seriously though, if the RemoveSessionFactoryForTenant method is called, users of that tenant won’t even have access to the system anymore so it’s really a non-issue.</p>
<p>Anyways, i think i’ve covered the implementation in more detail than you probably cared for.&#160; So, any thoughts? Are there still issues that i haven’t thought of? Is there another approach that you would use for this specific scenario?</p>

<p><a href="http://feedads.g.doubleclick.net/~a/sEg4m9ne0-baDPyLJuGRQMaGvf4/0/da"><img src="http://feedads.g.doubleclick.net/~a/sEg4m9ne0-baDPyLJuGRQMaGvf4/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/sEg4m9ne0-baDPyLJuGRQMaGvf4/1/da"><img src="http://feedads.g.doubleclick.net/~a/sEg4m9ne0-baDPyLJuGRQMaGvf4/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=wAIs9EfSuv8:EoroRlBFYAc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=wAIs9EfSuv8:EoroRlBFYAc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=wAIs9EfSuv8:EoroRlBFYAc:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/wAIs9EfSuv8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/03/using-copy-on-write-in-multithreaded-code-to-reduce-locking-overhead/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/03/using-copy-on-write-in-multithreaded-code-to-reduce-locking-overhead/</feedburner:origLink></item>
		<item>
		<title>Got 15 Minutes To Help Out With A University Study? I Mean, To Play A Game?</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/CkgqyXnyEm4/</link>
		<comments>http://davybrion.com/blog/2010/03/got-15-minutes-to-help-out-with-a-university-study-i-mean-to-play-a-game/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 09:34:21 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Off Topic]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2275</guid>
		<description><![CDATA[Bram De Moor, one of my coworkers, has developed a small game which is a part of a university study.  I can&#8217;t tell you what the study is about, since that would ruin the purpose of the game (and if anyone leaves a comment mentioning it, it will be deleted) but it&#8217;s pretty interesting. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://bramdemoor.blogspot.com/">Bram De Moor</a>, one of my coworkers, has developed a small game which is a part of a university study.  I can&#8217;t tell you what the study is about, since that would ruin the purpose of the game (and if anyone leaves a comment mentioning it, it will be deleted) but it&#8217;s pretty interesting.  The game itself is pretty nicely done, though you obviously shouldn&#8217;t expect too much.  So if you can spare about 15 minutes and want to participate in this study, download the game <a href="http://www.moddb.com/games/flight-of-the-strihuhn">here</a>, play it and answer the questionnaire at the end.  Don&#8217;t do it for yourself, do it for science! <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/N2W3AXaKp0MhP_wZEpaaL_mH0ok/0/da"><img src="http://feedads.g.doubleclick.net/~a/N2W3AXaKp0MhP_wZEpaaL_mH0ok/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/N2W3AXaKp0MhP_wZEpaaL_mH0ok/1/da"><img src="http://feedads.g.doubleclick.net/~a/N2W3AXaKp0MhP_wZEpaaL_mH0ok/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=CkgqyXnyEm4:KQ4TjxmOrOg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=CkgqyXnyEm4:KQ4TjxmOrOg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=CkgqyXnyEm4:KQ4TjxmOrOg:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/CkgqyXnyEm4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/03/got-15-minutes-to-help-out-with-a-university-study-i-mean-to-play-a-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/03/got-15-minutes-to-help-out-with-a-university-study-i-mean-to-play-a-game/</feedburner:origLink></item>
		<item>
		<title>Accepting OSS Donations… A Slippery Slope</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/nLavjO2NWG8/</link>
		<comments>http://davybrion.com/blog/2010/03/accepting-oss-donations-a-slippery-slope/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 21:18:46 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Opinions]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/03/accepting-oss-donations-a-slippery-slope/</guid>
		<description><![CDATA[As i’m sure most of you have noticed, Ayende recently started an NHibernate Donation Campaign.&#160; I was against the idea from the beginning and even though this post will probably piss off a few people (like i ever cared about that, right?), i wanted to share a few of my thoughts on this with you.
The [...]]]></description>
			<content:encoded><![CDATA[<p>As i’m sure most of you have noticed, Ayende recently started an <a href="http://ayende.com/Blog/archive/2010/02/27/nhibernate-donation-campaign.aspx" target="_blank">NHibernate Donation Campaign</a>.&#160; I was <a href="http://groups.google.com/group/nhibernate-development/browse_thread/thread/7cf59efa31c46119" target="_blank">against</a> the idea from the beginning and even though this post will probably piss off a few people (like i ever cared about that, right?), i wanted to share a few of my thoughts on this with you.</p>
<p>The idea of sponsoring an open source project and helping out financially is definitely nice, but there are some very important practical issues that need to be kept in mind.&#160; Those practical issues are IMO being ignored by this campaign (which honestly feels like a publicity stunt more than anything else) … some of them were mentioned in the original mailinglist discussion but no resolution has been offered.&#160; There are probably more issues that we haven’t even thought of yet, but the most important ones are (again, IMO):</p>
<ul>
<li>Who’s going to decide how much of the money will go to the committers, and what will that be based on?&#160; Will it be based on past contributions? Future contributions? The complexity of the contributions? Total number of contributions? Total time spent on the project?&#160; We can’t even measure some of these things, so this is bound to either raise some discussions sooner or later, or might even upset some people.&#160; I can certainly imagine that some contributors from the past (like, the ones who did most of the really hard work in the beginning) could feel that they’re entitled to some (or most) of the money.&#160;&#160; Discussing how to spend the money on committers is just going to open a can of worms that could possibly cause much more problems between committers than it would solve.&#160; </li>
<li>There has to be some legal entity (or at least a person) who’s in charge of the bank account where the money will be deposited.&#160; At this point, we have no formal structure or organization to deal with something like that.&#160; I’m no expert on legal matters at all, but i can’t help but think that this isn’t something you can just decide on in a mailinglist… Some level of formalization is most likely required here.</li>
<li>Taxes… if the money goes to committers, then they will have to pay taxes on it based on the laws of the country they’re living in.&#160; Tax laws obviously vary from country to country, but i don’t think it’s unrealistic to claim that between 25% and 50% of the money will simply go to the government(s) instead of actually benefitting the project.&#160; You might wanna keep this in mind before you donate…</li>
</ul>
<p>These are just 3 big issues that could come of this, and i don’t really see a true benefit to the whole thing.&#160; The people who in the past have spent time on NHibernate either volunteered to do so, or were paid by someone (or some company) to spend a certain amount of time on it.&#160; Without donations, it will just go on like that and i don’t really think there’s anything wrong with that.&#160;&#160; While resources are certainly limited, this approach probably causes <em>less</em> problems than one that distributes donations amongst committers.</p>
<p>Now, just to be absolutely clear on this… i am one of the NHibernate committers, and i don’t feel that i’m entitled to any of the money that will be raised.&#160; For one, i haven’t committed anything in the past 6 months, and the things i did commit definitely aren’t worthy of a donation.&#160; If we are going to accept donations and spend it on people instead of infrastructure, i believe any money that would be raised should go to the people who originally started NHibernate and worked hard to get it to a state where it actually became usable in the .NET world.&#160; If it weren’t for their efforts, it never would’ve been in a position to become the de facto ORM in the .NET world, regardless of who’s working on it right now. </p>

<p><a href="http://feedads.g.doubleclick.net/~a/oyOUgjZVGq_olGPmATli6HTqtbs/0/da"><img src="http://feedads.g.doubleclick.net/~a/oyOUgjZVGq_olGPmATli6HTqtbs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/oyOUgjZVGq_olGPmATli6HTqtbs/1/da"><img src="http://feedads.g.doubleclick.net/~a/oyOUgjZVGq_olGPmATli6HTqtbs/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=nLavjO2NWG8:RFzx0j_Jl2I:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=nLavjO2NWG8:RFzx0j_Jl2I:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=nLavjO2NWG8:RFzx0j_Jl2I:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/nLavjO2NWG8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/03/accepting-oss-donations-a-slippery-slope/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/03/accepting-oss-donations-a-slippery-slope/</feedburner:origLink></item>
		<item>
		<title>Wanna Review My Code?</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/ZREBm22fWhY/</link>
		<comments>http://davybrion.com/blog/2010/02/wanna-review-my-code/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 14:58:31 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Off Topic]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/02/wanna-review-my-code/</guid>
		<description><![CDATA[I just wrote some code, and i’d like your opinion on it.&#160; The thing is, i’m not going to provide any context as to what it does or why certain decisions were made since i know you guys like to be challenged.&#160; You also might want to keep the following in mind when reading it:

it [...]]]></description>
			<content:encoded><![CDATA[<p>I just wrote some code, and i’d like your opinion on it.&#160; The thing is, i’m not going to provide any context as to what it does or why certain decisions were made since i know you guys like to be challenged.&#160; You also might want to keep the following in mind when reading it:</p>
<ol>
<li>it might contain bugs that i’m not aware of </li>
<li>it contains weird parts that were either brainfarts on my part, things i did on purpose to avoid possible issues, or both </li>
<li>it contains at least one bug that i know about, yet don’t care about </li>
<li>i removed the comments that i originally put in it to make things more interesting </li>
<li>i haven’t tested the code yet </li>
<li>i haven’t even executed it yet </li>
<li>i <em>think</em> it’s ok… but i’m not sure </li>
</ol>
<p>Questions will be answered if you have them (and i’m sure you will) though i can’t make any promises as to how soon i can answer them… I will post a follow-up post to discuss the code in its entirety later on, though i’ll probably wait a few days to do so.</p>
<p>This is the code:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">TenantSessionFactoryManager</span> : <span style="color: #2b91af">ITenantSessionFactoryManager</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">ITenantContext</span> tenantContext;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">ITenantInfoHolder</span> tenantInfoHolder;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">string</span> mappingAssemblyName;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">object</span> writeLock = <span style="color: blue">new</span> <span style="color: blue">object</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Guid</span>, <span style="color: #2b91af">ISessionFactory</span>&gt; sessionFactories;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> TenantSessionFactoryManager(<span style="color: #2b91af">ITenantContext</span> tenantContext, <span style="color: #2b91af">ITenantInfoHolder</span> tenantInfoHolder, <span style="color: blue">string</span> mappingAssemblyName)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.tenantContext = tenantContext;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.tenantInfoHolder = tenantInfoHolder;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.mappingAssemblyName = mappingAssemblyName;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactories = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Guid</span>, <span style="color: #2b91af">ISessionFactory</span>&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">ISession</span> CreateSessionForCurrentTenant()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> tenantId = tenantContext.CurrentTenantId;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateSessionFactoryForCurrentTenant();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> sessionFactories[tenantId].OpenSession();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">void</span> CreateSessionFactoryForCurrentTenant()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">lock</span> (writeLock)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> tenantId = tenantContext.CurrentTenantId;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> connectionString = tenantInfoHolder.GetDatabaseConnectionString(tenantId);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> sessionFactory = <span style="color: blue">new</span> <span style="color: #2b91af">Configuration</span>()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Configure()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .AddProperties(<span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">string</span>&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style="color: #a31515">&quot;connection.connection_string&quot;</span>, connectionString },</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style="color: #a31515">&quot;cache.region_prefix&quot;</span>, <span style="color: #a31515">&quot;Tenant_&quot;</span> + tenantId }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; })</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .AddAssembly(mappingAssemblyName)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .BuildSessionFactory();</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> newDictionary = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Guid</span>, <span style="color: #2b91af">ISessionFactory</span>&gt;(sessionFactories);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; newDictionary[tenantId] = sessionFactory;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactories = newDictionary;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> RemoveSessionFactoryForTenant(<span style="color: #2b91af">Guid</span> tenantId)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">lock</span> (writeLock)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!sessionFactories.ContainsKey(tenantId))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> sessionFactory = sessionFactories[tenantId];</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> newDictionary = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Guid</span>, <span style="color: #2b91af">ISessionFactory</span>&gt;(sessionFactories);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; newDictionary.Remove(tenantId);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactories = newDictionary;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionFactory.Dispose();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>

<p><a href="http://feedads.g.doubleclick.net/~a/_Ybu4XolzGAqxb5-OuUVYpVXCHs/0/da"><img src="http://feedads.g.doubleclick.net/~a/_Ybu4XolzGAqxb5-OuUVYpVXCHs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/_Ybu4XolzGAqxb5-OuUVYpVXCHs/1/da"><img src="http://feedads.g.doubleclick.net/~a/_Ybu4XolzGAqxb5-OuUVYpVXCHs/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=ZREBm22fWhY:gOSFApX4s78:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=ZREBm22fWhY:gOSFApX4s78:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=ZREBm22fWhY:gOSFApX4s78:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/ZREBm22fWhY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/02/wanna-review-my-code/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/02/wanna-review-my-code/</feedburner:origLink></item>
		<item>
		<title>You’ll Never Get Sustainable Progress For Free</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/NB4-9mnElKQ/</link>
		<comments>http://davybrion.com/blog/2010/02/youll-never-get-sustainable-progress-for-free/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 21:10:58 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Opinions]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/02/youll-never-get-sustainable-progress-for-free/</guid>
		<description><![CDATA[Ayende recently wrote about how most software development processes ignore the actual building of software.&#160; Which reminded me of an excellent post written by James Shore over a year ago, and that i commented on as well.&#160; What James (and Ayende) are saying is that if you don’t actually take care of the way you [...]]]></description>
			<content:encoded><![CDATA[<p>Ayende recently <a href="http://ayende.com/Blog/archive/2010/02/20/nice-process-but-what-about-the-engineering-bits.aspx" target="_blank">wrote about</a> how most software development processes ignore the actual building of software.&#160; Which reminded me of an <a href="http://jamesshore.com/Blog/The-Decline-and-Fall-of-Agile.html" target="_blank">excellent post</a> written by James Shore over a year ago, and that i <a href="http://davybrion.com/blog/2008/11/agile-development-going-downhill/" target="_blank">commented on</a> as well.&#160; What James (and Ayende) are saying is that if you don’t actually take care of the way you <em>build</em> the software, no process can guarantee that any kind of perceived progress in the beginning can be sustained over the long run.&#160; Sustainable progress takes effort, and you’ll never keep it up if you don’t put the proper practices in place to <em>enable</em> it.</p>
<p>The sad (and typical) part is that the methodology which makes everything sound easy and prescribes the least amount of effort required is the one that took off the most.&#160; Obviously, i’m talking about Scrum.&#160; Scrum makes no mention whatsoever about technical practices that are required to enable sustained progress and Scrum proponents will proudly proclaim that it doesn’t need to since it’s about <em>product development</em> instead of <em>software development</em>.&#160;&#160; You can easily become a certified Scrum master while you might not know anything about real-world software development.&#160; In fact, i’ve seen plenty of people boasting that certification on their resume and their linkedin profiles while they really shouldn’t be involved in any kind of professional software development to begin with. </p>
<p>And since Scrum is simple enough for middle-management (you know who i’m talking about… the ones that are usually not competent enough to promote to upper management) to understand, they jump on it as well and before you know it, they’ll be talking about the Scrum concepts as if they actually knew what they’re talking about.&#160; Teams and even entire departments in large corporations are told to be ‘agile’ and follow the Scrum rules and concepts for every new project.&#160; And they do.&#160; And it works great… for a while.&#160; We all know what happens after that.</p>
<p>Then take a look at Extreme Programming (XP).&#160; It’s been around for a while now.&#160; And it unfortunately doesn’t have a good, catchy name.&#160; In fact, its name is probably one of the biggest reasons why it never took off the way Scrum did.&#160; XP prescribes a lot more than Scrum, and even details the technical practices that you’ll need to enable sustainable progress.&#160; And while you don’t need every single one of them, they all provide major benefits and the more of them that you do use, the better off you’ll be.&#160;&#160; So what is the problem with XP?&#160; I never really understood why so many people jumped on Scrum, and now Lean (until the Toyota recalls that is) but never really got into XP.&#160;&#160; Well yeah i do understand.&#160; XP takes more work and effort to understand and apply.&#160; And that, my friends, is the price you’ll have to pay for sustainable progress(*).</p>
<p>*: i’m talking about the actual effort and work, not XP in itself</p>

<p><a href="http://feedads.g.doubleclick.net/~a/r-H5AXC8JgJ3NANaqKPi1sY3sIQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/r-H5AXC8JgJ3NANaqKPi1sY3sIQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/r-H5AXC8JgJ3NANaqKPi1sY3sIQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/r-H5AXC8JgJ3NANaqKPi1sY3sIQ/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=NB4-9mnElKQ:FFAa4_dJXrQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=NB4-9mnElKQ:FFAa4_dJXrQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=NB4-9mnElKQ:FFAa4_dJXrQ:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/NB4-9mnElKQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/02/youll-never-get-sustainable-progress-for-free/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/02/youll-never-get-sustainable-progress-for-free/</feedburner:origLink></item>
		<item>
		<title>Avoiding Memory Leaks With NServiceBus And Your Own Castle Windsor Instance</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/iIdqUVQRccM/</link>
		<comments>http://davybrion.com/blog/2010/02/avoiding-memory-leaks-with-nservicebus-and-your-own-castle-windsor-instance/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 09:42:55 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Castle Windsor]]></category>
		<category><![CDATA[Memory Management]]></category>
		<category><![CDATA[nservicebus]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2268</guid>
		<description><![CDATA[If you’re using NServiceBus together with your own instance of Castle Windsor, there is one thing you really need to look out for.&#160; NServiceBus was originally developed with Spring.NET as its IoC container, but it’s been changed to support multiple containers in a similar manner as Agatha does it.&#160; Agatha however, was originally developed with [...]]]></description>
			<content:encoded><![CDATA[<p>If you’re using NServiceBus together with your own instance of Castle Windsor, there is one thing you really need to look out for.&#160; NServiceBus was originally developed with Spring.NET as its IoC container, but it’s been changed to support multiple containers in a similar manner as <a href="http://davybrion.com/blog/2009/11/integrating-your-ioc-container-with-agatha/" target="_blank">Agatha does it</a>.&#160; Agatha however, was originally developed with Castle Windsor as its IoC container, and as such is well aware of Windsor’s <a href="http://davybrion.com/blog/2008/12/the-importance-of-releasing-your-components-through-windsor/" target="_blank">need to explicitly release resolved components</a>.&#160;&#160; NServiceBus unfortunately was <a href="http://tech.groups.yahoo.com/group/nservicebus/message/5376" target="_blank">not aware of this need</a>, and a workaround that they have introduced is to set Windsor’s ReleasePolicy to the NoTrackingReleasePolicy (which doesn’t hold any instances in memory, but doesn’t provide any cleanup either) if you configure NServiceBus to use <em>its own instance of </em>Castle Windsor.&#160; However, if you’re integrating NServiceBus into a project that is already using Castle Windsor, then you probably want NServiceBus to use <em>your instance</em> of Castle Windsor.</p>
<p>And that is when problems might appear.&#160; If you’re using Castle Windsor with the default ReleasePolicy (which is the LifecycledComponentsReleasePolicy) then each resolved transient instance will be stored in memory by that policy until the instance is explicitly released.&#160; The benefit of this policy is that the container can automatically dispose any disposable transient dependency of a resolved component.&#160; In my case, i’ve come to rely on that feature to achieve deterministic disposal behavior throughout my code base.&#160; </p>
<p>Now, when you configure NServiceBus and pass it your instance of Castle Windsor, it obviously doesn’t change the ReleasePolicy like it does when it creates its own instance of Castle Windsor.&#160;&#160; This is good because changing the policy of a passed in container would almost certainly have a huge behavioral implication for the application and such an action would be unacceptable when integrating a new framework into your project.&#160;&#160; But since NServiceBus doesn’t have the notion of needing to release resolved components, every single transient instance it resolves through <em>your</em> container will be stored in memory until the application’s process is terminated.&#160; Which means that you’ll leak instances of the following types <em>for each message</em> that your system needs to handle:</p>
<ul>
<li>NServiceBus.Grid.MessageHandlers.GridInterceptingMessageHandler </li>
<li>NServiceBus.Sagas.Impl.SagaMessageHandler </li>
<li>Your own MessageHandlers (and their transient dependencies as well) </li>
</ul>
<p>If your MessageHandlers don’t have dependencies (highly unlikely if you’re already using an IOC container) then you’d still have 3 leaking instances per incoming message.&#160; Add the number of transient dependencies of any handler to that and the number of leaking instances can increase dramatically.</p>
<p>First of all, if you do not depend on Windsor’s default ReleasePolicy’s behavior, then the easiest way to avoid this problem is definitely to set the container’s ReleasePolicy to the NoTrackingReleasePolicy like NServiceBus does itself when it’s configuring itself with a new instance of the container.</p>
<p>If you do depend on it, and you don’t want leaking instances that hang around forever, then the approach listed below is one way to solve this problem.&#160; There are probably other solutions available, and while the approach listed below can definitely be considered to be a HACK, i do think it’s the best solution to this particular problem.</p>
<p>Because we don’t want to change anything about the way that we’re already using the container, we just need to make sure that NServiceBus’ usage of the container doesn’t conflict with ours.&#160; That basically means that we need to make sure that the components that it resolves should not be tracked by the container.&#160;&#160; There are 2 kinds of components that NServiceBus will resolve during normal operation:</p>
<ol>
<li>Its own components that it needs to implement the features it offers you </li>
<li>Your message handlers that you need to handle incoming messages </li>
</ol>
<p>The first category is easy to exclude from Windsor’s tracking behavior.&#160; We can simply create our own ReleasePolicy which extends the default ReleasePolicy, and make sure that any instances of an NServiceBus-type are no longer tracked by the container:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyReleasePolicy</span> : Castle.MicroKernel.Releasers.<span style="color: #2b91af">LifecycledComponentsReleasePolicy</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">void</span> Track(<span style="color: blue">object</span> instance, Castle.MicroKernel.<span style="color: #2b91af">Burden</span> burden)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!instance.GetType().FullName.StartsWith(<span style="color: #a31515">&quot;NServiceBus&quot;</span>))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">base</span>.Track(instance, burden);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Then you need to set this release policy somewhere in your application’s startup routine:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IoC</span>.Container.Kernel.ReleasePolicy = <span style="color: blue">new</span> <span style="color: #2b91af">MyReleasePolicy</span>();</p>
</p></div>
<p>&#160;</p>
<p>(in this case, IoC.Container returns an IWindsorContainer instance)</p>
<p>This will make sure that no instances of NServiceBus-types will ever leak in the container.&#160; But now we still have to deal with our message handlers.&#160; The solution to making sure they are not disposed is not the cleanest out there but hey, it works.&#160; I basically introduced a base class that all my message handlers will need to inherit from, with the following implementation in the Handle method:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">abstract</span> <span style="color: blue">class</span> <span style="color: #2b91af">MessageHandler</span>&lt;TMessage&gt; : <span style="color: #2b91af">IMessageHandler</span>&lt;TMessage&gt; <span style="color: blue">where</span> TMessage : <span style="color: #2b91af">IMessage</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Handle(TMessage message)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">try</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Process(message);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">finally</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// ugly as hell, but we need this until NSB releases its resolved components</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IoC</span>.Container.Release(<span style="color: blue">this</span>);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">abstract</span> <span style="color: blue">void</span> Process(TMessage message);</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Uh oh… i just felt a great disturbance in the Force, as if tens of voices suddenly cried out in terror about my direct usage of the IoC container in a component!</p>
<p>Conceptually, this is wrong on many levels.&#160; Then again, this makes sure that the message handlers (and their dependencies) that are resolved by NServiceBus will always be guaranteed to be released by the comtainer.&#160; It might not be nice, but it avoids the memory leak and it doesn’t force me to change <em>my other code</em>.</p>
<p>And once NServiceBus is modified to release the components it resolves (if it’s ever modified that is…) i only need to get rid of my custom policy and the try/finally block.&#160; Unfortunately, my existing message handlers will then all implement the Process method instead of the Handle method but that is quickly fixed with a simple rename-refactoring.</p>
<p>Even though this is a pretty big hack (IMHO), at least its impact on the code is minimal… it will be easy to get rid of once the real problem is solved in NSB, and there are no real downsides to this that i can think of at the moment. </p>

<p><a href="http://feedads.g.doubleclick.net/~a/qnQqBZQcQw1q9gZH0hjnJg8tKoU/0/da"><img src="http://feedads.g.doubleclick.net/~a/qnQqBZQcQw1q9gZH0hjnJg8tKoU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/qnQqBZQcQw1q9gZH0hjnJg8tKoU/1/da"><img src="http://feedads.g.doubleclick.net/~a/qnQqBZQcQw1q9gZH0hjnJg8tKoU/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=iIdqUVQRccM:mtOewlsjPKM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=iIdqUVQRccM:mtOewlsjPKM:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=iIdqUVQRccM:mtOewlsjPKM:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/iIdqUVQRccM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/02/avoiding-memory-leaks-with-nservicebus-and-your-own-castle-windsor-instance/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/02/avoiding-memory-leaks-with-nservicebus-and-your-own-castle-windsor-instance/</feedburner:origLink></item>
		<item>
		<title>Have You Jumped On The Bandwagon Yet?</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/RkanAnZEbxY/</link>
		<comments>http://davybrion.com/blog/2010/02/have-you-jumped-on-the-bandwagon-yet/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 20:16:29 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2263</guid>
		<description><![CDATA[I&#8217;m not sure if this is related to the time of the year, but i (and undoubtedly quite a few of you as well if you&#8217;re honest) am once again getting pretty annoyed with the fact that the blogosphere is once again filled with &#8220;you gotta do this or you&#8217;re just not cool&#8221;-kinda posts.  [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not sure if this is related to the time of the year, but i (and undoubtedly quite a few of you as well if you&#8217;re honest) am once again getting pretty annoyed with the fact that the blogosphere is once again filled with &#8220;you gotta do this or you&#8217;re just not cool&#8221;-kinda posts.   The weird part is that the <a href="http://davybrion.com/blog/2009/02/buzzword-driven-development-isnt-gonna-help-you/">last time i complained about this was pretty much exactly a year ago</a>.</p>
<p>These days, you&#8217;re an absolute moron if you&#8217;re still using a relational database.  And forget about getting invited to the best parties if you&#8217;re not developing according to the Command and Query Responsibility Segregation (CQRS) pattern.  And if you&#8217;re not doing Lean then you&#8217;re really not worth listening to.  Oh no wait, talking about Lean isn&#8217;t hot anymore since the Toyota recalls so we can scratch that off the list, sorry about that one folks.</p>
<p>Now, i&#8217;m not saying that the whole NoSQL and CQRS thing is worthless.  I actually find them both very interesting.  But to all of you who are now pushing these things as if they are the greatest things since sliced bread, i would just like to ask one thing:  can we get some more real-world examples?  You do use these things in real-world projects before proclaiming that these approaches will fix all (or most) of our current problems, right?  You&#8217;re not just trying to be cool on the internet, are you?  Honestly, of all the people that i hear talking about these things lately i only really believe about a handful of them when they say they use these things in real world projects.  I&#8217;ll leave it to you to figure out who the real ones are, and who is just jumping on the bandwagon.</p>
<p>And no, i&#8217;m definitely not going to claim that using an RDBMS for every system is the best choice.  I complain about databases every day, but i&#8217;m not just going to claim that going with a NoSQL database is going to solve all our problems either.  I&#8217;d actually feel rather silly doing so, unless of course i&#8217;d be able to actually back up what i&#8217;m saying.  Which i haven&#8217;t really seen happen yet. </p>
<p>The thing is&#8230; there simply is no one-size-fits-all approach to software development and it&#8217;s pretty sad to see that some idiots actually appear to believe that there is.  Sometimes an RDBMS makes sense, and sometimes it doesn&#8217;t.  Sometimes MVC makes sense, and sometimes it doesn&#8217;t.  Sometimes DDD makes sense, and sometimes it doesn&#8217;t.  Sometimes CQRS makes sense, and sometimes it doesn&#8217;t.   That goes for every tool, library, architectural style, or whatever other buzzword you can think of.   Use what makes sense for what you need, and let&#8217;s not pretend to be someone or something that you&#8217;re not, ok?</p>

<p><a href="http://feedads.g.doubleclick.net/~a/EnQXFvyyGARaCERzAWZdxB88KBA/0/da"><img src="http://feedads.g.doubleclick.net/~a/EnQXFvyyGARaCERzAWZdxB88KBA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/EnQXFvyyGARaCERzAWZdxB88KBA/1/da"><img src="http://feedads.g.doubleclick.net/~a/EnQXFvyyGARaCERzAWZdxB88KBA/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=RkanAnZEbxY:_OHQhApUjn4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=RkanAnZEbxY:_OHQhApUjn4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=RkanAnZEbxY:_OHQhApUjn4:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/RkanAnZEbxY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/02/have-you-jumped-on-the-bandwagon-yet/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/02/have-you-jumped-on-the-bandwagon-yet/</feedburner:origLink></item>
		<item>
		<title>Can You Come Up With A WHY Comment For This Code?</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/ddYRuOiLrQ8/</link>
		<comments>http://davybrion.com/blog/2010/02/can-you-come-up-with-a-why-comment-for-this-code/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 16:26:18 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Memory Management]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/02/can-you-come-up-with-a-why-comment-for-this-code/</guid>
		<description><![CDATA[I just had to write the following code:

&#160;&#160;&#160; public class MyReleasePolicy : Castle.MicroKernel.Releasers.LifecycledComponentsReleasePolicy
&#160;&#160;&#160; {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; public override void Track(object instance, Castle.MicroKernel.Burden burden)
&#160;&#160;&#160;&#160;&#160;&#160;&#160; {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (!instance.GetType().FullName.StartsWith(&#34;NServiceBus&#34;))
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; base.Track(instance, burden);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }
&#160;&#160;&#160;&#160;&#160;&#160;&#160; }
&#160;&#160;&#160; }

&#160;
and then this:

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; IoC.Container.Kernel.ReleasePolicy = new MyReleasePolicy();

&#160;
to make a problem go away.
Can you come up with a WHY comment? (the category of this post is another [...]]]></description>
			<content:encoded><![CDATA[<p>I just had to write the following code:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyReleasePolicy</span> : Castle.MicroKernel.Releasers.<span style="color: #2b91af">LifecycledComponentsReleasePolicy</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">void</span> Track(<span style="color: blue">object</span> instance, Castle.MicroKernel.<span style="color: #2b91af">Burden</span> burden)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!instance.GetType().FullName.StartsWith(<span style="color: #a31515">&quot;NServiceBus&quot;</span>))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">base</span>.Track(instance, burden);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>and then this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IoC</span>.Container.Kernel.ReleasePolicy = <span style="color: blue">new</span> <span style="color: #2b91af">MyReleasePolicy</span>();</p>
</p></div>
<p>&#160;</p>
<p>to make a problem go away.</p>
<p>Can you come up with a WHY comment? (the category of this post is another hint)</p>

<p><a href="http://feedads.g.doubleclick.net/~a/-GdY2rgS74Rde3g9HtQfT6CPkEY/0/da"><img src="http://feedads.g.doubleclick.net/~a/-GdY2rgS74Rde3g9HtQfT6CPkEY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/-GdY2rgS74Rde3g9HtQfT6CPkEY/1/da"><img src="http://feedads.g.doubleclick.net/~a/-GdY2rgS74Rde3g9HtQfT6CPkEY/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=ddYRuOiLrQ8:2n2Yt7LNLtg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=ddYRuOiLrQ8:2n2Yt7LNLtg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=ddYRuOiLrQ8:2n2Yt7LNLtg:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/ddYRuOiLrQ8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/02/can-you-come-up-with-a-why-comment-for-this-code/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/02/can-you-come-up-with-a-why-comment-for-this-code/</feedburner:origLink></item>
		<item>
		<title>When It Comes To IOC Containers, We Seem To Be Pretty Loyal</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/f-cEfDlkQDw/</link>
		<comments>http://davybrion.com/blog/2010/02/when-it-comes-to-ioc-containers-we-seem-to-be-pretty-loyal/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 21:33:43 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Inversion Of Control]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2257</guid>
		<description><![CDATA[I&#8217;m still working my way through my backlog of unread posts in Google Reader (not even halfway through it :s) and one thing i noticed by reading some posts is that we (.NET developers) generally seem to be pretty loyal when it comes to which IOC container we use.  Some of us might switch [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m still working my way through my backlog of unread posts in Google Reader (not even halfway through it :s) and one thing i noticed by reading some posts is that we (.NET developers) generally seem to be pretty loyal when it comes to which IOC container we use.  Some of us might switch between different containers from time to time, but i get the feeling that most of us typically stick with the container that we&#8217;ve come to know the best.</p>
<p>Some people will stick with Castle Windsor, some swear by StructureMap, some will always use Autofac, some only want NInject&#8230; hell, i even know a few people who still prefer Spring.NET.  The only one that doesn&#8217;t seem to have many loyal followers is Unity <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .  Every container has its strengths and weaknesses but most of them range from very good to downright excellent.  When new features are introduced to one container, they often quickly find their way into the other containers as well.  A cool new feature in container X is rarely a good incentive to switch containers because odds are high that your favorite container will get the new feature pretty soon as well.</p>
<p>What i think is a big reason of this (observed) loyalty is that the more you rely on your IOC container&#8217;s power, the less likely you are to switch to another container out of fear that some things might behave differently or might not work the way you think or expect they will.  If the container you&#8217;ve chosen to use has proved itself to you in production usage, you are very unlikely to replace it with another.  After all, it&#8217;s a very important piece of how your project works and you probably depend on it a lot.  You&#8217;ve probably become very familiar with its behavior in different circumstances and you&#8217;ve either come to rely on that, or have learned to live with it in some way.  But you&#8217;ve learned how it works and you&#8217;re pretty sure that there are no unpleasant surprises that will pop up a couple of days or weeks after a new deploy.  Because of that, i really wouldn&#8217;t feel all that comfortable with switching to a new container.  And lets face it, they all have their little special cases that you really need to know about to avoid problems.</p>
<p>And if you are using features that are unique to your container, then you&#8217;re obviously even more unlikely to make a switch.  In my case, i used Castle Windsor when i first started to use an IOC container.  I ran into some issues with memory usage because of the way Windsor works, but once i learned that i always have to explicitly release any resolved components, the problems went away.  In fact, i&#8217;ve come to rely on the fact that by releasing a resolved component, its disposable <em>transient</em> dependencies will automatically be disposed by Windsor as well.  I&#8217;m not sure about this, but i think Windsor is the only container that does this, or at least one of the few that do.  I actually consider it a must-have feature for a container, but most people don&#8217;t seem to consider this as important.</p>
<p>I can&#8217;t really imagine a situation that would make me consider switching to another container at this point&#8230; well, unless its development would someday halt obviously.  And even then i&#8217;d probably be holding on to it for a long while.  How about you?  How likely are you to switch to a different container?  And what would make you switch?  What holds you back from switching to a different one?  Which one do you use and why do you like it so much?</p>

<p><a href="http://feedads.g.doubleclick.net/~a/zDfktFDLbuOe-H1R4RGojWYG3Hs/0/da"><img src="http://feedads.g.doubleclick.net/~a/zDfktFDLbuOe-H1R4RGojWYG3Hs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/zDfktFDLbuOe-H1R4RGojWYG3Hs/1/da"><img src="http://feedads.g.doubleclick.net/~a/zDfktFDLbuOe-H1R4RGojWYG3Hs/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=f-cEfDlkQDw:EZXb2N_xZj4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=f-cEfDlkQDw:EZXb2N_xZj4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=f-cEfDlkQDw:EZXb2N_xZj4:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/f-cEfDlkQDw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/02/when-it-comes-to-ioc-containers-we-seem-to-be-pretty-loyal/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/02/when-it-comes-to-ioc-containers-we-seem-to-be-pretty-loyal/</feedburner:origLink></item>
		<item>
		<title>Back In Business</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/hUbtsh1yp-A/</link>
		<comments>http://davybrion.com/blog/2010/02/back-in-business/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 23:17:01 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[About The Blog]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2248</guid>
		<description><![CDATA[
Ok, i&#8217;m back from my trip to New York.  The picture above shows me and the 2 friends that joined me on the trip on top of the Rockefeller building.  For those of you who have no idea what i look like (and i&#8217;m assuming that&#8217;s most of you since i never put [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://davybrion.com/blog/wp-content/uploads/2010/02/P1010112.jpg"><img src="http://davybrion.com/blog/wp-content/uploads/2010/02/P1010112.jpg" alt="" title="P1010112" width="800" height="600" class="aligncenter size-full wp-image-2249" /></a></p>
<p>Ok, i&#8217;m back from my trip to New York.  The picture above shows me and the 2 friends that joined me on the trip on top of the Rockefeller building.  For those of you who have no idea what i look like (and i&#8217;m assuming that&#8217;s most of you since i never put a picture of myself on this blog before), i&#8217;m the dude on the left (_your left_).  The entire trip was simply fantastic&#8230; we did so much stuff that we often didn&#8217;t remember what we did over 2 days ago without pulling out the digital cameras, but some of the highlights include:</p>
<ul>
<li>The statue of liberty&#8230; it&#8217;s a bit cliche, but seriously, the level of detail on that statue is truly astonishing&#8230; the French really did a fantastic job on it (yea i said it <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> )</li>
<li>Going to a taping of David Letterman&#8217;s Late Show&#8230; i was sorta hoping in advance that we could get in, but we really didn&#8217;t plan on it.  We went into the Ed Sullivan&#8217;s theater asking for tickets, and luckily we were &#8217;selected&#8217; (the fact that it was freezing and that this was the tourist low season obviously helped) to attend a taping a few days later. </li>
<li>Brooklyn Dekker&#8230; anybody who knows me in real life knows i have a soft spot for good looking women, but when Brooklyn Dekker (the Sports Illustrated Swimsuit Edition <i> cover model</i>) got on stage during the taping of the Late Show the entire audience kinda got silent for about 2 seconds.. she really looked <em>that</em> stunning.</li>
<li>I&#8217;v been an NBA fan for the last 16 years or so&#8230; i finally got my first chance to see an NBA game in real life.  Ok, it was the Kincks vs the Kings (2 of the worst teams right now) but it was still a great experience&#8230; and i was actually at Madison Square Garden! (if you don&#8217;t know the significance of that, don&#8217;t worry&#8230; only basketball fans will understand)</li>
<li>My pancakes with strawberries and ice cream breakfast&#8230; yeah seriously, it was <em>that</em> good</li>
<li>Me asking an NYPD officer holding an M16 politely if i could take a picture of him and answering &#8220;oh&#8230; ok then&#8221; when he said he didn&#8217;t want me to take a picture of him</li>
<li>Walking through Brooklyn during Snowmaggedon looking for Grimaldi&#8217;s Pizzeria only to (finally) found it being closed.</li>
</ul>
<p>Those were the highlights but pretty much every single moment of the trip was memorable and i truly hope that i never forget a single one of them.  I took 1125 (according to iPhoto) pictures and they were definitely needed to remember everything that happened in such a short timespan.  We went during the low-season and even then, New York is definitely worthy of visiting.  The only thing you&#8217;ll regret is that you either didnt&#8217; stay long enough, or that you didn&#8217;t buy enough clothes (thank you oh horrible Dollar). </p>
<p>Once i got back to my country (Belgium for those that don&#8217;t know), i was astonished to see how badly it was handling the less than one inch of snow after what i had just seen in Manhattan.  The difference really is huge.  I usually avoid politics on this blog as much as possible, but the way our government is handling things compared to how they&#8217;re dealt with in New York in much worse weather conditions is truly saddening.  Less than an inch of snow and the whole country seems to be at a stand still.   And people wonder why i dont go out to vote (that&#8217;s right&#8230; we are required to show up at elections but i refuse to show up because there really isn&#8217;t even a single person who&#8217;s worthy of a vote right now).</p>
<p>Anyways, i&#8217;m back and i&#8217;m working through the backlog of unread posts in Google Reader&#8230; there have been a couple of posts so far that might inspire a few posts but other than that, i&#8217;m lacking any kind of inspiration to write a real post at the moment so if you have any requests, now is the time to make them <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>To wrap up this post: it was great to be gone for a while, but it&#8217;s great to be back as well <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/9efxrcITTxSohVG2OQ5H39gG3mc/0/da"><img src="http://feedads.g.doubleclick.net/~a/9efxrcITTxSohVG2OQ5H39gG3mc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/9efxrcITTxSohVG2OQ5H39gG3mc/1/da"><img src="http://feedads.g.doubleclick.net/~a/9efxrcITTxSohVG2OQ5H39gG3mc/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=hUbtsh1yp-A:NMrLGxV0jQE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=hUbtsh1yp-A:NMrLGxV0jQE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=hUbtsh1yp-A:NMrLGxV0jQE:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/hUbtsh1yp-A" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/02/back-in-business/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/02/back-in-business/</feedburner:origLink></item>
		<item>
		<title>Taking A Week Off</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/nR094C9VPeM/</link>
		<comments>http://davybrion.com/blog/2010/02/taking-a-week-off/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 20:27:11 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[About The Blog]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2244</guid>
		<description><![CDATA[Well, i&#8217;m off for a week&#8230; i&#8217;ll be doing the tourist thing (you know, hanging out, having fun, laughing our asses off, taking pictures and generally annoying the hell out of the locals) in New York City and i won&#8217;t be back until February 14th.  I will be avoiding all kinds of electronic communication [...]]]></description>
			<content:encoded><![CDATA[<p>Well, i&#8217;m off for a week&#8230; i&#8217;ll be doing the tourist thing (you know, hanging out, having fun, laughing our asses off, taking pictures and generally annoying the hell out of the locals) in New York City and i won&#8217;t be back until February 14th.  I will be avoiding all kinds of electronic communication and the internet in general, so if you&#8217;re wondering why i&#8217;m not responding to email, now you know why <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://davybrion.com/blog/wp-content/uploads/2010/02/NewYork.jpg"><img src="http://davybrion.com/blog/wp-content/uploads/2010/02/NewYork.jpg" alt="" title="NewYork" width="600" height="424" class="aligncenter size-full wp-image-2245" /></a></p>
<p>I&#8217;m not leaving until Saturday morning, so if you have some &#8216;must-see&#8217; or &#8216;must-do&#8217; tips about New York, bring em on! <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/T_QSqf6mxRdIf7_bAM7huZUmq7w/0/da"><img src="http://feedads.g.doubleclick.net/~a/T_QSqf6mxRdIf7_bAM7huZUmq7w/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/T_QSqf6mxRdIf7_bAM7huZUmq7w/1/da"><img src="http://feedads.g.doubleclick.net/~a/T_QSqf6mxRdIf7_bAM7huZUmq7w/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=nR094C9VPeM:VpQjJLUZDMQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=nR094C9VPeM:VpQjJLUZDMQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=nR094C9VPeM:VpQjJLUZDMQ:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/nR094C9VPeM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/02/taking-a-week-off/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/02/taking-a-week-off/</feedburner:origLink></item>
		<item>
		<title>Browser Usage</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/K1T06dDkm0A/</link>
		<comments>http://davybrion.com/blog/2010/02/browser-usage/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 21:06:23 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[About The Blog]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/02/browser-usage/</guid>
		<description><![CDATA[I read an article today about how Chrome has increased a lot in market share in the past year, mostly at the expense of Firefox.&#160; I wanted to see how Chrome usage on this blog has progressed in the past year so i got some stats from Google Analytics and created this little graph:
 
(note: [...]]]></description>
			<content:encoded><![CDATA[<p>I read an article today about how Chrome has increased a lot in market share in the past year, mostly at the expense of Firefox.&#160; I wanted to see how Chrome usage on this blog has progressed in the past year so i got some stats from Google Analytics and created this little graph:</p>
<p><a href="http://davybrion.com/pictures/BrowserUsage_12EC1/image.png" target="_blank"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://davybrion.com/pictures/BrowserUsage_12EC1/image_thumb.png" width="704" height="285" /></a> </p>
<p>(note: these numbers are obviously only from visits to this blog and are thus not representative of the entire internet (<em>yet</em>) etc…)</p>
<p>I started at the month before the first Chrome users were reported to give you an idea of Firefox’s score at that time.&#160; Naturally, the people that come to this blog are into technology so it’s no wonder that Firefox is the dominant browser instead of IE.&#160; Before Chrome entered the picture, nearly 70% of all visits to this blog in August 2008 were from Firefox users.&#160; In its first month, Chrome immediately got about 8%, which i thought was pretty impressive.&#160; Firefox’s numbers dropped below 50% for the first time less than a year later.&#160; That’s a pretty big impact in a relatively short time.&#160;&#160; And as you can see, Chrome usage has been growing a lot in the past 6 months ago, at the expense of both Firefox and IE.&#160; I wouldn’t be surprised if Chrome catches up to Firefox in another 6 months.</p>
<p>I’m not fanatic about any browser (as long as i don’t have to use IE, right?)… i use Firefox for OS X at home, but i use Chrome at work.&#160; I’m not really into the whole plugin/extension thing (except for Adblock obviously) so i’m not really ‘tied’ to a specific browser.&#160; Now, some people <em>are</em> pretty fanatic about their browser so what i’m wondering is: what would it take for you to switch to another browser (no matter which one you use now)?&#160; Are you hooked on extensions that you can’t go without in another browser?&#160; What was the reason you picked your current browser and why are you sticking with it? Or is it all pretty much the same for you?&#160; Do you frequently use multiple browsers?</p>
<p>In some weird way, i find that kinda stuff interesting so please do share <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/7Z5HFVWoN5YbxVPwJy0MWcc7DZU/0/da"><img src="http://feedads.g.doubleclick.net/~a/7Z5HFVWoN5YbxVPwJy0MWcc7DZU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/7Z5HFVWoN5YbxVPwJy0MWcc7DZU/1/da"><img src="http://feedads.g.doubleclick.net/~a/7Z5HFVWoN5YbxVPwJy0MWcc7DZU/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=K1T06dDkm0A:2d6CpPo3vAg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=K1T06dDkm0A:2d6CpPo3vAg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=K1T06dDkm0A:2d6CpPo3vAg:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/K1T06dDkm0A" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/02/browser-usage/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/02/browser-usage/</feedburner:origLink></item>
		<item>
		<title>Good Team Dynamics Are Essential For High Technical Quality</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/q_pqNX5qDTE/</link>
		<comments>http://davybrion.com/blog/2010/01/good-team-dynamics-are-essential-for-high-technical-quality/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 20:42:16 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Opinions]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/good-team-dynamics-are-essential-for-high-technical-quality/</guid>
		<description><![CDATA[Just read an interesting post from David Tchepak about the lessons he learned from his long-term agile project.&#160; The post itself is interesting but there was one point in particular on the momentum of his team that caught my attention:
This probably sounds a bit touchy-feely, but I think dismissing it on those grounds is ignoring [...]]]></description>
			<content:encoded><![CDATA[<p>Just read an interesting post from David Tchepak about the <a href="http://www.davesquared.net/2010/01/lessons-learned-from-my-current-project.html" target="_blank">lessons he learned from his long-term agile project</a>.&#160; The post itself is interesting but there was one point in particular on the momentum of his team that caught my attention:</p>
<blockquote><p>This probably sounds a bit touchy-feely, but I think dismissing it on those grounds is ignoring a fairly fundamental part of human nature. If every day, every task is a thankless struggle, the intertia will pull down your team&#8217;s morale, motivation, concentration, and reduce their creativity and ability to innovate. On the flip side, once the team gets up a bit of steam and starts churning through stories, seeing visible progress as they move across the task board and into the Done column, the team&#8217;s enthusiasm and creativity soars. They start kicking around new ideas on how to remove some duplication from the code and reduce some overhead for future stories.</p>
</blockquote>
<p>David is right, but i’d go even further than that.&#160; I’d assert that good team dynamics are essential for achieving high technical quality that is sustainable in the long term.&#160; I think we can all agree that people work better together if they actually like working with each other.&#160; And i’m pretty sure that many of us have experienced first hand how a negative relationship within a team can have a pretty negative impact on the technical quality of a project.&#160; </p>
<p>Have you ever been on a project where you were looking at a piece of code thinking “wow, this really ought to be cleaned up… but i’m not touching it since John is gonna get all pissed over somebody touching his code”.&#160; Or worse, “wow, he really made a mess of this but i’m not touching it… let him live with the pain”.&#160;&#160; I certainly wouldn’t be surprised if many of us have at one point (or more) in our career had this exact thought about a certain piece of code written by a teammate at the time.&#160; That is a perfect example of how negative team dynamics can have a pretty serious impact on the technical quality and thus, i’d argue, the long-term viability of the project.&#160;&#160; See, the thing is that it won’t end what that certain piece of code.&#160; Sure, code rot has been introduced.&#160; But even worse, team rot is being introduced by this behavior as well, even if you are just trying to preserve the peace by trying to avoid a possible confrontation.</p>
<p>This kind of team rot will not end there.&#160; If you don’t actively try to get rid of it ASAP, it’ll eventually spread throughout the team, as more and more people gradually get into the same mentality.&#160; Eventually, even the strongest, most positive personalities can fall victim to it.&#160; When new people join the team, they’ll quickly pick up on it as well and there are very few people who are willing to stand up to such a situation when joining an existing team.&#160; As i’m sure you can imagine, nobody really likes working in such a team.&#160; A lot of people in that situation will get into a “i’m just gonna look out for myself and to hell with anyone else” mentality.&#160;&#160; They will try to do their job in a way that they will not be blamed for anything.&#160; Communication will drop to a ‘nothing-more-than-necessary’-level.&#160; A lot of issues will be ignored until they are actually found by end-users.&#160; A lot of time and effort is going to be wasted in general.&#160; And you’ll probably end up with a pretty high turnover rate of team members.&#160; Believe it or not, these kind of teams truly do exist.</p>
<p>On the other hand, if everybody on the team gets along everything seems to go much more smoothly.&#160; People won’t be afraid to make changes in other people’s code if everyone understands that it’s for the best of the project and if they know that it’s not gonna lead to a bitchfest from a difficult team member.&#160; Discussions will be about actual facts and merits, not about not-wanting-to-give-in-to-someone-you-no-longer-like.&#160; People will alert their team members when they notice something that could cause problems later on.&#160; People will be much more likely to maintain their technical discipline when there is positive peer pressure to maintain high standards.&#160;&#160; People will help each other when they can.&#160; Essentially, those people will form a true team instead of being a collection of coworkers who are unfortunately working on the same project together.</p>
<p>As much as many of us strive to improve our technical abilities and skills on a continuous basis, we also need to ask ourselves the following questions regularly: </p>
<ul>
<li>Am i a good team member? </li>
<li>Would i like to work with someone like me? </li>
<li>What can i do to make people enjoy working with me more than they might do now? </li>
</ul>
<p>That doesn’t mean you have to become Mr Popular.&#160; It doesn’t mean you have to crack the best jokes.&#160; You can be almost entirely yourself, as long as you realize that:</p>
<ul>
<li>communication is extremely important </li>
<li>treating others with respect is crucial </li>
<li><a href="http://davybrion.com/blog/2009/12/dont-be-a-whiny-developer/" target="_blank">nobody likes working with whiney developers</a> </li>
<li>you can’t expect others to work on their flaws if you’re not willing to work on yours… it’s a two-way street </li>
</ul>

<p><a href="http://feedads.g.doubleclick.net/~a/2RTcgXNXU_1_ArEUrC78NchpyBo/0/da"><img src="http://feedads.g.doubleclick.net/~a/2RTcgXNXU_1_ArEUrC78NchpyBo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/2RTcgXNXU_1_ArEUrC78NchpyBo/1/da"><img src="http://feedads.g.doubleclick.net/~a/2RTcgXNXU_1_ArEUrC78NchpyBo/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=q_pqNX5qDTE:4vAbVMp8asA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=q_pqNX5qDTE:4vAbVMp8asA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=q_pqNX5qDTE:4vAbVMp8asA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/q_pqNX5qDTE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/01/good-team-dynamics-are-essential-for-high-technical-quality/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/01/good-team-dynamics-are-essential-for-high-technical-quality/</feedburner:origLink></item>
		<item>
		<title>NDepend 3 Preview</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/5SYjU6IszFY/</link>
		<comments>http://davybrion.com/blog/2010/01/ndepend-3-preview/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 09:45:30 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[IDE]]></category>
		<category><![CDATA[NDepend]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/ndepend-3-preview/</guid>
		<description><![CDATA[I was just playing around with an NDepend 3 beta version to experiment with the new Visual Studio integration that the NDepend team has developed.&#160; 
Once you install the Visual Studio addin through the VisualNDepend.exe tool, you’ll notice the following menu has been added in Visual Studio:
 
If you select the first item, you can [...]]]></description>
			<content:encoded><![CDATA[<p>I was just playing around with an NDepend 3 beta version to experiment with the new Visual Studio integration that the NDepend team has developed.&#160; </p>
<p>Once you install the Visual Studio addin through the VisualNDepend.exe tool, you’ll notice the following menu has been added in Visual Studio:</p>
<p><a href="http://davybrion.com/pictures/NDepend3Preview_1277D/01.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="01" border="0" alt="01" src="http://davybrion.com/pictures/NDepend3Preview_1277D/01_thumb.png" width="377" height="242" /></a> </p>
<p>If you select the first item, you can easily create a new NDepend project for your current solution:</p>
<p><a href="http://davybrion.com/pictures/NDepend3Preview_1277D/02.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="02" border="0" alt="02" src="http://davybrion.com/pictures/NDepend3Preview_1277D/02_thumb.png" width="644" height="400" /></a></p>
<p>You’ll also notice an NDepend icon in the bottom-right corner of Visual Studio.&#160; Clicking on it shows you an immediate overview of NDepend’s CQL query results for your solution:</p>
<p>&#160;<a href="http://davybrion.com/pictures/NDepend3Preview_1277D/03.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="03" border="0" alt="03" src="http://davybrion.com/pictures/NDepend3Preview_1277D/03_thumb.png" width="224" height="403" /></a> </p>
<p>Clicking on the Show SQL Explorer immediately brings you to this view, which many NDepend users will recognize:</p>
<p><a href="http://davybrion.com/pictures/NDepend3Preview_1277D/04.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="04" border="0" alt="04" src="http://davybrion.com/pictures/NDepend3Preview_1277D/04_thumb.png" width="644" height="333" /></a> </p>
<p>You can also right-click on a type (or a method) and you’ll see the following NDepend context-menu:</p>
<p><a href="http://davybrion.com/pictures/NDepend3Preview_1277D/05.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="05" border="0" alt="05" src="http://davybrion.com/pictures/NDepend3Preview_1277D/05_thumb.png" width="624" height="484" /></a> </p>
<p>Which in turn allows you to open many of the familiar NDepend views:</p>
<p><a href="http://davybrion.com/pictures/NDepend3Preview_1277D/06.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="06" border="0" alt="06" src="http://davybrion.com/pictures/NDepend3Preview_1277D/06_thumb.png" width="644" height="364" /></a></p>
<p>This particular view is something that i find extremely useful.&#160; Notice how you get an immediate overview of relevant metrics when you click on one of the items in the graph.&#160; Or you can look at the metrics view:</p>
<p>&#160;<a href="http://davybrion.com/pictures/NDepend3Preview_1277D/07.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="07" border="0" alt="07" src="http://davybrion.com/pictures/NDepend3Preview_1277D/07_thumb.png" width="644" height="479" /></a> </p>
<p>I’ve never understood how to interpret this view, but NDepend users who do will be glad to know they can now access it extremely easily <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>One thing that i can see myself make extensive usage of are the following features:</p>
<p><a href="http://davybrion.com/pictures/NDepend3Preview_1277D/08.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="08" border="0" alt="08" src="http://davybrion.com/pictures/NDepend3Preview_1277D/08_thumb.png" width="681" height="489" /></a> </p>
<p>I’ve only played around with it for about 15 minutes now, but i’m impressed.&#160; I never really liked NDepend 2 because i always got completely lost in the VisualNDepend UI.&#160; Now that i have all of this integrated in Visual Studio, it just seems much easier to get to the information i really want.&#160; And if you have to review a lot of code (like me), then this new NDepend version is sure to save you <em>a lot</em> of time.&#160;&#160; It’s pretty much everything you’ve always wanted or already loved from NDepend, but much more easily accessible.</p>
<p>I did notice some small visual glitches (which might be related to running this in a VMWare virtual machine on OS X) and at one time even a Visual Studio crash, but i’m sure those issues will be fixed before the final release.</p>
<p>Oh, and i’m also very happy to note that <a href="http://davybrion.com/blog/2009/12/ndepend-and-lack-of-cohesion-of-methods-not-to-be-trusted/" target="_blank">this particular issue</a> has also been resolved <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/Gfhy0ToxI0V4witUJ31hQ8UFmOA/0/da"><img src="http://feedads.g.doubleclick.net/~a/Gfhy0ToxI0V4witUJ31hQ8UFmOA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Gfhy0ToxI0V4witUJ31hQ8UFmOA/1/da"><img src="http://feedads.g.doubleclick.net/~a/Gfhy0ToxI0V4witUJ31hQ8UFmOA/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=5SYjU6IszFY:hdN0AOcmO4I:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=5SYjU6IszFY:hdN0AOcmO4I:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=5SYjU6IszFY:hdN0AOcmO4I:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/5SYjU6IszFY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/01/ndepend-3-preview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/01/ndepend-3-preview/</feedburner:origLink></item>
		<item>
		<title>Securing Your Agatha Service Layer</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/9YQkRV6Nlo8/</link>
		<comments>http://davybrion.com/blog/2010/01/securing-your-agatha-service-layer/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 20:05:11 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/securing-your-agatha-service-layer/</guid>
		<description><![CDATA[The question of how to implement security for an Agatha Service Layer is one that comes up frequently.&#160; First of all, you need to remember that if you’re using Agatha with WCF you can use any of the WCF features that you’d normally use to secure your WCF service.&#160; There’s already plenty of information available [...]]]></description>
			<content:encoded><![CDATA[<p>The question of how to implement security for an Agatha Service Layer is one that comes up frequently.&#160; First of all, you need to remember that if you’re using Agatha with WCF you can use any of the WCF features that you’d normally use to secure your WCF service.&#160; There’s already plenty of information available online or in books on implementing security for WCF services so i’m not going to spend time on covering those options.&#160; What i am going to cover is the approach that we typically use for our Agatha service layers.</p>
<p>I don’t like to tie myself to WCF-specific features, so i always plug in custom authentication into either a custom Request Processor, or a base Request Handler class that all other Request Handlers must inherit from.&#160;&#160; But first, how do we get the authentication-related data from the client to the service?</p>
<p>In each project i use Agatha in, i always have a MyProjectRequest and MyProjectResponse base class:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectRequest</span> : <span style="color: #2b91af">Request</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> UserName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">byte</span>[] PasswordHash { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectResponse</span> : <span style="color: #2b91af">Response</span> {}</p>
</p></div>
<p>&#160;</p>
<p>Each request in the project inherits from this base request, and each response inherits from the base response.&#160; The base response class is often empty, though this does make it very easy if you ever need to send something back with every response.</p>
<p>Now obviously, if every single request that is sent to your service layer needs values for the UserName and PasswordHash properties you want this to be done in only one place.&#160; I do this by using a custom request dispatcher:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectAsyncRequestDispatcher</span> : <span style="color: #2b91af">AsyncRequestDispatcher</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IUserContext</span> userContext;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> MyProjectAsyncRequestDispatcher(<span style="color: #2b91af">IAsyncRequestProcessor</span> requestProcessor, <span style="color: #2b91af">IUserContext</span> userContext) : <span style="color: blue">base</span>(requestProcessor)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.userContext = userContext;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> BeforeSendingRequests(<span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Request</span>&gt; requestsToProcess)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">base</span>.BeforeSendingRequests(requestsToProcess);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> (<span style="color: blue">var</span> myProjectRequest <span style="color: blue">in</span> requestsToProcess.OfType&lt;<span style="color: #2b91af">MyProjectRequest</span>&gt;())</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; myProjectRequest.UserName = userContext.UserName;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; myProjectRequest.PasswordHash = userContext.PasswordHash;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>The IUserContext dependency is just a component that is registered in my IOC container and will be injected automatically whenever you get an instance of IAsyncRequestDispatcher.&#160;&#160; Now, in this example you can see that i add the authentication data to <em>every request </em>in a batch, even though the batch will be sent in one roundtrip.&#160; If you want, you can add the authentication data only to the first request and then only use the first request to do the authentication within your service layer.&#160; I prefer to add the authentication data to each request and then authenticate every single request (even subsequent requests in a batch) within the service layer.&#160; I’ll get back to this point later on.</p>
<p>Now, the only thing we need to do to make sure that your requests will always have the authentication data contained within them is to instruct Agatha to always use instances of your MyProjectAsyncRequestDispatcher class whenever an IAsyncRequestDispatcher is needed.&#160; This is easily done during Agatha’s client-side configuration:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ClientConfiguration</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">MyProjectRequest</span>).Assembly, <span style="color: blue">new</span> Agatha.Castle.<span style="color: #2b91af">Container</span>(myContainerWrapper))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AsyncRequestDispatcherImplementation = <span style="color: blue">typeof</span>(<span style="color: #2b91af">MyProjectAsyncRequestDispatcher</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Initialize();</p>
</p></div>
<p>&#160;</p>
<p>Keep in mind that you still have to register your IUserContext with the container on your own though.&#160; </p>
<p>With that in place, we are sure that each request that comes from <em>our clients</em> always contains the proper authentication data.&#160; Now we need to make sure that we actually authenticate these requests within the service layer.&#160; You basically have 2 options: either implement your own Request Processor which adds authentication checks, or create a base Request Handler that your other Request Handlers inherit from.</p>
<p>We’ll first cover the option of using a custom Request Processor.&#160;&#160; You could create one like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectRequestProcessor</span> : <span style="color: #2b91af">RequestProcessor</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IAuthenticator</span> authenticator;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> MyProjectRequestProcessor(<span style="color: #2b91af">IAuthenticator</span> authenticator, <span style="color: #2b91af">ServiceLayerConfiguration</span> serviceLayerConfiguration, <span style="color: #2b91af">ICacheManager</span> cacheManager) </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: blue">base</span>(serviceLayerConfiguration, cacheManager)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.authenticator = authenticator;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> BeforeHandle(<span style="color: #2b91af">Request</span> request)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> myProjectRequest = request <span style="color: blue">as</span> <span style="color: #2b91af">MyProjectRequest</span>;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (myProjectRequest != <span style="color: blue">null</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!authenticator.AreValidCredentials(myProjectRequest.UserName, myProjectRequest.PasswordHash))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#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">MySecurityException</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>The BeforeHandle virtual method is called right before the request is passed to its Request Handler to be handled.&#160; Note that this Request Processor would authenticate <em>every </em>request.&#160; If you want a Request Processor that only authenticates the first request, you could do so like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectRequestProcessor</span> : <span style="color: #2b91af">RequestProcessor</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IAuthenticator</span> authenticator;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> MyProjectRequestProcessor(<span style="color: #2b91af">IAuthenticator</span> authenticator, <span style="color: #2b91af">ServiceLayerConfiguration</span> serviceLayerConfiguration, <span style="color: #2b91af">ICacheManager</span> cacheManager) </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: blue">base</span>(serviceLayerConfiguration, cacheManager)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.authenticator = authenticator;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> BeforeProcessing(<span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Request</span>&gt; requests)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> myProjectRequest = (<span style="color: #2b91af">MyProjectRequest</span>)requests.ElementAt(0);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!authenticator.AreValidCredentials(myProjectRequest.UserName, myProjectRequest.PasswordHash))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#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">MySecurityException</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>The BeforeProcessing virtual method is called before any of the requests are handled, so you could authenticate only the first request in a batch and then proceed with regular processing if the first one is authenticated.&#160; Now, the big problem that i have with this approach is that you aren’t really in control of what is sent to your service layer.&#160; Yes, you can guarantee that each request coming from <em>your clients</em> contains the proper authentication data.&#160; What you simply can’t guarantee however is what other people or custom clients can send to your service layer.&#160; If they send you a batch of 2 requests, the first containing valid credentials of a normal user for a benign request, it will pass the authentication just fine.&#160; If the second request in that batch contains invalid credentials (to trick your authorization into believing it’s from a user with higher privileges for instance) for a request that could cause some damage (deleting important information or triggering certain tasks or whatever), then you can’t really do anything to prevent that.&#160; Unless of course, you reject this approach and authenticate every single request.</p>
<p>As for the MySecurityException type, that’s up to you as well.&#160; When you configure your Agatha service layer, you can set the SecurityExceptionType property to the type of exception that should be considered as a security exception.&#160; When the Request Processor catches an exception of that type, it will set the ExceptionType property of the response to ExceptionType.Security so you can check for that specific situation in your client.</p>
<p>To configure Agatha to use your custom Request Processor, your configuration code would look like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ServiceLayerConfiguration</span>(<span style="color: #2b91af">Assembly</span>.GetExecutingAssembly(), <span style="color: blue">typeof</span>(<span style="color: #2b91af">MyProjectRequest</span>).Assembly,</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Agatha.Castle.<span style="color: #2b91af">Container</span>(containerWrapper))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RequestProcessorImplementation = <span style="color: blue">typeof</span>(<span style="color: #2b91af">MyProjectRequestProcessor</span>),</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SecurityExceptionType = <span style="color: blue">typeof</span>(<span style="color: #2b91af">MySecurityException</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Initialize();</p>
</p></div>
<p>&#160;</p>
<p>Another alternative is to create a base Request Handler for your project and to have each Request Handler inherit from it.&#160; Something like this, for instance:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">abstract</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectRequestHandler</span>&lt;TRequest, TResponse&gt; : <span style="color: #2b91af">RequestHandler</span>&lt;TRequest, TResponse&gt; </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">where</span> TRequest : <span style="color: #2b91af">MyProjectRequest</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">IAuthenticator</span> Authenticator { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">void</span> BeforeHandle(TRequest request)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">base</span>.BeforeHandle(request);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!Authenticator.AreValidCredentials(request.UserName, request.PasswordHash))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#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">MySecurityException</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Authorize(request);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">virtual</span> <span style="color: blue">void</span> Authorize(TRequest request) {}</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>In case you’re wondering why i’m using Setter-injection here instead of Constructor-injection, read <a href="http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/" target="_blank">this</a>.</p>
<p>I typically prefer the custom Request Handler approach for authentication.&#160; In most applications that we write, authentication is not enough and we need custom authorization checks for many requests.&#160; So i’m going to need a base Request Handler which introduces the virtual Authorize method anyway.&#160; So i might as well do my authentication check right before it. </p>
<p>With the custom Request Handler approach, you probably still want to configure Agatha to use your custom security exception type:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ServiceLayerConfiguration</span>(<span style="color: #2b91af">Assembly</span>.GetExecutingAssembly(), <span style="color: blue">typeof</span>(<span style="color: #2b91af">MyProjectRequest</span>).Assembly,</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Agatha.Castle.<span style="color: #2b91af">Container</span>(containerWrapper))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SecurityExceptionType = <span style="color: blue">typeof</span>(<span style="color: #2b91af">MySecurityException</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Initialize();</p>
</p></div>
<p>&#160;</p>
<p>And then you just need to let your own Request Handlers inherit from your MyProjectRequestHandler.&#160; Authentication will be performed for each request by default, and you can easily add specific authorization logic for every request.&#160; </p>
<p>And those are pretty much the options you have to secure your Agatha Service Layer.&#160;&#160; Oh, and be sure to only expose your Service Layer through SSL <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/TF8ESSb_kNz9F7Mo_yQr6IKpMR8/0/da"><img src="http://feedads.g.doubleclick.net/~a/TF8ESSb_kNz9F7Mo_yQr6IKpMR8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/TF8ESSb_kNz9F7Mo_yQr6IKpMR8/1/da"><img src="http://feedads.g.doubleclick.net/~a/TF8ESSb_kNz9F7Mo_yQr6IKpMR8/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=9YQkRV6Nlo8:eToPo9ImIAM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=9YQkRV6Nlo8:eToPo9ImIAM:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=9YQkRV6Nlo8:eToPo9ImIAM:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/9YQkRV6Nlo8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/01/securing-your-agatha-service-layer/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/01/securing-your-agatha-service-layer/</feedburner:origLink></item>
		<item>
		<title>Agatha Vs NServiceBus?</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/IloL4pASAKI/</link>
		<comments>http://davybrion.com/blog/2010/01/agatha-vs-nservicebus/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 20:50:51 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>
		<category><![CDATA[nservicebus]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/agatha-vs-nservicebus/</guid>
		<description><![CDATA[Ever since i open sourced Agatha, i’ve gotten questions from people who are wondering whether they should use Agatha or NServiceBus.&#160; I’ve also gotten questions about things that people wanted to do with Agatha but that aren’t really supported.&#160; And i’ve also noticed that people were coming to my site with search keywords like “agatha [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since i open sourced <a href="http://code.google.com/p/agatha-rrsl/" target="_blank">Agatha</a>, i’ve gotten questions from people who are wondering whether they should use Agatha or <a href="http://nservicebus.com/" target="_blank">NServiceBus</a>.&#160; I’ve also gotten questions about things that people wanted to do with Agatha but that aren’t really supported.&#160; And i’ve also noticed that people were coming to my site with search keywords like “agatha vs nservicebus”.&#160; The thing is, they are hardly comparable.</p>
<p>Agatha is a Request/Response Service Layer framework.&#160; It basically supports synchronous and asynchronous Request/Response style communication and tries to make it as easy as possible to write a service layer for that type of communication.&#160; Apart from being easy to use, it also encourages a clean implementation of your service layer and a way to minimize the repetitiveness of cross-cutting concerns.&#160; It also enables you to get better performance than with typical Remote Procedure Call or Remote Method Invocation style service layers because it will try to minimize roundtrips by batching requests and responses together.&#160; In the next release, it will also offer a caching layer so certain requests (depending on how you set it up) don’t need to be processed and cached responses can simply be returned.&#160; There’s also support for one-way requests (or fire-and-forget requests) but it has the same downsides as one-way requests with typical WCF services have. That’s pretty much all it does.&#160; That’s probably all it’ll ever do.&#160; In short, it’s just an upgrade over your typical WCF services.</p>
<p>NServiceBus on the other hand also does Request/Response, but that’s just one of the things it can do.&#160; Again, Agatha is essentially an RPC or RMI framework whereas NServiceBus is built on top of one-way messaging technology.&#160; This allows for much more possibilities when it comes to communicating between different applications or different parts or processes within the same application boundary.&#160; A great overview of those possibilities can be found <a href="http://nservicebus.com/ArchitecturalPrinciples.aspx" target="_blank">here</a>.&#160; Because it is based on messaging, there are a lot of benefits that you can get from NServiceBus that you’ll probably never get from Agatha.&#160; For one, the ‘Store &amp; Forward’ messaging model from NServiceBus might seem similar to Agatha’s one-way requests on first sight, but there are some very important differences.&#160; If you send a fire-and-forget request with Agatha, and the service is currently down, then that request is essentially lost.&#160; With NServiceBus, the message is stored in a message queue and it will be processed when the target of the fire-and-forget message comes up again.&#160; From a reliability point of view, that’s obviously a tremendous improvement over what Agatha can offer you.&#160; Another area where NServiceBus truly shines (IMO) is in its <a href="http://en.wikipedia.org/wiki/Publish_subscribe" target="_blank">Publish/Subscribe</a> implementation.&#160; Some people have asked me if i’ll ever provide something like that in Agatha and the answer has always been ‘no’.&#160;&#160; For one, it doesn’t fit into what Agatha tries to do and i don’t see the point in implementing it if a better implementation is available already in another project.&#160;&#160; There’s plenty more interesting things in NServiceBus to deal with more advanced scenarios, which you’ll have to find out about on your own <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>So which one is more suitable for you or your applications? As with every technology choice, it depends.&#160; Agatha is great for most typical business applications.&#160; If you need to communicate between one or more different clients (with different i don’t mean multiple instances but different types of clients like a web app, a silverlight client and/or a WPF client) and<em> one </em>service (you can obviously use it with more than one service as well if you want to) on an application server which encapsulates your business layer then it is a very attractive solution, as long as you don’t need the superior reliability that NServiceBus can offer you.&#160; With superior reliability i particularly mean the ability to still process received messages once the service layer comes back up after having been unavailable.&#160; In my experience, most business applications don’t really need that, since most of those applications are entirely unusable if the service they depend on is down.&#160; In short, if all the possible downsides of using a WCF service layer are not an issue for your project, then Agatha will be a great fit.</p>
<p>If however, you need to maximize reliability, performance and general robustness of a critical enterprise application, then NServiceBus will definitely be a much better choice.&#160; If you’re dealing with many distributed parts, NServiceBus will also make things much easier for you than Agatha (or any other WCF service layer for that matter) will.&#160;&#160; And obviously, if you want to integrate multiple applications while reducing coupling between applications as much as possible, NServiceBus will also be a much better fit than Agatha.&#160; With NServiceBus, you’d only need to share an assembly containing the types of the messages.&#160; With Agatha, you either need to share an assembly with shared request/response types or use proxies for them <em>and</em> you would also have to <em>know</em> about the other applications since you’d need to access their services directly.&#160; This can get quite ugly pretty fast.</p>
<p>And in some cases, you can just use both of them at the same time.&#160; At work we have two projects that use Agatha for all of their internal communication within their own application boundary, yet they use NServiceBus to notify each other of certain events.&#160; Neither of the applications knows about the existence of the other… the only thing they share is one assembly with some shared message types.&#160; We’ve also started working on a new platform where each application in the platform will use Agatha for all of the communication within their own application boundary (since they’re all typical silverlight clients backed by a WCF service style applications) but they will use NServiceBus for every kind of communication that goes outside of the application boundary.</p>
<p>As with many things, it’s just a matter of choosing the right tool for the right job.&#160; Hopefully, this post will help some of you make that decision should you need to make it in the future <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/Mre5wx7_5znYbE6FSsBFuSbgp5Y/0/da"><img src="http://feedads.g.doubleclick.net/~a/Mre5wx7_5znYbE6FSsBFuSbgp5Y/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Mre5wx7_5znYbE6FSsBFuSbgp5Y/1/da"><img src="http://feedads.g.doubleclick.net/~a/Mre5wx7_5znYbE6FSsBFuSbgp5Y/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=IloL4pASAKI:IYHmSLbJyxk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=IloL4pASAKI:IYHmSLbJyxk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=IloL4pASAKI:IYHmSLbJyxk:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/IloL4pASAKI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/01/agatha-vs-nservicebus/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/01/agatha-vs-nservicebus/</feedburner:origLink></item>
		<item>
		<title>Highly Recommended Book: Debug It!</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/FdCQMrmZfTU/</link>
		<comments>http://davybrion.com/blog/2010/01/highly-recommended-book-debug-it/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 06:03:57 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2223</guid>
		<description><![CDATA[I’m not the best debugger out there, but i usually manage to get to the bottom of things and sometimes i even enjoy chasing down a weird bug.&#160; And while i actively try to avoid bugs as much as possible, i’m also always looking to learn more techniques or practices to efficiently find and fix [...]]]></description>
			<content:encoded><![CDATA[<p>I’m not the best debugger out there, but i usually manage to get to the bottom of things and sometimes i even enjoy chasing down a weird bug.&#160; And while i actively try to avoid bugs as much as possible, i’m also always looking to learn more techniques or practices to efficiently find and fix bugs when they do occur.&#160; So when i first heard about Paul Butcher’s <a href="http://www.amazon.com/Debug-Repair-Prevent-Pragmatic-Programmers/dp/193435628X/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1264250644&amp;sr=8-1" target="_blank">Debug It</a>, i immediately preordered it.&#160; And now that i’ve read it, i’m very glad i did.</p>
<p>The book is divided in 3 parts.&#160; The chapters of the first part explain in depth what kind of debugging strategy you’ll need.&#160; Discussed topics include:</p>
<ul>
<li>figuring out what you’re really looking for </li>
<li>tips and tricks to come up with a reliable reproduction of the bug and the value of having that reproduction </li>
<li>diagnosing the <em>actual</em> problem </li>
<li>coming up with a <em>true</em> fix </li>
<li>reflecting on why the bug ever got into the software</li>
<li>making sure that the bug can’t come back and that we learn from the mistake</li>
</ul>
<p>For some of you, most of this stuff will just be common sense.&#160; For a lot of developers though, this part alone should be considered required reading.&#160; It’s a complete process of how to deal with bugs efficiently in less than 80 pages!&#160; Think about that for a second: 80 pages that <em>will</em> improve your efficiency at your job and will reduce the amount of time you spend doing something you probably don’t like doing.&#160; How could you resist?</p>
<p>The second part is pretty short (only 2 chapters) but pretty interesting as well.&#160; It mostly deals with organizational patterns and practices that a development shop should take into account.&#160; It covers things such as:</p>
<ul>
<li>
<div align="left">the importance of bug tracking</div>
</li>
<li>
<div align="left">what makes a good bug report</div>
</li>
<li>
<div align="left">effective communication with users and support staff</div>
</li>
<li>
<div align="left">giving priority to bugs as soon as they’re discovered</div>
</li>
<li>
<div align="left">the importance of pragmatic zero-tolerance for bugs</div>
</li>
<li>
<div align="left">ways to get out of a Quality Hole</div>
</li>
</ul>
<p align="left">Not all developers will like the second part, but it definitely contains some valuable information for technical managers, or for developers who need to convince their technical managers <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p align="left">The third part of the book deals with a lot of various strategies for a variety of specific situations.&#160; While not everyone will get something out of every topic discussed in this part, odds are that quite a few of them will indeed be interesting for you.&#160; You’ll find specific advice for quite a few special cases (performance, concurrency, backwards compatibility, third-party bugs, etc…).&#160; You’ll also find the obligatory chapter on creating an ideal debugging environment with automated tests, a build system and continuous integration.&#160; If you’re reading this blog, you’re hopefully already convinced of the values of such things so you might want to skip this chapter.&#160; The final 2 chapters are again very interesting… They’ll show you how you can write software that will debug itself.&#160; While not everyone will actually go out and do this, some ideas of that chapter should definitely be kept in mind by most of us.&#160; The final chapter deals with some common (mostly organizational) anti-patterns for dealing with bugs.</p>
<p align="left">All in all, there is just a lot of tremendously valuable information in this book.&#160; And it’s only about 190 pages so it definitely won’t take you a long time to read it.&#160; I’ve frequently been amazed at the inability of developers to efficiently debug issues when they occur.&#160; And i’m not just talking about bad developers.&#160; I’ve seen plenty of good or even great developers having trouble with debugging efficiently.&#160; This book would definitely get them on the right track, with just a little bit effort.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/irwr0HY-6bzUxftykGNkDhtdRRQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/irwr0HY-6bzUxftykGNkDhtdRRQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/irwr0HY-6bzUxftykGNkDhtdRRQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/irwr0HY-6bzUxftykGNkDhtdRRQ/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=FdCQMrmZfTU:B13UA2Pa4Eo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=FdCQMrmZfTU:B13UA2Pa4Eo:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=FdCQMrmZfTU:B13UA2Pa4Eo:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/FdCQMrmZfTU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/01/highly-recommended-book-debug-it/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/01/highly-recommended-book-debug-it/</feedburner:origLink></item>
		<item>
		<title>I Still Have Low Expectations For Visual Studio 2010</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/ybKWMSeCM1w/</link>
		<comments>http://davybrion.com/blog/2010/01/i-still-have-low-expectations-for-visual-studio-2010/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 11:46:39 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[IDE]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/i-still-have-low-expectations-for-visual-studio-2010/</guid>
		<description><![CDATA[Last month i told you i had very low expectations for Visual Studio 2010 from a performance point of view.&#160; Luckily, i was not alone as many people complained about it.&#160; Microsoft has been working hard on the performance of VS2010 ever since.&#160; Today i read a new post from Brian Harry about the results [...]]]></description>
			<content:encoded><![CDATA[<p>Last month i told you i had <a href="http://davybrion.com/blog/2009/12/very-low-expectations-for-visual-studio-2010/" target="_blank">very low expectations for Visual Studio 2010</a> from a performance point of view.&#160; Luckily, i was not alone as many people complained about it.&#160; Microsoft has been working hard on the performance of VS2010 ever since.&#160; Today i read a <a href="http://blogs.msdn.com/bharry/archive/2010/01/24/state-of-vs-2010-performance.aspx" target="_blank">new post</a> from Brian Harry about the results of their performance improvements.</p>
<p>Some quotes that make me cringe:</p>
<blockquote><p>The performance is acceptable now and I would consider the product generally shippable…</p>
</blockquote>
<p>Acceptable? One of the originally stated goals for Visual Studio 2010 was better performance over 2008.&#160; At this point, it should be more than acceptable IMO!</p>
<blockquote><p>I&#8217;ve been running the SLCTP3 for about 3.5 hours today and its been amazing. Previously I would have crashed at least 2 times and had insane perf issues.</p>
</blockquote>
<p>I can only hope that it runs without crashing and insane performance issues for those of us who are using it 8 hours (or more) a day…</p>
<blockquote><p>I&#8217;d say the performance is about equal or maybe slightly better in some scenarios than VS2008.</p>
</blockquote>
<p>I find this very disappointing… again, it was supposed to be faster than VS2008</p>
<blockquote><p>VS2008 still feels snappier when compared side-by-side on the same VM, but the performance doesn&#8217;t bother me in this build of VS2010.</p>
</blockquote>
<p>I’m glad it doesn’t bother Brian, but i sure as hell would prefer that the new version is at least faster and snappier than the previous version</p>
<blockquote><p>Build time is 10-15% longer for the same solution compared to 2008</p>
</blockquote>
<p>Excuse me? Build time in 2008 is already ridiculously slow for large solutions… the fact that it has gotten slower <em>on the same hardware</em> is simply unacceptable</p>
<blockquote><p>Compilation of WPF project is &gt;500% slower on VS 2010 SLCTP 3</p>
</blockquote>
<p>Wow… just wow</p>
<blockquote><p>Intellisense pop-ups are slow to pop up</p>
</blockquote>
<p>It’s not like we need those to be fast, right?</p>
<p>For those of you who think i’m making a big deal out of this, you might be right.&#160; But i’m often working with large solutions and i frequently have multiple instances of VS running at the same time.&#160; The performance of VS2008 is at times embarrassingly bad and to think that VS2010 is not going to improve this, and likely even make the situation worse is something that i can’t really be happy about.&#160; </p>
<p>Can’t we just get a Visual Studio 2008 Service Pack with support for .NET 4.0 instead?</p>

<p><a href="http://feedads.g.doubleclick.net/~a/2xemgRWGmhkV19TxrN0S5TeLODo/0/da"><img src="http://feedads.g.doubleclick.net/~a/2xemgRWGmhkV19TxrN0S5TeLODo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/2xemgRWGmhkV19TxrN0S5TeLODo/1/da"><img src="http://feedads.g.doubleclick.net/~a/2xemgRWGmhkV19TxrN0S5TeLODo/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=ybKWMSeCM1w:qnuKIINRyaw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=ybKWMSeCM1w:qnuKIINRyaw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=ybKWMSeCM1w:qnuKIINRyaw:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/ybKWMSeCM1w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/01/i-still-have-low-expectations-for-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/01/i-still-have-low-expectations-for-visual-studio-2010/</feedburner:origLink></item>
		<item>
		<title>As A Movement, ALT.NET Has Been Dead For A While</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/zDhumdeC8xc/</link>
		<comments>http://davybrion.com/blog/2010/01/as-a-movement-alt-net-has-been-dead-for-a-while/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 15:06:58 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[ALT.NET]]></category>
		<category><![CDATA[Opinions]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/as-a-movement-alt-net-has-been-dead-for-a-while/</guid>
		<description><![CDATA[Quite a few posts have been written lately on the state of the ALT.NET community, or movement or whatever else you want to consider or call it.&#160; I’ve always thought of ALT.NET in 3 ways.

it is a mindset 
it is a community 
there is a movement 

The ALT.NET mindset was originally described perfectly by Dave [...]]]></description>
			<content:encoded><![CDATA[<p>Quite a few posts have been written lately on the state of the ALT.NET community, or movement or whatever else you want to consider or call it.&#160; I’ve always thought of ALT.NET in 3 ways.</p>
<ol>
<li>it is a mindset </li>
<li>it is a community </li>
<li>there is a movement </li>
</ol>
<p>The ALT.NET mindset was originally described perfectly by <a href="http://laribee.com/blog/2007/04/10/altnet/" target="_blank">Dave Laribee</a>:</p>
<blockquote><ol>
<li>You’re the type of developer who uses what works while keeping an eye out for a better way. </li>
<li>You reach outside the mainstream to adopt the best of any community: Open Source, Agile, Java, Ruby, etc. </li>
<li>You’re not content with the status quo. Things can always be better expressed, more elegant and simple, more mutable, higher quality, etc. </li>
<li>You know tools are great, but they only take you so far. It’s the principles and knowledge that really matter. The best tools are those that embed the knowledge and encourage the principles (e.g. Resharper.) </li>
</ol>
</blockquote>
<p>This mindset still lives on, and will always stay alive. There will always be .NET developers that are continuously looking for better ways to create software.&#160; We might not think there are enough of them, but they are still growing gradually.&#160; Typical ALT.NET topics, practices and principles have become much more popular and more and more people are definitely still looking to learn about them.&#160; I can’t think of a single reason why this would ever cease to happen.</p>
<p>As a community, ALT.NET hasn’t done a good job (IMO) of being accessible to new-comers.&#160; I’ve complained about this <a href="http://davybrion.com/blog/2008/08/why-the-altnet-community-needs-to-change-their-ways/" target="_blank">in August 2008</a>.&#160; Nothing changed after that post, and nothing will change after this post either.&#160; A lot of the people in the ALT.NET community are still too negative in expressing their views, or have even stopped trying altogether.&#160; Some of them just attacked everyone who disagreed with them.&#160; Things like that will obviously never, ever lead to adoption of your thoughts and ideas.&#160; And after that, they’d complain that nobody was listening anyway and that they were tired of trying to discuss these things with people who didn’t believe in it yet. It also doesn’t help that quite a few people got involved purely for the sake of bettering themselves.&#160; After all, the ALT.NET name was hot and being associated with it and getting involved with it was an easy way to improve your reputation, status and for some even their finances.</p>
<p>As a movement, we’ve failed as well.&#160; Essentially, the ALT.NET movement is about education.&#160; Teaching people the things we believe in.&#160; Showing them that there are better ways to develop software than that what is more accepted in the Microsoft world.&#160; In the early stages of the movement, there was a tremendous amount of interesting blog posts being written with exactly that purpose in mind.&#160; Teaching people.&#160; Showing them the benefits.&#160; Explaining things to them.&#160;&#160; After a while though, some of those bloggers didn’t quite put the same effort into it anymore.&#160; A lot of them resorted to one-liners about why approach A is better than approach B and they weren’t really taking the time anymore to try to educate people.&#160; The rise of Twitter certainly didn’t help either since you simply can’t teach this stuff to people through 140-character tweets.&#160; You can’t really show them the benefit of whatever it is that you think is a good thing to do.&#160; And since a lot of those bloggers are spending more of their time on Twitter than on working on their blogs nowadays, a lot of valuable knowledge and experience isn’t being spread as much anymore as it used to.&#160; Now obviously, we can’t tell people how to spend their time so if those people prefer to spend time on Twitter instead of writing good posts that could help more people, that is their personal choice and nobody can blame them for that.&#160;&#160; At the same time, they can’t really complain about the current state of ALT.NET as a movement and a community either.</p>
<p>But hey, the mindset still lives and will continue on living.&#160; The best thing you can do is to share your knowledge and experience with as many people that want to hear it.&#160; And trust me, there are quite a few people who want to hear it.&#160; </p>

<p><a href="http://feedads.g.doubleclick.net/~a/3hwA4FHXHPmdg_Q1HGsi_Uz8Ixk/0/da"><img src="http://feedads.g.doubleclick.net/~a/3hwA4FHXHPmdg_Q1HGsi_Uz8Ixk/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/3hwA4FHXHPmdg_Q1HGsi_Uz8Ixk/1/da"><img src="http://feedads.g.doubleclick.net/~a/3hwA4FHXHPmdg_Q1HGsi_Uz8Ixk/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=zDhumdeC8xc:YOF1OIVkrVk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=zDhumdeC8xc:YOF1OIVkrVk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=zDhumdeC8xc:YOF1OIVkrVk:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/zDhumdeC8xc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/01/as-a-movement-alt-net-has-been-dead-for-a-while/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/01/as-a-movement-alt-net-has-been-dead-for-a-while/</feedburner:origLink></item>
		<item>
		<title>Inversion Of Control Containers And Factories Aren’t Mutually Exclusive</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/ra-JpWLOUL0/</link>
		<comments>http://davybrion.com/blog/2010/01/inversion-of-control-containers-and-factories-arent-mutually-exclusive/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 15:14:45 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Inversion Of Control]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2224</guid>
		<description><![CDATA[Julian Birch recently posted a reaction to my reaction to Uncle Bob’s IOC lunacy post.&#160; Julian mistakenly thinks that i have a problem with using factories.&#160; I definitely don’t have a problem with using factories.&#160; I just have a problem with using them in the manner that Uncle Bob suggested in his post.&#160; Jeffrey Palermo [...]]]></description>
			<content:encoded><![CDATA[<p>Julian Birch recently posted a <a href="http://www.colourcoding.net/Blog/archive/2010/01/19/dependency-reversi.aspx" target="_blank">reaction</a> to <a href="http://davybrion.com/blog/2010/01/dependency-injection-inversion-rejection/" target="_blank">my reaction to Uncle Bob’s IOC lunacy post</a>.&#160; Julian mistakenly thinks that i have a problem with using factories.&#160; I definitely don’t have a problem with using factories.&#160; I just have a problem with using them in the manner that Uncle Bob suggested in his post.&#160; Jeffrey Palermo also recently posted <a href="http://jeffreypalermo.com/blog/constructor-over-injection-anti-pattern/" target="_blank">an example</a> of where he thinks using a factory is better than injecting dependencies.&#160; I wanted to react to both those posts with a real-world example of where i prefer to use a factory and how i use an IOC container to do so.</p>
<p>As you probably know by now, i’m a big proponent of using IOC containers.&#160; I’ve never stated that they should be used in every single application, but using one certainly pays off in most applications, especially as complexity increases gradually.&#160; When you use an IOC container, you’ll have much less need for factories.&#160;&#160; There are however two situations where i certainly prefer to use a factory.&#160; And that is when:</p>
<ol>
<li>a certain dependency might not always be used by a class <em>and</em> that dependency is expensive to create </li>
<li>i might need multiple instances of a dependency during the lifetime of a class </li>
</ol>
<p>A good example of both those situations is when using <a href="http://code.google.com/p/agatha-rrsl/" target="_blank">Agatha</a>’s AsyncRequestDispatcher class from a Silverlight user control.&#160; Creating an AsyncRequestDispatcher is expensive because it in turn requires an instance of the AsyncRequestProcessorProxy class.&#160; The AsyncRequestProcessorProxy class inherits from WCF’s ClientBase class, and those types are relatively expensive to create.&#160;&#160; And due to the asynchronous nature of those service calls, you can never deterministically dispose of a AsyncRequestDispatcher instance with absolute certainty that it won’t be disposed <em>before</em> the response of the service call is returned from the service.&#160; Because of that, the AsyncRequestDispatcher class was designed to dispose of itself automatically once the response is returned.&#160; This effectively means that you’ll need a new instance of AsyncRequestDispatcher whenever you need to make an asynchronous call to an Agatha service.&#160; </p>
<p>For most user controls, it obviously doesn’t make sense to inject one instance of the AsyncRequestDispatcher into the user control because you often need to make multiple service calls depending on user interactions or other events.&#160;&#160; A good way to deal with this is obviously to ask a factory to create the AsyncRequestDispatcher whenever you need one.&#160;&#160; Which is why Agatha offers the AsyncRequestDispatcherFactory class, which looks like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IAsyncRequestDispatcherFactory</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IAsyncRequestDispatcher</span> CreateAsyncRequestDispatcher();</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">AsyncRequestDispatcherFactory</span> : <span style="color: #2b91af">IAsyncRequestDispatcherFactory</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">IAsyncRequestDispatcher</span> CreateAsyncRequestDispatcher()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: #2b91af">IoC</span>.Container.Resolve&lt;<span style="color: #2b91af">IAsyncRequestDispatcher</span>&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Now, some of you are probably thinking: what is the difference between this and Uncle Bob’s example?&#160; Well, unlike Uncle Bob’s example this factory is not responsible for registering the required components to create an AsyncRequestDispatcher with the container.&#160; It merely resolves an instance and returns it.&#160; And yes, it uses the container to do so.&#160;&#160; I could actually just new up an AsyncRequestDispatcher myself but then the factory would also have to <em>know</em> about its dependencies and make sure it’d be able to create them.&#160; If those dependencies have dependencies of their own, i’m back to dealing with dependencies manually which is exactly what i’m trying to avoid throughout my codebase.</p>
<p>I can’t come up with a single reason why this would be wrong, but some of you probably will so feel free to point out those reasons <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The benefit of this factory is that it enables me to delay the instantiation of an expensive dependency, and it also enables me to get more than one instance if i need to during the lifetime of a single object.&#160; At the same time, it doesn’t have <em>any</em> of the downsides of Uncle Bob’s approach.&#160; Also, this approach is something that you won’t need to resort to throughout your codebase, it’s more for edge-cases.</p>
<p>Now, how do we use this factory?&#160; Instead of new’ing the factory manually like Jeffrey does in his example, the factory is registered with the IOC container and i simply inject the factory into the class that needs to use it.&#160; This still enables me to change implementations of both the factory as well as the instances the factory needs to create.&#160; And it also makes it easy to stub or mock the factory during tests.&#160; I get all of the benefits from using Dependency Injection and an IOC container, and i have yet to notice any downsides to this approach.&#160; But again, i only use this approach in the 2 situations i mentioned in the beginning of this post.&#160; In most cases, you really don’t need factories anymore.&#160; And when you do, just leverage your IOC container to make the factory as simple and dumb as possible.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/Kd1hwbpejbPsIb21IkkMFsFD624/0/da"><img src="http://feedads.g.doubleclick.net/~a/Kd1hwbpejbPsIb21IkkMFsFD624/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Kd1hwbpejbPsIb21IkkMFsFD624/1/da"><img src="http://feedads.g.doubleclick.net/~a/Kd1hwbpejbPsIb21IkkMFsFD624/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=ra-JpWLOUL0:ZLpJ-AaEeT8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=ra-JpWLOUL0:ZLpJ-AaEeT8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=ra-JpWLOUL0:ZLpJ-AaEeT8:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/ra-JpWLOUL0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/01/inversion-of-control-containers-and-factories-arent-mutually-exclusive/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2010/01/inversion-of-control-containers-and-factories-arent-mutually-exclusive/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.820 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-03-14 12:15:12 -->
