Dependency Injection Flashcards
(21 cards)
singleton
The singleton pattern is a software design pattern that restricts the instantiation of a class to one “single” instance. This is useful when exactly one object is needed to coordinate actions across the system.
dependency injection replaces the…
“new” keyword to create an instance
is a singleton a service?
yes - annotate a singleton service with @Service - we didn’t learn this yet for Dagger
Dagger component
Dagger can create a graph of the dependencies in your project that it can use to find out where to get those dependencies when they are needed. To make Dagger do this, you have to create an interface and annotate it with @Component.
modules (in dependency injection)
classes where dependencies are defined for the class: ie CarModule contains definitions of dependencies for Car, like Engine or Wheels ----- import .. public class CarModule extends --- { @Provides public Engine providesEngine() { return new Engine(); } }
@Provides annotation
this annotation preceeding a methods tells Dagger that this method describes a dependency
@Inject
annotation for the constructor @Inject public Car(Engine engine) { this.engine = engine; }
dependency injection …
…decouples your class’s construction from the construction of its dependencies
core idea of dependency injection:
provide the member variables for a class through its constructor, rather than letting the class instantiate its own objects –> using the @Inject annotated constructor
when classes instantiate their own dependency objects…
…the dependency instantiation logic is mixed up with the actual class logic (hence, dependency injection)
dependency injection separates the instantiation logic from…
…from the code that uses the instantiated object to get its work done. (We can code the instantiation logic any way we please in a separate “injection” class: locality, edge cases, happy case, etc)
True or false: Dependence injection benefits unit testing as a kind of side effect.
True. By accepting the classes we need as arguments to our constructor, we can pass mocks in our unit tests, narrowing the scope of what our unit tests actually check to only the class functions we are testing.
Dagger allows us to specify how to provide dependency objects using …..
…annotations (rather than full methods or classes - think App() ) that are in the familiar Java annotation format, i.e. JUnit’s @Test, @BeforeEach
Dagger @Component
provider of the root object(s)
use provide* method(s) in the @Component interface to “start” the domino effect of instantiations
Dagger @Inject
specify how to construct dependencies Dagger will look at the return type of the provide* method(s) in the @Component interface, go to that class and use the constructor which is annotated with @Inject
Dagger requires us to annotate the specific constructor that it should use to instantiate an object of the class using which annotation?
@Inject
public ObjectConstructor(ObjectType1 objectType1, …
–>finds the class ObjectType1 and looks for the @Inject constructor, etc…
Note: no specific dagger method used! Just @Inject
The interface provide* method…
returns the root object that your application needs
@Component
public interface AppComponent {
public IceCreamService provideIceCreamService();
}
In the application, write two lines of code that a)retrieves the Dagger-generated component class, and b)returns an instance of the root class
//retrieve the Dagger-generated component class AppComponent dagger= DaggerIceCreamService.create()
//return an instance of the root class IceCreamService iceCreamService = dagger.provideIceCreamService();
What is dependency injection with a real time example?
DI can exist between two objects, for instance, a flashlight and a battery. The flashlight needs the battery to function. However, any changes made to the battery, such as switching it another brand/set of batteries, does not ean the dependent object (flashlight) also needs to be changed.
Is dependency injection used in Production?
Yes. Dependency Injection is an architecture pattern, not a testing one. So it is intended to be used when building production code.
When we have a @Singleton in the dependency graph we need to annotate the @Component interface with @Singleton as well.
True or False?
True