<?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>Schauderhaft &#187; dependency injection</title>
	<atom:link href="http://blog.schauderhaft.de/tag/dependency-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schauderhaft.de</link>
	<description>Softwaredevelopment, Projectmanagement, Qualitymanagement and all things &#34;schauderhaft&#34;</description>
	<lastBuildDate>Sun, 05 Feb 2012 20:46:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The One Correct Way to do Dependency Injection</title>
		<link>http://blog.schauderhaft.de/2012/01/01/the-one-correct-way-to-do-dependency-injection/</link>
		<comments>http://blog.schauderhaft.de/2012/01/01/the-one-correct-way-to-do-dependency-injection/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 05:17:02 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[dependency injection]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=996</guid>
		<description><![CDATA[A couple of weeks ago a coworker told me that they have a little utility in their projects in order to set private fields for tests. He kind of claimed they needed that since they are using Spring, which in production sets the dependency. I was a little confused. But after looking into it I [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago a coworker told me that they have a little utility in their projects in order to set private fields for tests. He kind of claimed they needed that since they are using Spring, which in production sets the dependency. I was a little confused. But after looking into it I realized that there is an (anti)pattern going on that needs some fighting against. Lets have a look at the pattern.</p>
<p>Spring (and other DI frameworks) can inject dependencies into private fields. It looks like people like to use this because it combines some compelling properties:</p>
<ul>
<li>Very little boilerplate code. All you need is a simple annotation on the field.</li>
<li>Since the field is private nobody outside the class can change it.</li>
</ul>
<p>But now when you want to test your class and want to assign e.g. a mock to the field you have to either setup a Spring context in your tests or use reflection in order to access the field (just as Spring does).</p>
<p>Now something is really starting to smell bad here. One of the purposes of Dependency Injection is to decouple code, which in turn should make the code easier to test. Now that didn&#8217;t work out to well.</p>
<p>So how can we clean up this mess? We can write a setter for the field. That solves the testing problem. It adds a little boiler plate code, but hey we are talking Java here, so you should be used to that. But now we have a setter which anybody can call at any time. At the very best that doesn&#8217;t make any sense at all in the production environment. In the worst case somebody actually uses it and creates some ugly bug.</p>
<p>So what do you do when you don&#8217;t want anybody to change a field? Correct you make it final. Great, now you have a compile time error in the setter.You can only set a final field in a constructor. This leads naturally to the solution of all our problems (and possibly to world peace): <strong>Make your dependency a constructor argument!</strong></p>
<p>This is the only place where dependencies really belong. If you try to stick to that rule I&#8217;d expect you&#8217;ll encounter the following two problems.</p>
<ol>
<li>You&#8217;ll find classes with lots and lots of constructor arguments. I&#8217;ll bet that class does a lot of different things and really needs to get broken down into smaller pieces.</li>
<li>You&#8217;ll find cases where a class A needs an instance of B and B needs an instance of  A. This is a typical case of a circular dependency and is obviously bad. In my experience the solution is either to make B a part of A when the two are so strongly dependent that they really should be one class. More often though there is at least one more class C hiding in there so that B doesn&#8217;t need A but only C.</li>
</ol>
<p>So really the problems you&#8217;ll find when doing constructor based dependency injection are problems that are present in your current code already. So don&#8217;t shoot the messenger, but fix the problems in your code and go for constructor arguments for dependency injection.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2012/01/01/the-one-correct-way-to-do-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Breaking Dependency Cycles</title>
		<link>http://blog.schauderhaft.de/2011/07/17/breaking-dependency-cylces/</link>
		<comments>http://blog.schauderhaft.de/2011/07/17/breaking-dependency-cylces/#comments</comments>
		<pubDate>Sun, 17 Jul 2011 17:04:30 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=496</guid>
		<description><![CDATA[If you look into rules for good software design you&#8217;ll probably come around the notion that dependency circles(1) are bad. From my interview experience I&#8217;d say that many of the more experienced developers know that rule. And almost everybody agrees to that rule when asked. (Of course that might be due to the way I [...]]]></description>
			<content:encoded><![CDATA[<p>If you look into rules for good software design you&#8217;ll probably come around the notion that dependency circles(1) are bad. From my interview experience I&#8217;d say that many of the more experienced developers know that rule. And almost everybody agrees to that rule when asked. (Of course that might be due to the way I phrase the question. Some people say I can be pretty &#8230; convincing).</p>
<p>Anyway the logical next question is: How do you break circular dependencies? I find it a little disturbing that few developers can answer this question. So I&#8217;ll try to fix that.</p>
<p>Imagine two classes(2), forming a dependency circle. A depends on B and B depends on A. How do you break that circle, without changing the functionality? To be more precise: we are talking about the compile/deploy time dependency. At runtime the two classes need each others, so there actually will be a circular dependency.</p>
<p><a href="http://blog.schauderhaft.de/wp-content/uploads/2011/07/dependency1.jpg"><img class="alignnone size-full wp-image-849" title="dependency1" src="http://blog.schauderhaft.de/wp-content/uploads/2011/07/dependency1.jpg" alt="" width="442" height="300" /></a></p>
<p>Got it? It&#8217;s actually not that difficult: Extract all functionality of B into an interface C. Let A depend on C, B implements C and still depends on A. Circle broken.</p>
<p><a href="http://blog.schauderhaft.de/wp-content/uploads/2011/07/dependency21.jpg"><img class="alignnone size-full wp-image-850" title="Dependencies, No Circles" src="http://blog.schauderhaft.de/wp-content/uploads/2011/07/dependency21.jpg" alt="" width="413" height="334" /></a></p>
<p>Actually there is a piece missing. As said, at runtime we do need the circular dependency. At runtime A needs to reference an actual B. So you need a third class D which knows A and B and plugs the B into the A (using the C shaped hole).</p>
<p><a href="http://blog.schauderhaft.de/wp-content/uploads/2011/07/dependency_3.jpg"><img class="alignnone size-full wp-image-851" title="Dependencies, No circles, with setup class" src="http://blog.schauderhaft.de/wp-content/uploads/2011/07/dependency_3.jpg" alt="" width="441" height="402" /></a></p>
<p>Problem solved.</p>
<p>Actually this has a well known name: Dependency Injection. It should be quite easily doable without any framework in your main language.</p>
<p>Note(1): Larger circles are worse than smaller ones. Circles between classes of a single package are kind of ok. Circles between packages are pretty ugly. Circles between Jars are a no go and actually will break many tool chains.</p>
<p>Note(2): If you prefer you can replace the term &#8216;class&#8217; with package or jar. In that sense a package implements another one, when it implements interfaces in that package.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2011/07/17/breaking-dependency-cylces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Project&#8217;s Resolution</title>
		<link>http://blog.schauderhaft.de/2010/01/10/new-projects-resolution/</link>
		<comments>http://blog.schauderhaft.de/2010/01/10/new-projects-resolution/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 10:41:07 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[resolution]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[toxicity]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=372</guid>
		<description><![CDATA[In a couple of days I&#8217;ll start working on a new project. Actually it is an project that I worked one or two years ago. I think I did decent job last time. But there is always lots of room for improvement. So today I want to list a couple of things I want to [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_374" class="wp-caption alignleft" style="width: 310px"><a href="http://www.sxc.hu/photo/1216962"><img class="size-medium wp-image-374" title="Fireworks" src="http://blog.schauderhaft.de/wp-content/uploads/2010/01/1216962_13713831-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Fireworks</p></div>
<p>In a couple of days I&#8217;ll start working on a new project. Actually it is an project that I worked one or two years ago. I think I did decent job last time. But there is always lots of room for improvement. So today I want to list a couple of things I want to do bettern than the last time.</p>
<p><strong>More Tests</strong> &#8211; When working on that project I was the only person with real experience in automated testing. I am proud that we had a test suite and that this test suite still exists and put to good use and actually grew during the last months. Yet I consider the coverage of the tests not sufficient by far. So for every code I write, I will add an extensive set of test. I&#8217;ll also add tests for existing code, when refactoring.</p>
<p><strong>Earlier Tests</strong> &#8211; One reason we didn&#8217;t produce as many tests as I would wish today is that we didn&#8217;t do TDD. It probably was a correct decission the last time, since it would have been just to much. But this time I will practice TDD as good as I can.</p>
<p><strong>Cleaner Code</strong> &#8211; One of the first tests I wrote back then was a test to ensure that we don&#8217;t have circular dependencies. We succeeded in this, but still parts of the system a pretty tangled. We gonna improve that. Appart from the knowledge I took from <a href="http://www.amazon.com/gp/product/0132350882?ie=UTF8&amp;tag=schauderhaft-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132350882">Clean Code</a> I&#8217;ll plan to use the <a href="http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/">toxicity metric</a> as an important tool for that.</p>
<p><strong>Dependency Injection</strong> &#8211; We considered using Spring and decided against it, because we already had about 20 libraries we hardly knew in the project. Again at that time it was a reasonable decision. But in the meantime I learned what we could have gained by using Spring. I&#8217;ll propose using it, but even when the team declines again, I&#8217;ll be able to use the concept in order to improve the code base.</p>
<p>So these are the things I want to improve on in the next project. What are the things you want to improve on in your next project?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/01/10/new-projects-resolution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

