"overrides" in Dagger 2

2,945 views
Skip to first unread message

Erik Kuefler

unread,
Jan 13, 2015, 2:25:05 PM1/13/15
to dagger-...@googlegroups.com
I'm trying to figure out if/how "overrides" on @Module works in the presence of Components in Dagger 2. I have a module FakeBackendModule that includes BackendModule with overrides=true, and a component that uses FakeBackendModule. FakeBackendModule and BackendModule both define a binding for ListeningExecutorService - I intend the one from FakeBackendModule to override the one from BackendModule, which worked in Dagger 1. In Dagger 2 I get this error:

TestComponent.java:14: error: com.google.common.util.concurrent.ListeningExecutorService is bound multiple times:
  ApiRouter getRouter();
            ^
      @Provides @Singleton com.google.common.util.concurrent.ListeningExecutorService backends.testing.FakeBackendModule.provideExecutorService()
      @Provides @Singleton com.google.common.util.concurrent.ListeningExecutorService backends.BackendModule.provideExecutorService()

What has changed with regards to overrides and how am I meant to deal with it?

Pierre-Yves Ricau

unread,
Feb 5, 2015, 1:16:56 AM2/5/15
to dagger-...@googlegroups.com

Override isn't supported any more. Component interfaces is how you do these kind of things.

Let's say you have a BackendComponent. It needs a ListeningExecutingService. You need a create another component that will provide it, e.g. ServiceComponent and use that component as a dependency on BackendComponent.

Then you can create FakeServiceComponent and have it extend ServiceComponent.

At runtime, you can give the FakeServiceComponent when creating tge BackendComponent.

--
You received this message because you are subscribed to the Google Groups "Dagger Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dagger-discus...@googlegroups.com.
To post to this group, send email to dagger-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vitaliy Popov

unread,
Apr 19, 2015, 10:29:53 AM4/19/15
to dagger-...@googlegroups.com
Hi Pierre-Yves Ricau, you said about making FakeServiceComponent. Could you please add simple example, because i always get error while compiling project when i make subclasses for Module or Component.
And by this link http://google.github.io/dagger/dagger-1-migration.html (3rd section) is said that we should create subclass of Module for overriding (actually it isn't work).
So, where is the truth?

четверг, 5 февраля 2015 г., 8:16:56 UTC+2 пользователь Pierre-Yves Ricau написал:

Gabor Varadi

unread,
Aug 31, 2015, 9:58:39 AM8/31/15
to Dagger Discuss
That sounds great in theory, but multiple scoped component dependencies are unsupported, which means the "pluggability" you've mentioned isn't actually possible unless you stop using scopes completely and store the "scoped" dependency references in the module, which adds a lot of extra boilerplate over something that Dagger2 should be supporting out of the box..

Dorian Cussen

unread,
Sep 4, 2015, 8:17:03 AM9/4/15
to Dagger Discuss
Some examples would be great. A big win when using Dagger 1 was how easy it was to test stuff as you could override individual modules on the object graph at test time and easily mock dependencies etc. There seems to be no simple way to do this with dagger 2 and all solutions I have seen seem to use more LoC than before. Im sure I am missing something but would be great to know what :)
Reply all
Reply to author
Forward
0 new messages