Dagger Flashcards

1
Q

Dependency

A

A coupling between two classes.

*Every ‘new’ we write is a hard dependency

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why Dependecies are bad?

A

– 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Dependency Injection

A

Passing dependencies (inject them) into a class, i.e dependecies objects are instantiated somewhere else and passed as constructor .

  • Decoupling
  • Single Responsability
  • Testability
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Dagger

A

Is a dependency injection framework.

It uses code generations and is based on annotations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Dagger Annotations

A

@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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Scope annotations

A

For application scope : @Singleton
For activity scope: some custom scope @MyScope
scope for instances which live as long as the activity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Lazy injections

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Qualifiers

A

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);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Subcomponents vs Component Dependencies

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Instantiating the component

And adding dependencies

A

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);
}
  1. To automaticaly inject dependencies anotated with @Inject:
    component. inject(this);
  2. To provide dependecies, without @Inject annotation:
    presenter = component.providePresenter();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly