<?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: When to use Dependency Injection</title>
	<atom:link href="http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=when-to-use-dependency-injection</link>
	<description>Testability Explorer</description>
	<lastBuildDate>Thu, 19 Jan 2012 16:42:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Michal K</title>
		<link>http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/comment-page-1/#comment-8591</link>
		<dc:creator>Michal K</dc:creator>
		<pubDate>Thu, 02 Jun 2011 16:58:33 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=380#comment-8591</guid>
		<description>Hi,

I have a problem with DI and lazy initialization.
Supose we have a code:

class IdObject {
  Database db;
  Memcache  mc;
  int id;
  String data;

  IdObject(int id, Database db, Memcache mc) {
    this.db = db;
    this.mc = mc;
    this.id = id;
  }

  int load_data() { 
      this.data = mc.get(this.id);
      if (this.data === null) {
        this.data = db.get(this.id);
     }
  }
}

So using DI during creating IdObject we are creating both Database  and Memcache objects, before we use  load_data(), but moreover we create Database  object even we need it, bacause in most cases mc.get() return data differentfrom null. So we have Database  object and we have database connetction though we don&#039;t need it. 
I&#039;m PHP developer and after each script execution the connection is close. One of the solutions is connect to database when we use get() methods, but we must implement this also in others methods. Is the better solution to this problem?
Thanks, and sorry for my poor English.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I have a problem with DI and lazy initialization.<br />
Supose we have a code:</p>
<p>class IdObject {<br />
  Database db;<br />
  Memcache  mc;<br />
  int id;<br />
  String data;</p>
<p>  IdObject(int id, Database db, Memcache mc) {<br />
    this.db = db;<br />
    this.mc = mc;<br />
    this.id = id;<br />
  }</p>
<p>  int load_data() {<br />
      this.data = mc.get(this.id);<br />
      if (this.data === null) {<br />
        this.data = db.get(this.id);<br />
     }<br />
  }<br />
}</p>
<p>So using DI during creating IdObject we are creating both Database  and Memcache objects, before we use  load_data(), but moreover we create Database  object even we need it, bacause in most cases mc.get() return data differentfrom null. So we have Database  object and we have database connetction though we don&#8217;t need it.<br />
I&#8217;m PHP developer and after each script execution the connection is close. One of the solutions is connect to database when we use get() methods, but we must implement this also in others methods. Is the better solution to this problem?<br />
Thanks, and sorry for my poor English.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/comment-page-1/#comment-4586</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Wed, 27 Oct 2010 16:08:44 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=380#comment-4586</guid>
		<description>@Dileep,
