<?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>out &#62;&#62; m_Conscientia; &#187; Dependency Injection</title>
	<atom:link href="http://blog.hypercomplex.co.uk/index.php/tag/dependency-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hypercomplex.co.uk</link>
	<description>a multidimensional braindump</description>
	<lastBuildDate>Sat, 27 Aug 2011 19:16:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>When dependency injection meets IDisposable</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 21:50:58 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[IDisposable]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=500</guid>
		<description><![CDATA[What follows is a solution which should really have been obvious to me the first time I faced the problem: how do you decouple logic and instantiation of collaborating objects when faced with a class you need to instantiate inline, such as an IDisposable class? Consider this: public class NiceClass &#123; private DependentClass injectedDependency; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>What follows is a solution which should really have been obvious to me the first time I faced the problem: how do you decouple logic and instantiation of collaborating objects when faced with a class you need to instantiate inline, such as an IDisposable class? Consider this:</p>

<div class="wp_codebox"><table><tr id="p5004"><td class="code" id="p500code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NiceClass
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> DependentClass injectedDependency<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NiceClass<span style="color: #008000;">&#40;</span>DependentClass injectedDependency<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">injectedDependency</span> <span style="color: #008000;">=</span> injectedDependency<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CoupledMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IFace c <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Face<span style="color: #008000;">&#40;</span>injectedDependency<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// do something with c</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>We can see that injectedDependency is not coupled, but what about the local instance of Face in CoupledMethod? To decouple it use a delegate to return the IFace instance instead of calling new directly. If the coupling had been to a concrete type (Face), we could refactor to an abstract type (IFace) or wrapper first.</p>

<div class="wp_codebox"><table><tr id="p5005"><td class="code" id="p500code5"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NiceClass
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> DependentClass injectedDependency<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Func<span style="color: #008000;">&lt;</span>DependentClass, IFace<span style="color: #008000;">&gt;</span> faceFactoryMethod<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NiceClass<span style="color: #008000;">&#40;</span>
        DependentClass injectedDependency, 
        Func<span style="color: #008000;">&lt;</span>DependentClass, IFace<span style="color: #008000;">&gt;</span> faceFactoryMethod<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">injectedDependency</span> <span style="color: #008000;">=</span> injectedDependency<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">faceFactoryMethod</span> <span style="color: #008000;">=</span> faceFactoryMethod<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CoupledMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IFace c <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">faceFactoryMethod</span><span style="color: #008000;">&#40;</span>injectedDependency<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// do something with c</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This is an additional three lines of code, which arguably makes the control flow harder to read statically. It does, however, improve testability. I think of this as equivalent to the classic tradeoff between <a href="http://www.threeriversinstitute.org/AbstractVsConcreteParameters.html">abstract and concrete parameters</a>.</p>
<p>Instantiating NiceClass is not too arduous either with a lambda expression (although it is still a little verbose).</p>

<div class="wp_codebox"><table><tr id="p5006"><td class="code" id="p500code6"><pre class="csharp" style="font-family:monospace;">NiceClass nice <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> NiceClass<span style="color: #008000;">&#40;</span>dependency, d <span style="color: #008000;">=&gt;</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Face<span style="color: #008000;">&#40;</span>d<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dependency Injection &amp; Testability</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/06/dependency-injection-testability/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/06/dependency-injection-testability/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 19:01:06 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[code coverage]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[talk]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=111</guid>
		<description><![CDATA[When objects have no (or few) collaborators it&#8217;s relatively easy to get high unit test code coverage. As the number and complexity of collaborators increases, so too does the difficulty in obtaining decent coverage. This talk illustrates why dependency injection principles improve the testability of your code, and ease mock/stub based unit testing.]]></description>
			<content:encoded><![CDATA[<p>When objects have no (or few) collaborators it&#8217;s relatively easy to get high unit test code coverage. As the number and complexity of collaborators increases, so too does the difficulty in obtaining decent coverage. This talk illustrates why dependency injection principles improve the testability of your code, and ease <a href="http://martinfowler.com/articles/mocksArentStubs.html" target="_blank">mock/stub based unit testing</a>.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://www.youtube.com/v/RlfLCWKxHJ0&amp;color1=0xb1b1b1&amp;color2=0xcfcfcf&amp;hl=en&amp;feature=player_embedded&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/RlfLCWKxHJ0&amp;color1=0xb1b1b1&amp;color2=0xcfcfcf&amp;hl=en&amp;feature=player_embedded&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/06/dependency-injection-testability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

