how it works @BindsInstance dagger 2

Juanes30 picture Juanes30 · Feb 7, 2017 · Viewed 14.2k times · Source

I have recently updated dagger 2.8 to 2.9 dagger. and documentation of the last release have been added as follows:

-Added @BindsInstance for component builders to easily bind instances that are constructed outside of the graph.

-Producers: Added ProducerMonitor.ready (), which is called when all of a producer's inputs are available.

-Removed @Provides(type =...) usage. Use the annotations in dagger.multibindings instead. @Produces.type was also removed.

-All binding methods are now validated, even if they are unused in a particular @Component

-@Component.dependencies can no longer include @Modules.

I want to know how these new features:

Thank you!!

Note: I am new to dagger 2, but you want to be able to make maximum use of this library.

Answer

Thracian picture Thracian · Jul 18, 2018

@bindsInstance is used for removing constructor from module and chaining modules where you get component.

Without @BindsInstance

@Module
public class AppModule {

    private final Application application;

    public AppModule(Application application) {
        this.application = application;
    }

    @Provides
    @Singleton
    Application provideApplication() {
        return  application;
    }

    @Provides
    @Singleton
    public SharedPreferences providePreferences() {
        return application.getSharedPreferences("store",
                Context.MODE_PRIVATE);
    }
}

These modules(ToastMakerModule, and SensorControllerModule) are for learning purposes they get context and instantiate , may not be practical for real examples

public class ToastMaker {

    private Application application;

    public ToastMaker(Application application) {
        this.application = application;
    }

    public void showToast(String message) {
        Toast.makeText(application, message, Toast.LENGTH_SHORT).show();
    }
}

    @Module
    public class ToastMakerModule {

        @Singleton
        @Provides
        ToastMaker provideToastMaker(Application application) {
            return  new ToastMaker(application);

        }
   }

@Singleton
@Component(modules = {AppModule.class, ToastMakerModule.class, SensorControllerModule.class})
public interface AppComponent {
    void inject(MainActivity mainActivity);

    // DaggerAppComponent.build() returns this Builder interface

    @Component.Builder
    interface Builder {
        AppComponent build();

        Builder appModule(AppModule appModule);

        Builder sensorControllerModule(SensorControllerModule sensorControllerModule);

        Builder toastMakerModule(ToastMakerModule toastMakerModule);
    }

}

Build component like this

 appComponent = DaggerAppComponent
                .builder()
                .appModule(new AppModule(this))
                .sensorControllerModule(new SensorControllerModule())
                .toastMakerModule(new ToastMakerModule())
                .build();

With @BindsInstance

@Module
public class AppModule {

    @Provides
    @Singleton
    public SharedPreferences providePreferences(Application application) {
        return application.getSharedPreferences("data",
                Context.MODE_PRIVATE);
    }
}

Component

@Singleton
@Component(modules = {AppModule.class, ToastMakerModule.class, SensorControllerModule.class})

public interface AppComponent {
    void inject(MainActivity mainActivity);

    @Component.Builder
    interface Builder {

        AppComponent build();


        // @BindsInstance replaces Builder appModule(AppModule appModule)
        // And removes Constructor with Application AppModule(Application)

        @BindsInstance
        Builder application(Application application);
    }
}

and build component like this

   appComponent = DaggerAppComponent
                .builder()
                .application(this)
                .build();