How it works @BindsInstance dagger 2

I recently updated the dagger from 2.8 to 2.9. and documentation on the latest version was added as follows:

-Added @BindsInstancefor component builders that easily link instances created outside the graph.

-Producers: Added ProducerMonitor.ready ()that is called when all manufacturer inputs are available.

-Removed @Provides(type =...). Use annotations in instead dagger.multibindings. @Produces.typealso been deleted.

- All binding methods are now checked, even if they are not used in a particular @Component

- @Component.dependenciescan no longer include @Modules.

I want to know how these new features:

Thank!

Note. I am new to Dagger 2, but you want to be able to make the most of this library.

+7
source share
3 answers

@BindsInstancedocumented in the user guide: https://google.imtqy.com/dagger/users-guide.html#binding-instances

+4
source

@bindsInstance is used to remove the constructor from the modules and module chains where you get the 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 intended for training purposes, they get context and are created, and in real examples they can be impractical

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);
    }

}

Create such a component

 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 create such a component

   appComponent = DaggerAppComponent
                .builder()
                .application(this)
                .build();
+19
source

@BindsInstance , , , - . , @Singleton. ? DaggerAppComponent DoubleCheck. , - . - , DaggerAppComponent . @BindsInstance, , Lazy<> Provider<>.

Any dependencies (such as string constants, etc.) known as AppComponentare created are good candidates for @BindsInstance. Please note that this is based on Dagger 2.19.

+1
source

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


All Articles