- A dependencyis a coupling between two classes usually because one of them uses the other to do something.
- Dependency injectionconsists of passing dependencies (inject them) via constructor in order to extract the task of creating modules out from other modules. Objects are instantiated somewhere else and passed as constructor attributes when creating the current object.
- A dependency injectoris another module in our app that is in charge of providing instances of the rest of modules and inject their dependencies. It's responsibility is the creation of modules is localized in a single point in our app, and we have full control over it.
- Daggeris a dependency injector designed for low-end devices. Most dependency injectors rely on reflection to create and inject dependencies. Reflection is very time consuming on low-end devices, and specially on old android versions. Dagger, however, uses a pre-compiler that creates all the classes it needs to work. That way, no reflection is needed. Dagger is less powerful than other dependency injectors, but it’s the most efficient.
- Modulesare classes that provide instances of the objects we will need to inject.
- Modulesare defined by annotating the class with @Module.
- The object graphis the place where all these dependencies live. Theobject graphcontains the created instances and is able to inject them to the objects we add to it.
- Without dependency injectionyou cannot easily swap out modules such as a test module.
- Modulespecifies all the providers of the injection.
- Componentis an interface that Dagger will use to generate the code that will do the dependency injection for you.
- @Module+- @Provides: mechanism for providing dependencies.
- @Inject: mechanism for requesting dependencies.
- @Component: bridge between modules and injections.
- Modules are classes whose methods provide dependencies.
- @Moduleannotation is on the class.
- @Providesannotation is one each method.
- Modulesare designed to be partitioned and composed back together to form a complete graph.
- @Injectannotation required to request dependency.
- Constructor Injection
- @Injecton a single constructor.
- Constructor parameters are dependencies.
- Dependencies can be stored in private and final fields.
- Implicitly made available for downstream injection.
 
- Method Injection
- @Injecton methods.
- Method parameters are dependencies.
- Injection happens after object is fully instantiated.
- Only valid use case : passting thisto a dependency.
 
- Field Injection
- @Injecton fields for dependencies.
- Field may not be private or final.
- Injection happens after the object is fully instantiated but before method injection.
- Object is usually responsible for or aware of injection.
 
- Providing objects with Modules and requesting injection with @Inject, Components bridge the two.
- Componentsbridge between modules and injection.
- Componentacts as an injector.
- Componentis an interface with- @Componentannotation. In the annotation, you list the modules that make up that component.
- Componentsare aware of the scope of the dependencies, so all of the dependencies inside this module are Singletons, in this case you can annotate the Component as- @Singleton.
- A dependency in a scope will only have a single instance.
- @Singletonis the largest scope.
- The fundamentals of dependency injection : how you provide dependencies, how you request dependencies, and the Component interface of how you link the two together.
          Last active
          August 3, 2018 19:19 
        
      - 
      
- 
        Save lawloretienne/145a24b12a35e796d59eeb0445782b2c to your computer and use it in GitHub Desktop. 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment