Open Groostav opened 7 years ago
At a glance both the status quo and option one seem more reasonable than an @ActualType
annotation. I'd try to go the other direction, and remove the implied expectation that scenarioOneHandlerProvider
and scenarioTwoHandlerProvider
return specific implementations. It's hard to say for certain without understanding complexDomainLogicThatDerrivesScenarioOne()
, but that's the part that seems smelly to me.
Hey guys,
so we have a use case that I don't believe is really uncommon. The gist is that we have a factory that has a dependency on a bunch of providers. It exposes a single method that switches based on its input and the state of the application to select one of these providers to use. The values given by these providers could be specific, but they would rather use an an interface that is the interface on this factory method.
some code, given that I have:
I think it should be pretty apparent that I don't want
scenarioOneHandlerProvider
to provide the same type asscenarioTwoHandlerProvider
. So, given that I want to return different types for scenario one and scenario two, these are my options:existing option one
request the most specific type at provider-injection:
downside:
Testing this is harder than it needs to be. Nothing about this code has a runtime dependency on
SOneHandlerImplementsDomainInterface
, yet in testing I'm now going to have to create a hard instance of that type, even though this code compiles and expresses its intent through variable names with the previous type. Yes I'm aware of mocking frameworks, but creating an instance of a concrete type is always going to be harder than creating an instance of the interface for testing!existing option two
use binding annotations:
then update your module bindings:
downside:
In my production code I'm actually injecting 5 such providers and soon there will be a 6th. This means the calling module(s) will each need 5 separate bindings. With one growing each time. To a user accustomed to spring or other explcit-configuration dependency injection systems this might be OK, but to those of us who work hard to keep things auto-wired nicely, its a pain in the butt.
proposed new option
increase flexibility of providers: I was hoping to write something like
Now this could be added as another special case pre-supplied
BindingAnnotation
, or it might (more generally) register annotation instances as available to providers, while also lifting provides from implementation providers to type-alias providers:or perhalps with a similar provider method:
many thanks for a great library and keep up the good work!