Skip to content

Instantly share code, notes, and snippets.

@lawloretienne
Last active August 3, 2018 19:19
Show Gist options
  • Save lawloretienne/145a24b12a35e796d59eeb0445782b2c to your computer and use it in GitHub Desktop.
Save lawloretienne/145a24b12a35e796d59eeb0445782b2c to your computer and use it in GitHub Desktop.
  • A dependency is a coupling between two classes usually because one of them uses the other to do something.
  • Dependency injection consists 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 injector is 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.
  • Dagger is 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.
  • Modules are classes that provide instances of the objects we will need to inject.
  • Modules are defined by annotating the class with @Module.
  • The object graph is the place where all these dependencies live. The object graph contains the created instances and is able to inject them to the objects we add to it.
  • Without dependency injection you cannot easily swap out modules such as a test module.
  • Module specifies all the providers of the injection.
  • Component is 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.
  • @Module annotation is on the class.
  • @Provides annotation is one each method.
  • Modules are designed to be partitioned and composed back together to form a complete graph.
  • @Inject annotation required to request dependency.
  • Constructor Injection
    • @Inject on a single constructor.
    • Constructor parameters are dependencies.
    • Dependencies can be stored in private and final fields.
    • Implicitly made available for downstream injection.
  • Method Injection
    • @Inject on methods.
    • Method parameters are dependencies.
    • Injection happens after object is fully instantiated.
    • Only valid use case : passting this to a dependency.
  • Field Injection
    • @Inject on 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.
  • Components bridge between modules and injection.
  • Component acts as an injector.
  • Component is an interface with @Component annotation. In the annotation, you list the modules that make up that component.
  • Components are 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.
  • @Singleton is 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment