dependency injection - Dagger - Is it possible to select a Provider based on inheritance? -
I currently have a base
class that contains a member I want to inject . However, I depend on the concrete type of this member to be immediately on subclass
. Which side I am targeting, there is something with these lines:
public interface stringinfo {public string getString (); } Public class HelloStringConcrete StringInterface applied {public string getString () {return "hello"; }} Public square applies World String Concrete string enforfference {receives public string () {return "world"; }} Public Essentials Support {@Inject StringInterface Member; Access Public Base () {// Object Graph MyObjectGraph.get (). (Inject this); } Public Zero Print Some () {System.out.println (member.getString ()); }} Public Square SubclassHello Base {} Expands Public Category SubclassWorld Base {} Expand Module {injects = {SubclassHello.class}} Public Square HelloModule {@Provides StringInterface Provide StringInterface () {New HelloStringConcrete Returns}; }} @module (Inject = {Subclass World. Class}) Public class world module {@Provides StringInterface provides StringInterface () {New WorldStringConcrete return}; }}
So now what I would like to do:
@ module (contains = {HelloModule.class, WorldModule class}) Public class BigModule { } // Somewhere else the code ... objectGraph = ObjectGraph.create (New BigModule ()); // In another piece of code ... subclass Hello Hello = new subclass (hello); Sub-class world world = new sub-orbit (); Hello.printSomething (); World.printSomething (); // Hope this will be the result: // hello // world
This type of installation will not work though, because there will be two modules with the same provider, resulting in a duplicate provider The error will be to see the resolution of this problem without using the Scope Graph extension via compile time @ Named
or @qualifer
annotation, or graph.plus () Good for
because these strategies It is necessary to introduce coupling of the sub-classes.
It is possible, but I suppose That code is attached below using 'I' scoped graph or annotation. In fact, you can use Constructor injection to inject concrete dependencies on your subclass
and SubclassWorld
. Public Authentic Class Support {Private Final String Interface Member; Public base (string interface member) {this.member = member; } ...} @module (injects = {SubclassWorld.class}) Public class world module {@ProvidesWorldStringConcrete provided StringInterface () {New WorldStringConcrete back); }} Public Square SubclassWorld Base Extension {@Inject Public SubclassWorld (WorldStringConcrete worldStringConcrete) {super (worldStringConcrete); }} @module (injects = {subclassHello.class}) Public class HelloModule {@Provides HelloStringConcrete provided StringInterface () {New HelloStringConcrete (); }} Public Square SubclassHello Base {@ InStartConcrete helloStringConcrete} {super (helloStringConcrete) extends; }} // somewhere else in the code ... Object Graph ObjectGraph = Object Graph. (New bizomum ()); // in another piece of code ... subclassHello hello = objectGraph.get (subclassHello.class); Subclass world = object graph. (SubclassWorld.class);
I do not think there are other solutions. How can a dagger find out which code should be implemented for concrete sections?
Comments
Post a Comment