<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rob bowley &#187; dependency injection</title>
	<atom:link href="http://blog.robbowley.net/tag/dependency-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.robbowley.net</link>
	<description>adventures in extreme programming</description>
	<lastBuildDate>Thu, 15 Jul 2010 08:12:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Less interface intensive dependency injection (C#)</title>
		<link>http://blog.robbowley.net/2010/01/19/less-interface-intensive-dependency-injection-c/</link>
		<comments>http://blog.robbowley.net/2010/01/19/less-interface-intensive-dependency-injection-c/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 14:44:31 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[IoC]]></category>

		<guid isPermaLink="false">http://blog.robbowley.net/?p=1118</guid>
		<description><![CDATA[In a study session at work last week we were having a discussion about how with dependency injection you can end up with loads of anaemic interfaces. Arguably these interfaces provide little value. However in C# at least there&#8217;s an alternative (thanks Josh and I think also Joe Campbell for this idea) &#8211; instead of [...]]]></description>
			<content:encoded><![CDATA[<p>In a study session at work last week we were having a discussion about how with dependency injection you can end up with loads of anaemic interfaces. Arguably these interfaces provide little value. However in C# at least there&#8217;s an alternative (thanks Josh and I think also <a href="http://joecampbell.wordpress.com/">Joe Campbell</a> for this idea) &#8211; instead of taking an interface for a dependency in the client&#8217;s constructor, use a delegate instead.</p>
<p>So, instead of this &#8220;poor man&#8217;s&#8221; dependency injection example from my <a href="http://blog.robbowley.net/2010/01/18/not-so-poor-mans-dependency-injection/">last post</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IUserPaymentRepository
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">void</span> MakeTransaction<span style="color: #000000;">&#40;</span>Price amount<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TrackPaymentActivity
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">private</span> UserPaymentRepository _userPaymentRepository<span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> TrackPaymentActivity<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">:</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> UserPaymentRepository<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> TrackPaymentActivity<span style="color: #000000;">&#40;</span>IUserPaymentRepository userPaymentRepository<span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">this</span>._userPaymentRepository <span style="color: #008000;">=</span> userPaymentRepository<span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> AttemptToPayForTrack<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          ......
          _userPaymentRepository.<span style="color: #0000FF;">MakeTransaction</span><span style="color: #000000;">&#40;</span>trackPrice<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          ......
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>you can do this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TrackPaymentActivity
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">private</span> Action<span style="color: #008000;">&lt;</span>Price<span style="color: #008000;">&gt;</span> _makeTransaction<span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> TrackPaymentActivity<span style="color: #000000;">&#40;</span>Action<span style="color: #008000;">&lt;</span>Price<span style="color: #008000;">&gt;</span> makeTransaction<span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">this</span>._makeTransaction <span style="color: #008000;">=</span> _makeTransaction<span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> AttemptToPayForTrack<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          ......
          _makeTransaction<span style="color: #000000;">&#40;</span>trackPrice<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          ......
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So how do you test this? Mocking frameworks don&#8217;t (and probably couldn&#8217;t) support delegates so you&#8217;ll need to create an interface which has a method with the signature of the delegate, but only for testing purposes:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">interface</span> ITestPaymentTransaction
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">void</span> MakeTransaction<span style="color: #000000;">&#40;</span>Price amount<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Should_Take_Correct_Payment_Amount_For_Track_From_User<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     IUserPaymentRepository mockedTransaction <span style="color: #008000;">=</span>
               MockRepository.<span style="color: #0000FF;">GenererateMock</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #008000;">new</span> TrackPaymentActivity<span style="color: #000000;">&#40;</span>mockedTransaction.<span style="color: #0000FF;">MakeTransaction</span><span style="color: #000000;">&#41;</span>
               .<span style="color: #0000FF;">AttemptToPayForTrack</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     mockedTransaction.<span style="color: #0000FF;">AssertWasCalled</span><span style="color: #000000;">&#40;</span>transaction <span style="color: #008000;">=&gt;</span> transaction.<span style="color: #0000FF;">MakeTransaction</span><span style="color: #000000;">&#40;</span>expectedAmount<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In most situations I think this is preferable. You&#8217;re still having to create an interface, but it&#8217;s not creating noise in your production code. It also means you can&#8217;t use an IoC container, but as I said in my <a href="http://blog.robbowley.net/2010/01/18/not-so-poor-mans-dependency-injection/">last post</a>, in many situations you probably don&#8217;t need to anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.robbowley.net/2010/01/19/less-interface-intensive-dependency-injection-c/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Not so poor man&#8217;s dependency injection</title>
		<link>http://blog.robbowley.net/2010/01/18/not-so-poor-mans-dependency-injection/</link>
		<comments>http://blog.robbowley.net/2010/01/18/not-so-poor-mans-dependency-injection/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 13:23:14 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[IoC]]></category>

		<guid isPermaLink="false">http://blog.robbowley.net/?p=1082</guid>
		<description><![CDATA[Uncle Bob&#8217;s written a  post explaining why he tries to be very sparing with the use of IoC containers. I couldn&#8217;t agree more. Overuse of IoC containers can end up with a huge amount of indirection and noise in compared to the value they may provide*.
In my mind, the main benefit of dependency injection is [...]]]></description>
			<content:encoded><![CDATA[<p>Uncle Bob&#8217;s written a  post explaining why he <a href="http://blog.objectmentor.com/articles/2010/01/17/dependency-injection-inversion">tries to be very sparing with the use of IoC containers</a>. I couldn&#8217;t agree more. Overuse of IoC containers can end up with a huge amount of indirection and noise in compared to the value they may provide*.</p>
<p>In my mind, the main benefit of dependency injection is testing and is crucial to being able to do TDD. If you do TDD you want to test one thing at a time (one logical assertion per test). If you find you need another piece of logic which is not the responsibility of the object you&#8217;re testing you don&#8217;t carry on writing reams of code until your test passes, you create a stub for the piece of functionality, make your test pass and only then consider implementing the stubbed out code.</p>
<p>I&#8217;ve always been a fan of poor man&#8217;s dependency injection. I can&#8217;t find any good examples on t&#8217;web so I&#8217;ll give an example (in C#). Poor man&#8217;s basically means having a default parameterless constructor and an overloaded one which takes the dependencies. The default constructor calls the overloaded one with concrete instances of any dependencies:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IUserPaymentRepository
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">void</span> MakeTransaction<span style="color: #000000;">&#40;</span>Price amount<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TrackPaymentActivity
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">private</span> UserPaymentRepository _userPaymentRepository<span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> TrackPaymentActivity<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">:</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> UserPaymentRepository<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> TrackPaymentActivity<span style="color: #000000;">&#40;</span>IUserPaymentRepository userPaymentRepository<span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">this</span>._userPaymentRepository <span style="color: #008000;">=</span> userPaymentRepository<span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> AttemptToPayForTrack<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          ......
          _userPaymentRepository.<span style="color: #0000FF;">MakeTransaction</span><span style="color: #000000;">&#40;</span>trackPrice<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          ......
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This allows you to call the parameterless constructor in your production code, but the overloaded one from your tests. I guess this is where it gets it&#8217;s bad name as test hooks are pretty scorned upon and there&#8217;s no denying that&#8217;s what&#8217;s going on here.</p>
<p>Here&#8217;s the thing &#8211; in many of the situations where you need to inject a dependency it&#8217;s for one piece of behaviour and there&#8217;s no decision going on to determine which object you need for that behaviour &#8211; you know there&#8217;s only one place in your code base that has that functionality.  In other words, the dependency is still there! It&#8217;s just that by using an IoC container you&#8217;ve made it magically look like it&#8217;s decoupled. With poor man&#8217;s it&#8217;s easy to see these dependencies but if you&#8217;ve palmed it all of to an IoC  you&#8217;re gonna end up having no idea what&#8217;s using what until you fire up the good ol&#8217; debugger. What&#8217;s worse is your code metrics won&#8217;t pick it up giving you the impression your code is in a better condition than it actually is.</p>
<p>Of course there is a time and a place for IoC containers. It&#8217;s just that it&#8217;s probably a lot less than you thought. If there&#8217;s a decision to be made at runtime about which type that impliments IUserPaymentRepository should be used or there&#8217;s more than one member on the interface (suggesting that it is stateful) then an IoC container would be desirable, otherwise I&#8217;m often quite happy being poor.</p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p>*<em><span style="font-size: small;">I actually don&#8217;t have a huge amount of experience with IoC containers, but that&#8217;s because like Uncle Bob,  I&#8217;ve always tried to avoid them until absolutely necessary, however I do have first hand reports (within orgs I&#8217;ve worked for) of where IoC abuse has caused very expensive problems.</span></em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.robbowley.net/2010/01/18/not-so-poor-mans-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
