Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dagger2

Buzzvil
August 22, 2018

 Dagger2

By Ethan

Buzzvil

August 22, 2018
Tweet

More Decks by Buzzvil

Other Decks in Programming

Transcript

  1. What is Dagger2 • Dagger2 is DI(Dependency Injection) library of

    Java • Based on annotation processing, not reflection, to aim high performance Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is an adaptation of an earlier version created by Square and now maintained by Google. from official document
  2. Dagger is not specific functional library • Dagger is just

    tool that handle easily dependency injection • So, we need to know about dependency injection • Let’s check dependency injection one by one
  3. Dependency Injection Dependency injection is a style of object creation

    in which an objects are created by an external entity, or technique whereby one object supplies the dependencies of another object.
  4. Dependency Injection - Advantages / Disadvantages • Advantages ◦ Flexibility

    of architecture ◦ Focus only on business logic ◦ Better for Unit Test and Refactoring ◦ Reconfigurable without recompile • Disadvantages ◦ Make code difficult to trace or read ◦ More complexity on initializing architecture ◦ Need to be apply design principle
  5. How to dagger inject dependencies? • Make a rule about

    creation of object. (Which object is injected into?) • Rule is defined by @Component, @Module • Mark something that need to be injected with annotation • Call inject() function
  6. Dagger2 - Component • Component is Injector • Component injects

    dependencies provided from Module • modules attribute means dependency provider Module • Component must be implement inject method.
  7. Dagger2 - Module • Module is Provider • Implement how

    to create dependency class and annotate @Provides
  8. Dagger2 - Inject • Mark @Inject to dependency class •

    Call inject method. Injection process occurs at this time.
  9. Dagger2 - Diagram App Component Module 1. App request dependencies

    to Component 2. Component find proper module in own modules 3. Component inject dependencies to App
  10. Conclusion • Advantages ◦ Concise code ◦ Reusability ◦ Force

    to DI Principle • Dagger doesn’t convert code to DI pattern from plain code • Dagger just help to make code, already applied DI pattern, concise • If you wanna dagger’s advantages, change your code to DI pattern.