see: http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/</description>
		<content:encoded><![CDATA[<p>@Dileep,<br />
see: <a href="http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/" rel="nofollow">http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dileep</title>
		<link>http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/comment-page-1/#comment-4581</link>
		<dc:creator>Dileep</dc:creator>
		<pubDate>Wed, 27 Oct 2010 06:44:23 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=380#comment-4581</guid>
		<description>Hi Misko,
Which and what type of Objects are Injectable ?</description>
		<content:encoded><![CDATA[<p>Hi Misko,<br />
Which and what type of Objects are Injectable ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Multi Tenant Architecture via Dependency Injection: Part 1</title>
		<link>http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/comment-page-1/#comment-3726</link>
		<dc:creator>Multi Tenant Architecture via Dependency Injection: Part 1</dc:creator>
		<pubDate>Sun, 11 Jul 2010 20:51:41 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=380#comment-3726</guid>
		<description>[...] using a dependency injection framework, which is not the scope of this essay but can be enumerated here, for instance. Our example will use Ninject and ASP.NET MVC, although you could use any DI and [...]</description>
		<content:encoded><![CDATA[<p>[...] using a dependency injection framework, which is not the scope of this essay but can be enumerated here, for instance. Our example will use Ninject and ASP.NET MVC, although you could use any DI and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Test Driven Development</title>
		<link>http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/comment-page-1/#comment-2351</link>
		<dc:creator>Test Driven Development</dc:creator>
		<pubDate>Tue, 17 Nov 2009 01:26:23 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=380#comment-2351</guid>
		<description>[...] links to his site and there I found excellent articles that changed the way I approach programming. When to use Dependency Injection, Guide to Writing Testable Code and Dependency Injection Myth: Reference Passing are some of the [...]</description>
		<content:encoded><![CDATA[<p>[...] links to his site and there I found excellent articles that changed the way I approach programming. When to use Dependency Injection, Guide to Writing Testable Code and Dependency Injection Myth: Reference Passing are some of the [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Soapbox Rants and Raves &#187; Good Reading&#8230;</title>
		<link>http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/comment-page-1/#comment-939</link>
		<dc:creator>Soapbox Rants and Raves &#187; Good Reading&#8230;</dc:creator>
		<pubDate>Sat, 02 May 2009 14:34:00 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=380#comment-939</guid>
		<description>[...] having to put them into words! Please check out his posts, topics range from testability, to dependency injection, to Changing Developer [...]</description>
		<content:encoded><![CDATA[<p>[...] having to put them into words! Please check out his posts, topics range from testability, to dependency injection, to Changing Developer [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 1up4Developers &#187; Blog Archive &#187; TPW - Testando sistemas legados: manipulando dependências</title>
		<link>http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/comment-page-1/#comment-699</link>
		<dc:creator>1up4Developers &#187; Blog Archive &#187; TPW - Testando sistemas legados: manipulando dependências</dc:creator>
		<pubDate>Thu, 19 Feb 2009 15:48:51 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=380#comment-699</guid>
		<description>[...] da classe são delegados à outro objeto, ou normalmente um container de objetos, responsável por injetar as dependências nas instâncias das classes. Simples, não?! Mas isso será detalhado em outro [...]</description>
		<content:encoded><![CDATA[<p>[...] da classe são delegados à outro objeto, ou normalmente um container de objetos, responsável por injetar as dependências nas instâncias das classes. Simples, não?! Mas isso será detalhado em outro [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oh, your comments...</title>
		<link>http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/comment-page-1/#comment-621</link>
		<dc:creator>Oh, your comments...</dc:creator>
		<pubDate>Sun, 25 Jan 2009 23:37:09 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=380#comment-621</guid>
		<description>You will argue about which DI framework is better but what about THE subject: to use or not to use framework?I will agree that DI is the way to go, but I am sure this applies to the smallest applications as well. Nothing keeps the right order of things in application better than DI.But manual DI produces so much boilerplate code, that the clear benefits of using it starts to vanish. Few weeks ago I decided to do a first step in making my application better, because it looks like a spaghetti in many places. This is a large Swing application client for EJB3 back-end. I was asking myself exactly the same question: to choose a framework or not. I decided not to pick up framework and do manual DI. After few days I had a feeling that something goes wrong. Then I created a little, tiny Swing application&#160; from scratch using (for the first time) Guice (excluding EJB3 - that was my first DI framework I dealt with in practice). When I finished it and moved back to my main job, the Swing client without DI framework, I felt like I there was missing something fundamental. One of the most brilliant features is the automatic creation of factories in Guice (called Providers, I am sure Spring has something simmilar). In Swing this is very common to ask for something new on every method call. Automatic creation of providers is so great. Without it, I would have to write like dozens, hundreds of dump factories and keep track of every single dependency, bacause factories need factories, those other factories propably need another factories. Terrible to keep the track manually.Another aspect is that without good framework there is always a tempation to create something using &quot;new&quot; in the bussiness code. You think - this class is a leaf, why shold I bother creating yet another factory for that. But when that leaf becomes a non-leaf, you end up with dozen places where you have to get rid of &quot;new&quot; and use your brand new, yet another factory... In Guice I am injecting everything, even if that is a leaf, because that costs me so little, just:@Injectprivate Provider&lt;Something&gt; somethingProvider;and later:Something = something.get();As soon as that &quot;Something&quot; becomes a non-leaf, everything still works. In my &quot;Something&quot; I can use injection and I do not care about proper wiring the stuff together. That was my $0.02 :)Regards,Witold SzczerbaP.S.Why everyone says: Spring does not need XML config, because since v2.5 it works with annotations.Maybe I;m wrong, but annotations are way too little to configure a module. In Guice, the counterpart Spring&#039;s XML files are the com.google.inject.Module classes (by implementing the interface directly or by extending AbstractModule). I find it impossible to configure DI framework using the annotations and nothing more.</description>
		<content:encoded><![CDATA[<p>You will argue about which DI framework is better but what about THE subject: to use or not to use framework?I will agree that DI is the way to go, but I am sure this applies to the smallest applications as well. Nothing keeps the right order of things in application better than DI.But manual DI produces so much boilerplate code, that the clear benefits of using it starts to vanish. Few weeks ago I decided to do a first step in making my application better, because it looks like a spaghetti in many places. This is a large Swing application client for EJB3 back-end. I was asking myself exactly the same question: to choose a framework or not. I decided not to pick up framework and do manual DI. After few days I had a feeling that something goes wrong. Then I created a little, tiny Swing application&nbsp; from scratch using (for the first time) Guice (excluding EJB3 &#8211; that was my first DI framework I dealt with in practice). When I finished it and moved back to my main job, the Swing client without DI framework, I felt like I there was missing something fundamental. One of the most brilliant features is the automatic creation of factories in Guice (called Providers, I am sure Spring has something simmilar). In Swing this is very common to ask for something new on every method call. Automatic creation of providers is so great. Without it, I would have to write like dozens, hundreds of dump factories and keep track of every single dependency, bacause factories need factories, those other factories propably need another factories. Terrible to keep the track manually.Another aspect is that without good framework there is always a tempation to create something using &#8220;new&#8221; in the bussiness code. You think &#8211; this class is a leaf, why shold I bother creating yet another factory for that. But when that leaf becomes a non-leaf, you end up with dozen places where you have to get rid of &#8220;new&#8221; and use your brand new, yet another factory&#8230; In Guice I am injecting everything, even if that is a leaf, because that costs me so little, just:@Injectprivate Provider&lt;Something&gt; somethingProvider;and later:Something = something.get();As soon as that &#8220;Something&#8221; becomes a non-leaf, everything still works. In my &#8220;Something&#8221; I can use injection and I do not care about proper wiring the stuff together. That was my $0.02 <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Regards,Witold SzczerbaP.S.Why everyone says: Spring does not need XML config, because since v2.5 it works with annotations.Maybe I;m wrong, but annotations are way too little to configure a module. In Guice, the counterpart Spring&#8217;s XML files are the com.google.inject.Module classes (by implementing the interface directly or by extending AbstractModule). I find it impossible to configure DI framework using the annotations and nothing more.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/comment-page-1/#comment-606</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Thu, 22 Jan 2009 00:23:22 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=380#comment-606</guid>
		<description>Chris,I would like to know why you find Guice&#039;s DI much nicer to use than Spring&#039;s?&#160; I hope you are comparing apples-to-apples and are comparing the use of spring annotations vs. Guice annotations because I would like to know how the annotation implementations differ.</description>
		<content:encoded><![CDATA[<p>Chris,I would like to know why you find Guice&#8217;s DI much nicer to use than Spring&#8217;s?&nbsp; I hope you are comparing apples-to-apples and are comparing the use of spring annotations vs. Guice annotations because I would like to know how the annotation implementations differ.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://misko.hevery.com/2009/01/14/when-to-use-dependency-injection/comment-page-1/#comment-605</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Thu, 22 Jan 2009 00:20:33 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=380#comment-605</guid>
		<description>Misko,I would like see a post on why you favor constructor injection over setter injection.&#160; This is a common question that comes up when developers are first introduced to DI.</description>
		<content:encoded><![CDATA[<p>Misko,I would like see a post on why you favor constructor injection over setter injection.&nbsp; This is a common question that comes up when developers are first introduced to DI.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

