Testing Blog

When to use Dependency Injection

tirsdag, januar 20, 2009
Share on Twitter Share on Facebook
Google
Etiketter: Misko Hevery

13 kommentarer :

  1. Tim O'Brien20. januar 2009 kl. 22.08.00 GMT-8

    Don't forget Plexus. I think more people are unwittingly using Plexus every day than something like Pico, it is at the core of Maven.

    SvarSlet
    Svar
      Svar
  2. Hamlet D'Arcy21. januar 2009 kl. 07.50.00 GMT-8

    I'll defend Spring here...

    Spring has a very good deployment package and you can easily only include the DI jars and not include all the others. Don't use what you don't need.

    Spring supports annotations, in fact I would have said it is the recommended configuration choice. There is no data to support the claim though.

    Spring supports constructor injection, and again I would have said it is the recommended approach. No data though.

    Spring also has great support for autowiring JUnit tests. Cool! Check it out: http://is.gd/gIcB

    SvarSlet
    Svar
      Svar
  3. David Linsin22. januar 2009 kl. 03.32.00 GMT-8

    I agree with Hamelt here. I'm using Spring every day and I only use it with constructor injection.

    However, if you are referring to the Spring Documentation, you are indeed right.

    The Spring team generally advocates the usage of setter injection, since a large number of constructor arguments can get unwieldy, especially when some properties are optional.

    SvarSlet
    Svar
      Svar
  4. Blair Zajac23. januar 2009 kl. 11.23.00 GMT-8

    Spring has a JavaConfig that is in beta which allows one to use DI in Java (or Scala code for that matter) without using any XML.

    I understand it is possible to mix JavaConfig with XML, but I haven't done that on any of my projects.

    SvarSlet
    Svar
      Svar
  5. Gavin Bong26. januar 2009 kl. 20.21.00 GMT-8

    One of the problems I face with Guice is the fact that a Provider only provides a #get() method. Is there a feature to pass in parameters to a variant of the get method like AssistedInject ? Does Spring have a comparable thing to Providers ?

    SvarSlet
    Svar
      Svar
  6. Dhanji R. Prasanna27. januar 2009 kl. 20.29.00 GMT-8

    @andorra Yes Spring has "Factory" interfaces, which are the same thing, but they are not available by default and are not type safe last I checked.

    Spring also has an assisted inject equivalent with Factory methods (and interfaces), which uses method decoration. Again, it is a bit clunky but it's there.

    @Hamlet
    Annotations != type safety
    JavaConfig is also not type safe, like Guice's binder API.

    As for other features, Guice has much more compelling servlet and JPA stories, which are 90% of use cases in the kitchen sink.

    Disclaimer: I am a Guice team member.

    Dhanji.

    SvarSlet
    Svar
      Svar
  7. Blair Zajac27. januar 2009 kl. 21.46.00 GMT-8

    @Dhanji

    Can you explain more the statement "JavaConfig is not ype safe". With JavaConfig, everything is done in Java code so it checks all the types.

    SvarSlet
    Svar
      Svar
  8. Dhanji R. Prasanna27. januar 2009 kl. 22.07.00 GMT-8

    @Blair

    Oops, I meant to say JavaConfig *AOP* is also not typesafe.

    Dhanji. =)

    SvarSlet
    Svar
      Svar
  9. DEPRECATEDpetersson27. januar 2009 kl. 23.36.00 GMT-8

    i am using spring in my dayjob and guice in my pet projects, both for about half a year.

    both have increased my productivity drastically.

    spring seems to have an advantage at the business side - as a manager, you will recruit a spring expert easier than a guice expert.

    on the other hand, it seems much easier to mess up the spring configuration, than the guice config. @Inject apeared more intuitive to my coworkers on the pet project than using the external config.

    what bothers me in both cases: there seems to be a tendency to view the injector/webappcontext as a super-factory.

    so i've seen the approach that should be used as a last resort more often than i should:

    StaticUtils.injectorFromservletContext(). getInstance( SomeDao.class );
    SpringUtils.getBean("someDao");

    in 99% of the cases, this is wrong, since you could simply inject the right class using @Inject or as a < property >. effectively you transform your class to a top-level class by abusing the DI container this way.

    SvarSlet
    Svar
      Svar
  10. Chad Woolley10. februar 2009 kl. 23.22.00 GMT-8

    Use Ruby and you won't need a DI framework:

    * http://rubyconf.org/talks/18
    * http://rubyconf2008.confreaks.com/recovering-from-enterprise.html

    ...although I disagree with Jamis that DI is completely unnecessary (you can hear me on the video) - DI is still a valuable architecture, even in Ruby. Like testing, it generally leads programmers to do Good Things - such as loose coupling and high cohesion.

    Unfortunately, most Ruby frameworks (such as Rails) are architected in a way that pretty much precludes the widespread or high-level use of DI in your app.

    However, if you are not constrained by a framework, DI is an excellent way to structure your app. Start with a simple Registry pattern which builds your App and take it from there (like this: http://geminstaller.rubyforge.org/code/coverage/lib-geminstaller-registry_rb.html). Simple, maybe ugly, but the result of pure YAGNI TDD and completely functional (btw, this started out with Needle but I scrapped it because it was my only external dependency).

    -- Chad

    SvarSlet
    Svar
      Svar
  11. iSilver Labs12. oktober 2010 kl. 11.06.00 GMT-7

    We just developed a open source Light Weight Ioc / Dependency Injection framework. Can you have a look?

    http://blog.isilverlabs.com/2010/08/lightweight-ioc-dependency-injection-framework/

    SvarSlet
    Svar
      Svar
  12. Nikita Naik31. august 2011 kl. 21.28.00 GMT-7

    i am planning make my project on inversion of control/dependency injection.can anybody please suggest me from where actually my project should start????? reply as soon as possible...i need your help because i have understood the concept but unable to implement.

    SvarSlet
    Svar
      Svar
  13. Nikita Naik31. august 2011 kl. 21.32.00 GMT-7

    please let me know how to make framework using dependency injection??? and how to implement the concept of inversion control

    SvarSlet
    Svar
      Svar
Tilføj kommentar
Indlæs flere...

The comments you read and contribute here belong only to the person who posted them. We reserve the right to remove off-topic comments.

  

Labels


  • TotT 108
  • GTAC 61
  • James Whittaker 42
  • Misko Hevery 32
  • Code Health 31
  • Anthony Vallone 27
  • Patrick Copeland 23
  • Jobs 18
  • Andrew Trenk 13
  • C++ 11
  • Patrik Höglund 8
  • JavaScript 7
  • Allen Hutchison 6
  • George Pirocanac 6
  • Zhanyong Wan 6
  • Harry Robinson 5
  • Java 5
  • Julian Harty 5
  • Adam Bender 4
  • Alberto Savoia 4
  • Ben Yu 4
  • Erik Kuefler 4
  • Philip Zembrod 4
  • Shyam Seshadri 4
  • Chrome 3
  • Dillon Bly 3
  • John Thomas 3
  • Lesley Katzen 3
  • Marc Kaplan 3
  • Markus Clermont 3
  • Max Kanat-Alexander 3
  • Sonal Shah 3
  • APIs 2
  • Abhishek Arya 2
  • Alan Myrvold 2
  • Alek Icev 2
  • Android 2
  • April Fools 2
  • Chaitali Narla 2
  • Chris Lewis 2
  • Chrome OS 2
  • Diego Salas 2
  • Dori Reuveni 2
  • Jason Arbon 2
  • Jochen Wuttke 2
  • Kostya Serebryany 2
  • Marc Eaddy 2
  • Marko Ivanković 2
  • Mobile 2
  • Oliver Chang 2
  • Simon Stewart 2
  • Stefan Kennedy 2
  • Test Flakiness 2
  • Titus Winters 2
  • Tony Voellm 2
  • WebRTC 2
  • Yiming Sun 2
  • Yvette Nameth 2
  • Zuri Kemp 2
  • Aaron Jacobs 1
  • Adam Porter 1
  • Adam Raider 1
  • Adel Saoud 1
  • Alan Faulkner 1
  • Alex Eagle 1
  • Amy Fu 1
  • Anantha Keesara 1
  • Antoine Picard 1
  • App Engine 1
  • Arham Jain 1
  • Ari Shamash 1
  • Arif Sukoco 1
  • Bartosz Papis 1
  • Benjamin Pick 1
  • Bob Nystrom 1
  • Bruce Leban 1
  • Carlos Arguelles 1
  • Carlos Israel Ortiz García 1
  • Cathal Weakliam 1
  • Christopher Semturs 1
  • Clay Murphy 1
  • Dagang Wei 1
  • Dan Maksimovich 1
  • Dan Shi 1
  • Dan Willemsen 1
  • Dave Chen 1
  • Dave Gladfelter 1
  • David Bendory 1
  • David Mandelberg 1
  • Derek Snyder 1
  • Diego Cavalcanti 1
  • Dmitry Vyukov 1
  • Eduardo Bravo Ortiz 1
  • Ekaterina Kamenskaya 1
  • Elliott Karpilovsky 1
  • Elliotte Rusty Harold 1
  • Espresso 1
  • Felipe Sodré 1
  • Francois Aube 1
  • Gene Volovich 1
  • Google+ 1
  • Goran Petrovic 1
  • Goranka Bjedov 1
  • Hank Duan 1
  • Havard Rast Blok 1
  • Hongfei Ding 1
  • Jason Elbaum 1
  • Jason Huggins 1
  • Jay Han 1
  • Jeff Hoy 1
  • Jeff Listfield 1
  • Jessica Tomechak 1
  • Jim Reardon 1
  • Joe Allan Muharsky 1
  • Joel Hynoski 1
  • John Micco 1
  • John Penix 1
  • Jonathan Rockway 1
  • Jonathan Velasquez 1
  • Josh Armour 1
  • Julie Ralph 1
  • Kai Kent 1
  • Kanu Tewary 1
  • Karin Lundberg 1
  • Kaue Silveira 1
  • Kevin Bourrillion 1
  • Kevin Graney 1
  • Kirkland 1
  • Kurt Alfred Kluever 1
  • Kyle Freeman 1
  • Manjusha Parvathaneni 1
  • Marek Kiszkis 1
  • Marius Latinis 1
  • Mark Ivey 1
  • Mark Manley 1
  • Mark Striebeck 1
  • Matt Lowrie 1
  • Meredith Whittaker 1
  • Michael Bachman 1
  • Michael Klepikov 1
  • Mike Aizatsky 1
  • Mike Wacker 1
  • Mona El Mahdy 1
  • Noel Yap 1
  • Palak Bansal 1
  • Patricia Legaspi 1
  • Per Jacobsson 1
  • Peter Arrenbrecht 1
  • Peter Spragins 1
  • Phil Norman 1
  • Phil Rollet 1
  • Pooja Gupta 1
  • Project Showcase 1
  • Radoslav Vasilev 1
  • Rajat Dewan 1
  • Rajat Jain 1
  • Rich Martin 1
  • Richard Bustamante 1
  • Roshan Sembacuttiaratchy 1
  • Ruslan Khamitov 1
  • Sam Lee 1
  • Sean Jordan 1
  • Sebastian Dörner 1
  • Sharon Zhou 1
  • Shiva Garg 1
  • Siddartha Janga 1
  • Simran Basi 1
  • Stan Chan 1
  • Stephen Ng 1
  • Tejas Shah 1
  • Test Analytics 1
  • Test Engineer 1
  • Tim Lyakhovetskiy 1
  • Tom O'Neill 1
  • Vojta Jína 1
  • Zhe Lu 1
  • automation 1
  • dead code 1
  • iOS 1
  • mutation testing 1


Archive


  • ►  2026 (2)
    • ►  mar. (2)
  • ►  2025 (3)
    • ►  okt. (1)
    • ►  sep. (1)
    • ►  jan. (1)
  • ►  2024 (13)
    • ►  dec. (1)
    • ►  okt. (1)
    • ►  sep. (1)
    • ►  aug. (1)
    • ►  jul. (1)
    • ►  maj (3)
    • ►  apr. (3)
    • ►  mar. (1)
    • ►  feb. (1)
  • ►  2023 (14)
    • ►  dec. (2)
    • ►  nov. (2)
    • ►  okt. (5)
    • ►  sep. (3)
    • ►  aug. (1)
    • ►  apr. (1)
  • ►  2022 (2)
    • ►  feb. (2)
  • ►  2021 (3)
    • ►  jun. (1)
    • ►  apr. (1)
    • ►  mar. (1)
  • ►  2020 (8)
    • ►  dec. (2)
    • ►  nov. (1)
    • ►  okt. (1)
    • ►  aug. (2)
    • ►  jul. (1)
    • ►  maj (1)
  • ►  2019 (4)
    • ►  dec. (1)
    • ►  nov. (1)
    • ►  jul. (1)
    • ►  jan. (1)
  • ►  2018 (7)
    • ►  nov. (1)
    • ►  sep. (1)
    • ►  jul. (1)
    • ►  jun. (2)
    • ►  maj (1)
    • ►  feb. (1)
  • ►  2017 (17)
    • ►  dec. (1)
    • ►  nov. (1)
    • ►  okt. (1)
    • ►  sep. (1)
    • ►  aug. (1)
    • ►  jul. (2)
    • ►  jun. (2)
    • ►  maj (3)
    • ►  apr. (2)
    • ►  feb. (1)
    • ►  jan. (2)
  • ►  2016 (15)
    • ►  dec. (1)
    • ►  nov. (2)
    • ►  okt. (1)
    • ►  sep. (2)
    • ►  aug. (1)
    • ►  jun. (2)
    • ►  maj (3)
    • ►  apr. (1)
    • ►  mar. (1)
    • ►  feb. (1)
  • ►  2015 (14)
    • ►  dec. (1)
    • ►  nov. (1)
    • ►  okt. (2)
    • ►  aug. (1)
    • ►  jun. (1)
    • ►  maj (2)
    • ►  apr. (2)
    • ►  mar. (1)
    • ►  feb. (1)
    • ►  jan. (2)
  • ►  2014 (24)
    • ►  dec. (2)
    • ►  nov. (1)
    • ►  okt. (2)
    • ►  sep. (2)
    • ►  aug. (2)
    • ►  jul. (3)
    • ►  jun. (3)
    • ►  maj (2)
    • ►  apr. (2)
    • ►  mar. (2)
    • ►  feb. (1)
    • ►  jan. (2)
  • ►  2013 (16)
    • ►  dec. (1)
    • ►  nov. (1)
    • ►  okt. (1)
    • ►  aug. (2)
    • ►  jul. (1)
    • ►  jun. (2)
    • ►  maj (2)
    • ►  apr. (2)
    • ►  mar. (2)
    • ►  jan. (2)
  • ►  2012 (11)
    • ►  dec. (1)
    • ►  nov. (2)
    • ►  okt. (3)
    • ►  sep. (1)
    • ►  aug. (4)
  • ►  2011 (39)
    • ►  nov. (2)
    • ►  okt. (5)
    • ►  sep. (2)
    • ►  aug. (4)
    • ►  jul. (2)
    • ►  jun. (5)
    • ►  maj (4)
    • ►  apr. (3)
    • ►  mar. (4)
    • ►  feb. (5)
    • ►  jan. (3)
  • ►  2010 (37)
    • ►  dec. (3)
    • ►  nov. (3)
    • ►  okt. (4)
    • ►  sep. (8)
    • ►  aug. (3)
    • ►  jul. (3)
    • ►  jun. (2)
    • ►  maj (2)
    • ►  apr. (3)
    • ►  mar. (3)
    • ►  feb. (2)
    • ►  jan. (1)
  • ▼  2009 (54)
    • ►  dec. (3)
    • ►  nov. (2)
    • ►  okt. (3)
    • ►  sep. (5)
    • ►  aug. (4)
    • ►  jul. (15)
    • ►  jun. (8)
    • ►  maj (3)
    • ►  apr. (2)
    • ►  feb. (5)
    • ▼  jan. (4)
      • TotT: Keep Your Fakes Simple
      • When to use Dependency Injection
      • TotT: Use EasyMock
      • Interfacing with hard-to-test third-party code
  • ►  2008 (75)
    • ►  dec. (6)
    • ►  nov. (8)
    • ►  okt. (9)
    • ►  sep. (8)
    • ►  aug. (9)
    • ►  jul. (9)
    • ►  jun. (6)
    • ►  maj (6)
    • ►  apr. (4)
    • ►  mar. (4)
    • ►  feb. (4)
    • ►  jan. (2)
  • ►  2007 (41)
    • ►  okt. (6)
    • ►  sep. (5)
    • ►  aug. (3)
    • ►  jul. (2)
    • ►  jun. (2)
    • ►  maj (2)
    • ►  apr. (7)
    • ►  mar. (5)
    • ►  feb. (5)
    • ►  jan. (4)

Feed

  • Google
  • Privacy
  • Terms