Guice Project Dagger Migration

I have a Guice based project using vanilla Guice; no Assisted-Inject, no AOP, no additional plugin extending Guice, etc. To make it easier to run on Android, the Dagger seems like the best solution. Each class has a dependency and constructor with @Inject annotation. No field or method injection is used.

The modules are pretty simple (which makes Guice redundant) and basically 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 as follows:

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

Unfortunately, I cannot omit the terminology of daggers . Can you direct me with direct translation / conversion of the Guice module to the Dagger module?

The dagger has:

  • Dagger components.
  • Dagger Modules.
  • @Provides
  • @Inject

Guice has:

  • @Inject
  • @Named (or any custom annotation, if done correctly).
  • Our modules are expanded by AbstractModule .
  • @Provides in modules.
  • Guice Injector , created from modules.

How does this compare?

Update: In addition to the nice answer from EpicPandaForce, these slides can also help.

+5
source share
1 answer
 Bind(Handler.class) .annotatedWith(Names.named("something")) .to(SomeImplementation.class); 

Will translate to

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

What would be related to the " Injector " (component):

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

What is the โ€œinjector" that you must create using the created APT creator:

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

Where is this thing

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

Components usually exist for an area , so, for example, @ApplicationScope has one โ€œinjectorโ€ (component). Examination can be achieved using subcomponents and component dependencies.

An important fact is that the component has rendering methods (which are dependencies that are inherited from undermining components if you use component dependencies) and void inject(X x); formatted methods. This is required for field injection for a particular type . For example, a base class can introduce itself and not subclasses. However, you can write a method called protected abstract void injectThis() , which also calls .inject(this) in a subclass.

Like I really haven't used Guice, I'm not sure I missed anything. I think I forgot the injection constructor, which is a problem because when the Dagger supports it, it cannot be reconfigured. For reconfiguration, you need to use modules and make injections in the constructors yourself.

 @Module(includes={ThoseModule.class, TheseModule.class}) public class SomethingModule { @Provides @Singleton public Whatever whatever(Those those, These these) { return new Whatever(those, these); } } public class Whatever { Those those; These these; 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(); } 
+3
source

Source: https://habr.com/ru/post/1246643/


All Articles