Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traceback on Dagger .plus() on incomplete parent

Consider a Dagger module:

@Module(library = true, complete = false)
public static class Module {
    @Provides
    public Contextualized providesContextualized(Context ctx) {
        return new Contextualized(ctx.getUsername());
    }
    // ... and many more such provides.
}

Context is an object may be connected to e.g. an HTTP session that cannot be known at startup, when one would normally create a graph:

@Module(library = true, complete = false)
public static class ContextModule {
    private final String username;
    public ContextModule(String username) { this.username = username; }

    @Provides
    public Context providesContext() {
        return new Context() {
            public String getUsername() { return username; }
        };
    }
}

Given that Module is sufficiently long, it would seem to make sense to first create a graph for Module:

ObjectGraph baseline = ObjectGraph.create(new Module());

and then, on processing a particular request, to create a unique graph that makes the graph complete:

ObjectGraph withContext = baseline.plus(new ContextModule("killroy"));

However, .plus() seems to assume that the inherited graph is complete:

java.lang.IllegalStateException: Errors creating object graph:
Context could not be bound with key Context required by class PlusExample$Module
at dagger.internal.ThrowingErrorHandler.handleErrors(ThrowingErrorHandler.java:34)
at dagger.internal.Linker.linkRequested(Linker.java:182)
at dagger.internal.Linker.linkAll(Linker.java:109)
at dagger.ObjectGraph$DaggerObjectGraph.linkEverything(ObjectGraph.java:244)
at dagger.ObjectGraph$DaggerObjectGraph.plus(ObjectGraph.java:203)
at PlusExample.plusFailsOnIncompleteModule(PlusExample.java:46)

Have I misunderstood what .plus() does or is this a limitation in Dagger? Is there some other straightforward way to introduce the user late into the graph? (It would be annoying if each provide in Module would have to look up the user from a threadlocal or some such.)

like image 581
Bittrance Avatar asked Nov 10 '22 05:11

Bittrance


1 Answers

Looks like an answer to your question: https://github.com/square/dagger/issues/384

Hope that helps!

like image 113
iluu Avatar answered Nov 15 '22 07:11

iluu