Slide 1

Slide 1 text

Dagger2 created by ethan.you

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

Dependency Injection - implementation by constructor ● Constructor

Slide 6

Slide 6 text

Dependency Injection - implementation by setter ● Setter

Slide 7

Slide 7 text

Dependency Injection - implementation by annotation ● Annotation

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Dagger2 - Component ● Component is Injector ● Component injects dependencies provided from Module ● modules attribute means dependency provider Module ● Component must be implement inject method.

Slide 11

Slide 11 text

Dagger2 - Module ● Module is Provider ● Implement how to create dependency class and annotate @Provides

Slide 12

Slide 12 text

Dagger2 - Inject ● Mark @Inject to dependency class ● Call inject method. Injection process occurs at this time.

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Let’s practice

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

Q & A