tag:blogger.com,1999:blog-3237724005744642470.post4945945726956638582..comments2020-01-08T11:33:04.583+00:00Comments on Captain Debug's Blog: The Benefits Of Dependency InjectionRoger Hugheshttp://www.blogger.com/profile/07042290171112551665[email protected]Blogger8125tag:blogger.com,1999:blog-3237724005744642470.post-61247959786282095042011-10-19T21:23:02.314+01:002011-10-19T21:23:02.314+01:00A link to this simple, seemingly uncontentious blo...A link to this simple, seemingly uncontentious blog was posted on <a href="http://www.reddit.com/r/programming/comments/lf1px/are_there_any_benefits_in_dependency_injection/" rel="nofollow">Reddit</a> a few days ago. This generated about 6000 page hits and over 200 comments. It just goes to show that there are as many opinions as programmers; however, the the consensus, with which I agree, seems to be in favour of DI, although I&#39;m promising myself that I&#39;ll get around to checking out OSGi - one day.Roger Hugheshttps://www.blogger.com/profile/07042290171112551665[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-20122909517007719102011-10-19T21:10:16.275+01:002011-10-19T21:10:16.275+01:00Wow, a lot of hate for DI here! I&#39;ve worked...Wow, a lot of hate for DI here! <br /><br />I&#39;ve worked on several projects that wrote their own object configuration factory based on XML and/or annotations (and one I wrote myself). This was in pre-Spring/CDI days, and they served their purpose well. It isn&#39;t that hard... at its heart Core Spring doesn&#39;t do anything particularly clever; IMHO the real value of Spring is in its vast ecosystem. That to one side, I do think it&#39;s pretty hard to argue CoreSpring/DI/IoC doesn&#39;t bring <i>any</i> benefits over using new Impl(), even if you don&#39;t think they out-weigh the trade-offs (the indirection, arguably greater complexity and the everything-is-a-singleton trap). DI/Spring makes it really easy to wire your app in different ways to support different configurations, which amoungst other things, does have obvious benefits for testability as Roger pointed out. Accepted that&#39;s more important to some than others.<br /><br />As an aside: if peeps have worked on sucky projects that use Spring, don&#39;t blame Spring or the DI pattern, blame the architect or the developers. Bad workmen, tools and all that.<br /><br />What I really want to say though is: don&#39;t assume DI/IoC is the end game for achieving proper modularity and all the benefits that brings. You&#39;re still coupling interfaces with their implementations, you&#39;re just doing it within an XML file or annotations rather than via new Impl(). For that you need a broker, and in this space OSGi is still pretty much the only game in town. <a href="https://oracleus.wingateweb.com/published/oracleus2011/sessions/24805/S24805_2554740.pdf" rel="nofollow">Services-First Migration to OSGi</a> was a good passionate talk on this at JavaONE. There were several others.Nathan Dolanhttps://www.blogger.com/profile/18142434669988468721[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-32963418265513253172011-10-18T17:39:15.760+01:002011-10-18T17:39:15.760+01:00I used Spring for about 3 years. DI and Spring _ca...I used Spring for about 3 years. DI and Spring _can_ be useful for testing, I agree. <br /><br />Spring as an implementation is far from optimal. I think this is more an artifact of DI/AOP/etc not being natively supported by Java.<br /><br />I&#39;ve spent countless hours trying to figure out the xml incantations to get things to work as expected.Anonymous[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-74554693037673851262011-10-18T13:42:46.312+01:002011-10-18T13:42:46.312+01:00Santiago, You do not need a DI framework to do DI...Santiago,<br /><br />You do not need a DI framework to do DI. Your use of factories is still DI. DI is just injecting dependencies into a class rather than the class instantiating its own dependencies.<br /><br />&quot;The idea of communicating objects through public interfaces and exchanging backing implementations is older than DI, and perfectly plausible without it.&quot;<br /><br />That is DI. Just because you don&#39;t use an IoC container like Spring doesn&#39;t mean you are not doing DI.Bill Wixonhttps://www.blogger.com/profile/12772898276320185407[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-82762941973150025062011-10-18T11:26:08.137+01:002011-10-18T11:26:08.137+01:00I come from PHP and Zend Framework where they use ...I come from PHP and Zend Framework where they use DI to good effect, being able to plug and play aspects of a framework is nice (config files: PHP array, Yaml, Xml, Ini) etc is nice... DI seems to depend on what level you are in the software stack, if you are the glue level then it has it&#39;s placeAnonymous[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-62029460879837291122011-10-18T09:56:43.439+01:002011-10-18T09:56:43.439+01:00There are no benefits to DI. There are no design ...There are no benefits to DI.<br /><br />There are no design advantages to using DI.<br /><br />The belief that &quot;it is ok to partially construct objects&quot; and spring will sort it out later on is complete bullshit.<br /><br />This allows programmers (especially the weaker ones) to write code without first thinking, which ultimately ends up in project disaster.<br /><br />Don&#39;t use DI.<br /><br />You will find that without DI you will be writing less tests, and therefore, concentrating on getting your job done rather than noncing on about agile practices.Anonymous[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-63656355166860219192011-10-17T16:20:11.656+01:002011-10-17T16:20:11.656+01:00I&#39;m with Anonymous here, I never saw real bene...I&#39;m with Anonymous here, I never saw real benefits to DI.<br /><br />The idea of communicating objects through public interfaces and exchanging backing implementations is older than DI, and perfectly plausible without it.<br /><br />The amount of code you write is roughly the same, or more for DI frameworks that need heavy configuration, only now the actual logic that drives instantiation is hidden from sight. I&#39;d much rather have a plain-old factory for the classes that I actually plan on interchanging, and put that logic where it belongs -- right were the next programmer to read my code will need it to know what&#39;s happening.<br /><br />Yeah, DI feels automatic and magical, but magical also means mysterious, and I don&#39;t like my code being mysterious.Santiago Lezicahttp://blogactivity.wordpress.com/[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-55097973535180276172011-10-14T11:00:16.296+01:002011-10-14T11:00:16.296+01:00DI copes the wiring of static dependencies. And pr...DI copes the wiring of static dependencies. And preferrably it does this via Constructor Injection.<br /><br />This brings in two issues: First devs regullary mix dynamic dependencies (i.e. sth. like a Tx) with static dependencies (Statless Service A depending on a stateless Service B).And the other thing is: Is it really so beneficially to get some services injected vs. retrieving those frome some service provider?<br /><br />I don&#39;t think you write less boilercode with DI. I&#39;d rather say, unless you fragment you solution into hundreds of individual &amp; stateless services, you even write more boilercode and esp. much more complexity compared to a simple ServiceFactory which simply readss: ServiceProvider.get(ServiceInterface.class).doServiceA().<br /><br />The last pro-DI resort would: &quot;It helps desigining better software&quot;. Frankly meanwhile I do think: None at all!<br /><br />Good software means loose coupling via interfaces. Isolatable units which can be unit tested. Clear, simple and eady design and architecture contracts. I&#39;ve seen to much really grave design error provoced by the developers idea that DI will &quot;magically&quot; know and do the right thing and rather places where I&#39;d the impression DI helped to achieve a better software design.Anonymous[email protected]