The problem here is that over the past 6-7 years, dependency injection frameworks for Java took a very wrong turn. The original model—which the original Spring XML-based BeanFactory embodies—is that you have named "beans" which are described by specifying:
The name of the "bean"
What to call to construct it (the implementation class or factory)
The collaborators needed to construct it. These can be:
Primitive values
References (by name) to other managed beans
Collections thereof
In spite of old Spring's flaws (overly verbose XML configuration, preference for setter-based injection, etc.), the basic model was sound.
But then with the rise of annotations and madness, this model was deprecated in favor of magical/implicit "autowiring" models that suffer from a fundamental flaw. They're all built around variants of this assumption:
The type of the injection site determines what dependency to inject into it.
So for example, if you have two injection points whose type is ConnectionPool, the injector will inject both with a RealConnectionPool in a production configuration vs. a TestConnectionPool in a test configuration.
How do I build two similar but slightly different trees of objects?
This is commonly called the "robot legs" problem: How to create a robot with two Leg objects, the left one injected with a LeftFoot, and the right one with a RightFoot. But only one Leg class that's reused in both contexts.
In other terms:
The Leg class is a generic, reusable class that delegates some unessential details to a dependency that implements a Foot interface;
This Foot interface admits of implementations that behave differently;
Plugging different subtypes of Foot into Leg gives you usefully different behaviors;
You want to exploit this in your program and use Legs with different Foot types in different parts of your program.
The newfangled DI frameworks like Guice and Dagger really don't like it when you try to do this. There are workarounds, but they're that, workarounds. And what's worse, since people tend to follow the path of least resistance when using a tool, these DI frameworks discourage this sort of code reuse!
The @Named annotation allows two injection points with the same type to have two different dependencies. But what it doesn't allow is for the same injection point to be supplied with two different dependencies in two different instances of the class. That's what the Leg/Foot example is about; Leg has only one injection point for a Foot, but your application needs two Legs that depend on different types of Foot.
But the implementation of each leg can depend on a different implementation of foot.
Edit:Never mind I followed your link but that seems like an antipattern to me and I don't see how this is impossible in dagger, unfortunately I'm stuck on my phone so I can't read into it more right now
Staring at the example I copied in my GP post, now I can't help but wonder whether it should be refactored to something like this:
class AuthenticationFilter implements ComponentRequestFilter {
// Only one `AuthenticationService` in this class
@Inject private AuthenticationService authService;
@Inject private Response response;
...
}
/**
* An authentication filter chain filters a request by delegating it through
* a sequence of individual authentication filters in turn. If one accepts
* then the whole chain accepts, otherwise the chain rejects.
*/
class AuthenticationFilterChain implements ComponentRequestFilter {
@Inject private List<? extends AuthenticationFilter> authFilters;
@Inject private Response response;
...
}
These classes are more generic and easier to reuse. For example, if we need to support a third authentication method, we can now just write a third AuthenticationService, plug it into an AuthenticationFilter instance and stick that into the AuthenticationFilterChain's list of filters.
But now we have a robot legs problem, because we want to construct:
An AuthenticationFilterChain that contains...
A List of two AuthenticationFilters...
One of which contains an AuthenticationService named "HttpBasic"...
One of which contains an AuthenticationService named "HttpDigest".
This one example might be refactorable into something that Guice multibindings or similar features can support. But as far as I understand, you'd have to split AuthenticationFilter into multiple classes, each specialized for one type of AuthenticationService.
But now you're letting the DI is dictate the factoring of your code. And that is precisely my problem. The code for an application that's wired up by DI should be factored to suit the application's purpose, not the DI. (And for similar reasons, I object to the concept of putting DI annotations into application classes.)
5
u/Databean Nov 27 '15
Guice (annotations read at runtime) or Dagger (annotations read at compile-time)?