Dagger Flashcards
Dependency
A coupling between two classes.
*Every ‘new’ we write is a hard dependency
Why Dependecies are bad?
– If we need to change one class with another, we necessarily need to modify the code of the coupled class.
– That’s really bad if we want to create a TESTABLE app, because unit testing requires that when we are testing a class, it is isolated from the rest of classes in our app.
To do this, we need to substitute dependencies with mocks.
Don’t forget single responsibility principle.
Dependency Injection
Passing dependencies (inject them) into a class, i.e dependecies objects are instantiated somewhere else and passed as constructor .
- Decoupling
- Single Responsability
- Testability
Dagger
Is a dependency injection framework.
It uses code generations and is based on annotations.
Dagger Annotations
@Inject — base annotation whereby the “dependency is requested”
@Module — classes which methods “provide dependencies”
-includes : other @Module-annotated classes
@Provide — methods inside @Module, which “tell Dagger how we want to build and present a dependency“
@Component — bridge between @Inject and @Module
-modules - @Module-annotated classes used to
generate the component implementation
-dependencies- other components
@Scope — enables to create global and local singletons
@Qualifier — if different objects of the same type are necessary
Scope annotations
For application scope : @Singleton
For activity scope: some custom scope @MyScope
scope for instances which live as long as the activity.
Lazy injections
Sometimes you need an object to be instantiated lazily. For any binding T, you can create a Lazy which defers instantiation until the first call to Lazy’s get() method.
@Inject Lazy lazyGrinder;
public void brew() {
lazyGrinder.get().grind();
}
Qualifiers
Sometimes the type alone is insufficient to identify a dependency.
In this case, we add a qualifier annotation. @Qualifier @Documented @Retention(RUNTIME) public @interface Named { String value() default ""; }
@Provides @Named("hot plate") static Heater provideHotPlateHeater() { return new ElectricHeater(70); }
@Provides @Named("water") static Heater provideWaterHeater() { return new ElectricHeater(93); }
Subcomponents vs Component Dependencies
Subcomponents have acces to all parent objects;
Component dependecies gives acces only to those parent object which are exposed in Component Interface.
REstriction for both: Two dependent components cannot share the same scope.
Instantiating the component
And adding dependencies
DaggerNetComponent component =
DaggerNetComponent.builder()
.appModule(new AppModule(this))
.build();
If acomponent does not have any constructor arguments for any of its modules, then we can use .create() as a shortcut instead:
mNetComponent = DaggerNetComponent.create();
Activities, services, or fragments that are allowed to request the dependencies declared by the modules (by means of the @Inject annotation) should be declared in this class with individual inject() methods: @Singleton @Component(modules={AppModule.class, NetModule.class}) public interface NetComponent { void inject(MainActivity activity); // void inject(MyFragment fragment); // void inject(MyService service); }
- To automaticaly inject dependencies anotated with @Inject:
component. inject(this); - To provide dependecies, without @Inject annotation:
presenter = component.providePresenter();