Search code examples
javaguicedagger-2

Migrating a Guice-based project to Dagger


I have a Guice based project using vanilla Guice; no Assisted-Inject, no AOP, no extra plugin extending Guice, etc. To run it more easily on Android, Dagger seems like a better solution. Every class has a dependency and a constructor with @Inject annotation. No field or method injection is used.

The modules are quite simple (making Guice an overkill) and mostly contain bindings like the following:

class SomethingModule extends AbstractModule {

  protected void configure() {
    Bind(Handler.class)
      .annotatedWith(Names.named("something"))
      .to(SomeImplementation.class);
    }
  }

}

And later used like the following:

Injector inj = Guice.createInjector(new SomethingModule());
... = inj.getInstance(SampleInterface.class);
// and rest of the code.

Unfortunately, I can not get my head around Daggers terminology. Can you guide me with a direct translation / transformation of a Guice module to a Dagger module?

Dagger has:

  • Dagger's Components.
  • Dagger's Modules.
  • @Provides
  • @Inject

Guice has:

  • @Inject
  • @Named (or any custom annotation, if implemented correctly).
  • Our modules extending AbstractModule.
  • @Provides in the modules.
  • Guice Injector created from modules.

How do these relate?

Update: In addition to the nice answer by EpicPandaForce, these slides can help too.


Solution

  • Bind(Handler.class)
    .annotatedWith(Names.named("something"))
    .to(SomeImplementation.class);
    

    Would translate to

    @Module
    public class SomethingModule {
        @Provides
        @Named("something")
        //scope if needed
        public Handler handler() {
            return new SomeImplementation();
        }
    }
    

    Which would be bound to an "Injector" (component):

    @Component(modules={SomethingModule.class})
    //scope if needed
    public interface SomethingComponent {
        @Named("something")
        Handler handler();
    
        void inject(ThatThingy thatThingy);
    }
    

    Which is an "injector" that you have to create with the APT-generated builder:

    SomethingComponent somethingComponent = DaggerSomethingComponent.builder()
                                               .somethingModule(new SomethingModule()) //can be omitted, has no params
                                               .build();
    somethingComponent.inject(thatThingy);
    

    Where that thingy has

    public class ThatThingy {
        @Inject
        @Named("something")
        Handler handler;
    }
    

    Components typically exist per scope, so for example @ApplicationScope has one "injector" (component). Scoping can be achieved with subcomponents and component dependencies.

    Important fact, a component has provision methods (which are the dependencies that are inherited to subscoped components if you use component dependencies), and void inject(X x); formatted methods. This is required for field injection per concrete type. A base class for example can only inject itself, and not its subclasses. You can however write a method called protected abstract void injectThis() which would call the .inject(this) on the subclass as well.

    As I haven't really used Guice, I'm not sure if I missed out on anything. I think I forgot constructor injection, which is an issue because while Dagger does support it, it cannot be reconfigured. For reconfiguration, you have to use modules, and do the injection in the constructors yourself.

    @Module(includes={ThoseModule.class, TheseModule.class})
    public abstract class SomethingModule {
        @Binds
        abstract Whatever whatever(WhateverImpl impl);
    }
    
    @Singleton
    public class WhateverImpl implements Whatever {
        Those those;
        These these;
    
        @Inject
        public Whatever(Those those, These these) {
            this.those = those;
            this.these = these;
        }
    }
    
    @Component(modules={SomethingModule.class})
    @Singleton
    public interface SomethingComponent {
        These these();
        Those those();
        Whatever whatever();
    }