<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Setter injection sucks</title>
	<atom:link href="http://evan.bottch.com/2009/02/03/setter-injection-sucks/feed/" rel="self" type="application/rss+xml" />
	<link>http://evan.bottch.com/2009/02/03/setter-injection-sucks/</link>
	<description>// TODO: think of a witty and intelligent tagline</description>
	<lastBuildDate>Tue, 31 Aug 2010 07:06:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Nicholas Sterling</title>
		<link>http://evan.bottch.com/2009/02/03/setter-injection-sucks/comment-page-1/#comment-921</link>
		<dc:creator>Nicholas Sterling</dc:creator>
		<pubDate>Thu, 10 Jun 2010 07:59:40 +0000</pubDate>
		<guid isPermaLink="false">http://evan.bottch.com/?p=60#comment-921</guid>
		<description>The desire for names is understandable, but the fault is not in the concept of constructor injection but rather in the Java language (I assume you&#039;re talking about Java) itself.  If you use Scala, then one of the myriad benefits you get is the ability to pass parameters by name when you so desire.  So now you can write nice, tight code to invoke constructors and be sure that all of the dependencies are being satisfied, while still seeing the name of every dependency.</description>
		<content:encoded><![CDATA[<p>The desire for names is understandable, but the fault is not in the concept of constructor injection but rather in the Java language (I assume you&#8217;re talking about Java) itself.  If you use Scala, then one of the myriad benefits you get is the ability to pass parameters by name when you so desire.  So now you can write nice, tight code to invoke constructors and be sure that all of the dependencies are being satisfied, while still seeing the name of every dependency.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nat Harward</title>
		<link>http://evan.bottch.com/2009/02/03/setter-injection-sucks/comment-page-1/#comment-831</link>
		<dc:creator>Nat Harward</dc:creator>
		<pubDate>Sat, 10 Oct 2009 20:01:33 +0000</pubDate>
		<guid isPermaLink="false">http://evan.bottch.com/?p=60#comment-831</guid>
		<description>Completely agree in general.

&lt;blockquote cite=&quot;Ben Hutchinson&quot;&gt;
3. It must bind against an exact concrete type. You cant substitute a subclass. 
&lt;/blockquote&gt;

It seems (to me, anyway) that a more appropriate pattern to use for situations where you are likely to have different implementations is the factory pattern.  When I run my code in a debugger to find an issue I can actually see what&#039;s happening and what sub-class my code is actually using (and why).  When I get a NPE from bad configuration (assuming the factory is configurable) I actually know immediately where to go look.  No such luck with Spring :(

&lt;blockquote cite=&quot;Tomas Varsavsky&quot;&gt;
I try to use constructor injection for dependencies that the class cannot function without and setter injection for optional dependencies that the class can do without.
&lt;/blockquote&gt;

Good point.  Seems like a reasonable way to help bridge the differences between the two approaches, but keep code (and particularly intent) sane and understandable.</description>
		<content:encoded><![CDATA[<p>Completely agree in general.</p>
<blockquote cite="Ben Hutchinson"><p>
3. It must bind against an exact concrete type. You cant substitute a subclass.
</p></blockquote>
<p>It seems (to me, anyway) that a more appropriate pattern to use for situations where you are likely to have different implementations is the factory pattern.  When I run my code in a debugger to find an issue I can actually see what&#8217;s happening and what sub-class my code is actually using (and why).  When I get a NPE from bad configuration (assuming the factory is configurable) I actually know immediately where to go look.  No such luck with Spring <img src='http://evan.bottch.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<blockquote cite="Tomas Varsavsky"><p>
I try to use constructor injection for dependencies that the class cannot function without and setter injection for optional dependencies that the class can do without.
</p></blockquote>
<p>Good point.  Seems like a reasonable way to help bridge the differences between the two approaches, but keep code (and particularly intent) sane and understandable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Mansour</title>
		<link>http://evan.bottch.com/2009/02/03/setter-injection-sucks/comment-page-1/#comment-821</link>
		<dc:creator>Mark Mansour</dc:creator>
		<pubDate>Mon, 30 Mar 2009 02:38:50 +0000</pubDate>
		<guid isPermaLink="false">http://evan.bottch.com/?p=60#comment-821</guid>
		<description>I couldn&#039;t agree with you more.  Constructors put the object in the correct state at the time of construction.  If you need to use setters before you&#039;ve used the object then you should be beefing up your constructors.</description>
		<content:encoded><![CDATA[<p>I couldn&#8217;t agree with you more.  Constructors put the object in the correct state at the time of construction.  If you need to use setters before you&#8217;ve used the object then you should be beefing up your constructors.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Romilly Cocking</title>
		<link>http://evan.bottch.com/2009/02/03/setter-injection-sucks/comment-page-1/#comment-805</link>
		<dc:creator>Romilly Cocking</dc:creator>
		<pubDate>Sat, 28 Feb 2009 15:50:55 +0000</pubDate>
		<guid isPermaLink="false">http://evan.bottch.com/?p=60#comment-805</guid>
		<description>I agree that setter injection is undesirable. It encourages the creation of objects in invalid states, and it often hides intent.

Don&#039;t worry about your preference for configuration in code as opposed to XML. Not everyone will agree with you, but lots of smart people do.

When they want to create objects, they use constructors.  That&#039;s what constructors are for.</description>
		<content:encoded><![CDATA[<p>I agree that setter injection is undesirable. It encourages the creation of objects in invalid states, and it often hides intent.</p>
<p>Don&#8217;t worry about your preference for configuration in code as opposed to XML. Not everyone will agree with you, but lots of smart people do.</p>
<p>When they want to create objects, they use constructors.  That&#8217;s what constructors are for.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Hutchison</title>
		<link>http://evan.bottch.com/2009/02/03/setter-injection-sucks/comment-page-1/#comment-793</link>
		<dc:creator>Ben Hutchison</dc:creator>
		<pubDate>Thu, 19 Feb 2009 04:09:22 +0000</pubDate>
		<guid isPermaLink="false">http://evan.bottch.com/?p=60#comment-793</guid>
		<description>I agree that its better to make an object&#039;s instantiation and configuration appear atomic to clients.

But c&#039;tor injection has problems too: 

1. unless you compile with debug symbols on, the names of the injected params get erased to types, so you can&#039;t inject by name. I wish that would change, maybe it has already in practice, cause everyone leaves debug on nowadays.

2. Sometimes its easier to break up _instatiation_ from _configuration_ of an object, because different pieces do each job. 

AFAIK, thats how Spring 2.x does DI into domain objects created by foreign frameworks (eg hibernate). It uses an aspect that intercepts after the &#039;new&#039; operation, to apply config via setter injection. Its seems a bit more invasive to completely replace the &#039;new&#039; call using AOP.

3. It must bind against an exact concrete type. You cant substitute a subclass. 

PS Got a broken link on your main page, the &quot;more&quot; link on this blog post.

PPS Re: &quot;some new amazing functional language&quot;: Scala absolutely and utterly rocks. Thoughtworks seems conspicuously quiet in the  Scala community right now. Come on board!!</description>
		<content:encoded><![CDATA[<p>I agree that its better to make an object&#8217;s instantiation and configuration appear atomic to clients.</p>
<p>But c&#8217;tor injection has problems too: </p>
<p>1. unless you compile with debug symbols on, the names of the injected params get erased to types, so you can&#8217;t inject by name. I wish that would change, maybe it has already in practice, cause everyone leaves debug on nowadays.</p>
<p>2. Sometimes its easier to break up _instatiation_ from _configuration_ of an object, because different pieces do each job. </p>
<p>AFAIK, thats how Spring 2.x does DI into domain objects created by foreign frameworks (eg hibernate). It uses an aspect that intercepts after the &#8216;new&#8217; operation, to apply config via setter injection. Its seems a bit more invasive to completely replace the &#8216;new&#8217; call using AOP.</p>
<p>3. It must bind against an exact concrete type. You cant substitute a subclass. </p>
<p>PS Got a broken link on your main page, the &#8220;more&#8221; link on this blog post.</p>
<p>PPS Re: &#8220;some new amazing functional language&#8221;: Scala absolutely and utterly rocks. Thoughtworks seems conspicuously quiet in the  Scala community right now. Come on board!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Johnston</title>
		<link>http://evan.bottch.com/2009/02/03/setter-injection-sucks/comment-page-1/#comment-790</link>
		<dc:creator>Chris Johnston</dc:creator>
		<pubDate>Sun, 08 Feb 2009 12:54:31 +0000</pubDate>
		<guid isPermaLink="false">http://evan.bottch.com/?p=60#comment-790</guid>
		<description>Yep, totally agree with your complaints about setter injection. I hadn&#039;t thought about the code smell angle from constructor injection before, but it is definitely something I will keep in the back of my mind from now on.

I started out doing setter injection, but when I finally realized that objects should have everything they need in order to work before you can use them, I never looked back. Constructor injection all the way. Now if I could just get my team from defining every single object in our code base in or applicationContext.xml files. grumble, grumble, grumble.</description>
		<content:encoded><![CDATA[<p>Yep, totally agree with your complaints about setter injection. I hadn&#8217;t thought about the code smell angle from constructor injection before, but it is definitely something I will keep in the back of my mind from now on.</p>
<p>I started out doing setter injection, but when I finally realized that objects should have everything they need in order to work before you can use them, I never looked back. Constructor injection all the way. Now if I could just get my team from defining every single object in our code base in or applicationContext.xml files. grumble, grumble, grumble.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tomas Varsavsky</title>
		<link>http://evan.bottch.com/2009/02/03/setter-injection-sucks/comment-page-1/#comment-789</link>
		<dc:creator>Tomas Varsavsky</dc:creator>
		<pubDate>Thu, 05 Feb 2009 11:20:21 +0000</pubDate>
		<guid isPermaLink="false">http://evan.bottch.com/?p=60#comment-789</guid>
		<description>I try to use constructor injection for dependencies that the class cannot function without and setter injection for optional dependencies that the class can do without.</description>
		<content:encoded><![CDATA[<p>I try to use constructor injection for dependencies that the class cannot function without and setter injection for optional dependencies that the class can do without.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sud</title>
		<link>http://evan.bottch.com/2009/02/03/setter-injection-sucks/comment-page-1/#comment-788</link>
		<dc:creator>sud</dc:creator>
		<pubDate>Thu, 05 Feb 2009 01:48:37 +0000</pubDate>
		<guid isPermaLink="false">http://evan.bottch.com/?p=60#comment-788</guid>
		<description>If you use Spring for DI and your Unit Test class extends SpringTestCase, you should be able to have spring inject those dependencies for you from an applicationContext.xml file.</description>
		<content:encoded><![CDATA[<p>If you use Spring for DI and your Unit Test class extends SpringTestCase, you should be able to have spring inject those dependencies for you from an applicationContext.xml file.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim McMaster</title>
		<link>http://evan.bottch.com/2009/02/03/setter-injection-sucks/comment-page-1/#comment-787</link>
		<dc:creator>Jim McMaster</dc:creator>
		<pubDate>Thu, 05 Feb 2009 01:43:20 +0000</pubDate>
		<guid isPermaLink="false">http://evan.bottch.com/?p=60#comment-787</guid>
		<description>Yes!  I agree completely.  When people complain about too many constructor arguments&#039; making the code ugly, I tell them Dependency Injection doesn&#039;t &lt;i&gt;cause&lt;/i&gt; ugliness, it only &lt;i&gt;exposes&lt;/i&gt; it.  The class already had all these dependencies, they were just hidden.

Setter injection is evil.</description>
		<content:encoded><![CDATA[<p>Yes!  I agree completely.  When people complain about too many constructor arguments&#8217; making the code ugly, I tell them Dependency Injection doesn&#8217;t <i>cause</i> ugliness, it only <i>exposes</i> it.  The class already had all these dependencies, they were just hidden.</p>
<p>Setter injection is evil.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Beans</title>
		<link>http://evan.bottch.com/2009/02/03/setter-injection-sucks/comment-page-1/#comment-786</link>
		<dc:creator>Beans</dc:creator>
		<pubDate>Wed, 04 Feb 2009 10:27:23 +0000</pubDate>
		<guid isPermaLink="false">http://evan.bottch.com/?p=60#comment-786</guid>
		<description>&lt;blockquote&gt;A big reason I prefer constructor injection is that I can ensure that when an object is constructed is ready to do it’s job.&lt;/blockquote&gt;

How do you feel about ctors throwing exceptions?

</description>
		<content:encoded><![CDATA[<blockquote><p>A big reason I prefer constructor injection is that I can ensure that when an object is constructed is ready to do it’s job.</p></blockquote>
<p>How do you feel about ctors throwing exceptions?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
