Pros n cons of using Dependency injection.

142 views
Skip to first unread message

Rohit Sharma

unread,
Jun 9, 2015, 3:20:03 AM6/9/15
to growing-object-o...@googlegroups.com
What are the advantages and disadvantages of using dependency injection?
Also where should I use it and where to avoid.

Daniel Wellman

unread,
Jun 10, 2015, 8:43:22 PM6/10/15
to growing-object-o...@googlegroups.com
Hi Rohit!

I'm assuming that by dependency injection you mean passing in a collaborator to an object, rather than the object creating the collaborator itself.  And at least two ways I know to do this is either passing in the dependency via the constructor or via a method parameter.

I find that using dependency injection can help me separate how an object works from how it is constructed.  That is, if I'm building some object, say a newsletter distribution service that needs to collaborate with something that can get send e-mails over SMTP, I can pass that SMTP-speaking object into the constructor as a collaborator.  Then I can worry about all the messy connection and mail session lifecycle into other objects.  This means there is less code in my object, the code can focus on solving my problem domain (distributing newsletters to subscribers) instead of intermixing SMTP and connection code all in one place.  This usually means the code is easier for me to understand later.  I can pass in an alternate collaborator in my tests if it's the real thing is hard to work with.  And when I start thinking about alternate testing versions of the collaborator, I can also start thinking of alternative implementations in the production code that might be useful as well.  For example, an implementation of the mail sender that does nothing but prints what it would have sent -- a "dry-run" feature, for example.

The disadvantage I've found to using dependency injection is that I can err on the side of passing in *too many* dependencies, creating code that is equally hard to test and understand.  What I've learned from GOOS is that some collaborators might be peers of the object and have other uses in my system, and some objects are truly implementation details and can be created and managed entirely by the object.  GOOS calls these objects "internals".  

Is this what you were asking about, Rohit?

Cheers,
Dan



On Tue, Jun 9, 2015 at 12:39 AM, Rohit Sharma <hun...@gmail.com> wrote:
What are the advantages and disadvantages of using dependency injection?
Also where should I use it and where to avoid.

--

---
You received this message because you are subscribed to the Google Groups "Growing Object-Oriented Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to growing-object-oriente...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raoul Duke

unread,
Jun 11, 2015, 4:58:29 PM6/11/15
to growing-object-o...@googlegroups.com
Please also see discussions around 'Object Capabilities' (ocaps) for
some more pros.

Władysław Mettler

unread,
Jun 12, 2015, 5:34:00 AM6/12/15
to growing-object-o...@googlegroups.com
Hi Rohit,

One of the advantages that were not mentioned is that dependency injection provides you with a mechanism for a very clear mocking of objects that fall outside of the system under test. The loose coupling you achieve enables much more freedom in refactoring.

A big disadvantage I found was that most of junior to mid-range coders do not understand the concept of inversion of control. Also, it adds a burden of a contract  between the 'injected' and the 'injectee'; which may be both a good and a bad thing...

Cheers,
WM

Luca Minudel

unread,
Jun 12, 2015, 5:46:06 PM6/12/15
to growing-object-o...@googlegroups.com
Rohit, is probably useful to search for an answer within the context of the problem that one wants to solve (with DI) and the other techniques that one considers as possible alternatives.
Another interesting angle is to think about which errors can one do misusing a technique such as DI.

HTH, Luca

한울

unread,
Jun 12, 2015, 9:35:59 PM6/12/15
to growing-object-o...@googlegroups.com

I think that DI exposes dependencies explicitely.

Sony Xperia™스마트폰에서 보냄



---- Luca Minudel 님이 작성: ----
Reply all
Reply to author
Forward
0 new messages