Where should I put bindings for dependency injection?

Posted by Mike G on Programmers See other posts from Programmers or by Mike G
Published on 2014-05-06T01:02:48Z Indexed on 2014/06/05 9:42 UTC
Read the original article Hit count: 326

I'm new to dependency injection and though I've really liked it so far, I'm not sure where bindings should go. I'm using Guice in Java, so some of what I say might be specific to just Guice. As I see it, there's two options:

Accompanying the class(s) its needed for. Then, just write install(OtherClassModule.class) in whatever other modules want to be able to use said class. As I see it, the advantage of this is that classes that want to use it (or manage classes that want to use it) don't need to know any of the implementation detail. The issue I see is that what if two classes want to use two different versions of the same class? There's a lot of customization possible because of DI and this seems to restrict it a lot.

Implemented in the module of the class(s) its needed for. It's the flip of what I said above. Now you have customization, but not encapsulation.

Is there a third option? Am I misunderstanding something obvious? What's the best practice?

© Programmers or respective owner

Related posts about java

Related posts about dependency-injection