jraska / Dagger-Codelab

Codelab to teach and demonstrate usage of dependency injection with Dagger 2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dagger-Codelab

Codelab to teach and demonstrate usage of dependency injection with Dagger 2

CircleCI

What you will learn

  • About dependency injection(DI) and Inversion of Control(IoC)
    • Introduction to the problem, why it matters.
    • Why we use DI frameworks - which problems they solve for us.
    • Brief history of DI. Spring -> Guice -> Dagger 1 -> Dagger 2
      Initial presentation slides
  • Basics of Dagger
    • How to setup Dagger in your project.
    • How to use @Inject annotation, creating simple @Component
    • Adding @Module. Discussion when to use modules and when @Inject annotation.
    • Usage of simple @Singleton scope.
    • Exploring Dagger generated source code and learning from it.
  • Wiring together with Android
    • Where to create and hold instances of components.
    • Injecting Activities, Injecting Fragments.
    • Using @BindsInstance to bind objects we don't own.
  • Multi-module project setup
    • How to compose modules together within AppComponent
    • Injecting Activity/Fragment and other dependencies within separate module.
    • Sharing instances with other modules.
  • Multibindings
    • Composing @IntoMap and @IntoSet.
    • Plugin based development within multi-module project.
    • Discuss which problems can multibinding solve.
  • Instrumented Testing
    • How to replace dependencies within androidTest.
    • Using separate component to replace dependencies in tests.
    • How to assert on your analytic events.
  • Dagger Hilt migration
    • Setting up Dagger Hilt in the project.
    • How to Migrate project to Hilt
  • Instrumented testing with Dagger Hilt
    • Setting up testing in project.
    • Replacing dependencies in tests
    • Discussing Hilt pros and cons.

Why this codelab?

  • Primarily this should be a learning resource for any engineer who wants to learn or recap Dagger usage.
  • The codelab format is chosen with the belief, that first hand experience leaves stronger memories and the skills are truly learned.
  • Dagger became more complex over years - making onboarding of new engineers harder.
  • Dagger became also misused or overused. New engineers see already brownfield and Dagger seems like one of the devils responsible for this leading to lower trust in the tool. Dagger can be almost invisible in your project.
  • We already forgot about the problems we had before Dagger like runtime DI resolution. It is important to be reminded about these problems.

How to use this Codelab

  • Codelab is split into few smaller sections - each of them can be run independently.
  • Each section has a branch with a format {section number}-{area of focus} e.g. 01-basics. The branch with higher number is always the solution of the previous section. master branch is the final solution of present tasks.
  • You can run the codelab yourself, by following the instructions within each sections.
  • In case of any question, please feel free to create an issue in this repo.

Now let' start by moving to section you want

Dagger Hilt Option

If you would like to see how would the current project look like in Dagger Hilt check the extra exercises:

With solution at 08a-hilt-option-solution

References

About

Codelab to teach and demonstrate usage of dependency injection with Dagger 2

License:Apache License 2.0


Languages

Language:Kotlin 100.0%