<?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>Dude, where is my Kaizen? &#187; Poor mans dependency Injection</title>
	<atom:link href="http://www.bjoernrochel.de/tag/poor-mans-dependency-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bjoernrochel.de</link>
	<description>Björn Rochel&#039;s weblog</description>
	<lastBuildDate>Mon, 21 Jun 2010 07:26:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Poor mans dependency injection</title>
		<link>http://www.bjoernrochel.de/2008/08/29/poor-mans-dependency-injection/</link>
		<comments>http://www.bjoernrochel.de/2008/08/29/poor-mans-dependency-injection/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 09:02:08 +0000</pubDate>
		<dc:creator>BjRo</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[Poor mans dependency Injection]]></category>

		<guid isPermaLink="false">http://www.bjoernrochel.de/?p=104</guid>
		<description><![CDATA[While watching the first of JP Boodhoos screencast series I discovered that a concept that I used in order to introduce testing to developers who work mostly in legacy code (I&#8217;m using Michael Feathers terminology here) is in fact a well known pattern.
Let me give you an example. You&#8217;ve got a class that you want [...]]]></description>
			<content:encoded><![CDATA[<p>While watching the first of JP Boodhoos screencast series I discovered that a concept that I used in order to introduce testing to developers who work mostly in legacy code (I&#8217;m using Michael Feathers terminology here) is in fact a well known pattern.</p>
<p>Let me give you an example. You&#8217;ve got a class that you want test, but this class somehow depends on an untestable resource like for instance a webservice. You don&#8217;t have some kind of dependency inversion to you&#8217;re hand and have to solve this problem on your own. A short example:</p>
<pre class="brush: csharp">
public class BillingService
{
      public BillingResult DoBilling(BillingData data)
      {
            Bill theBil = MakeBill(data);

            using(BillingClient wc = new BillingClient())
            {
                 wc.DoBilling(theBil);
            }
      }
}
</pre>
<p>What I mostly recommend in such a situation is</p>
<ol>
<li>Extract or isolate the ugly (Quote from Jeremy Miller) which is the webservice call here.</li>
<li>Introduce an interface that shields the class from the webservice call.</li>
<li>Make the whole class rely on the abstraction.</li>
<li>Use two constructors. I mostly reffered to them as the opened and the closed constructor.</li>
</ol>
<pre class="brush: csharp">
public class BillingService
{
      private IBillingSystem _BillingSystem;

      public BillingService(IBillingSystem billingSystem)
      {
             _BillingSystem = billingSystem;
      }

      public BillingSystem() : this(new BillingSystem())
      {
      }

      public BillingResult DoBilling(BillingData data)
      {
            Bill theBill = MakeBill(data);

            _BillingSystem.DoBilling();
      }
}

public interface IBillingSystem
{
      void DoBilling(Bill theBill);
}

public class BillingSystem : IBillingSystem
{
      public void DoBilling(Bill theBill)
      {
            using(BillingClient wc = new BillingClient())
            {
                 wc.DoBilling(theBill);
            }
      }
}
</pre>
<p>With that you can easily replace the untestable code with some kind of testdouble like a stub or a mock.</p>
<pre class="brush: csharp">
public class BillingServiceTest
{
     [Fact]
     public void DoBilling_ValidBill_BillIsCorrectlySendToBillingSystem()
     {
           BillingSystemStub billingSystem =  new BillingSystemStub();
           BillingService billingService = new BillingService(billingSystem);
           BillingData billingData = MakeValidBillingData();

           billingService.DoBilling(billingData);

           //Do some assertions
           Assert.NotNull(billingSystem.RecievedBill);
     }
}
</pre>
<p>Today I learned that this is also called POOR MANS DEPENDENCY INJECTION.</p>
<p>P.S.:<br />
Mabe the example is not the best one but I didn&#8217;t want to go into something like  Foo and Bar again. Besides that it&#8217;s only a simplified example. Consider that before throwing eggs and tomatos at me <img src='http://www.bjoernrochel.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>P.S.2:<br />
An alternative to this approach which doesn&#8217;t require a new interface is &#8220;Extract &amp; Override&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bjoernrochel.de/2008/08/29/poor-mans-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
