본문

180423(월) - Dagger (User's Guide)

Dagger 


User's Guide

- boilerplate writing 줄임

- easy to test

- reusable interchangeable modules


Why dagger2 is Different

- generated code로 full stack implement

- dependency injection을 사용하기 간단하게 하기 위해서, generate code를 hand-written한것처럼 모방

this talk (slides)


Using Dagger

- coffee maker sample로 설명

coffee example.


Declaring Dependencies

-  @Inject annotation사용

- constructor

class Thermosiphon implements Pump {
  private final Heater heater;

  @Inject
  Thermosiphon(Heater heater) {
    this.heater = heater;
  }

  ...
}


- field directly

class CoffeeMaker {
  @Inject Heater heater;
  @Inject Pump pump;

  ...
}


- field에만 @inject가 있고, constructor에는 없다면, dagger는 new instance해서 inject하지는 않는다.

- create instance하고 싶으면 parameter가 없는 constructor에 @Inject 해주자

- method injection도 가능하지만, 보통 constructor or field에 쓴다.


Satisfying Dependencies

- CoffeeMaker가 요청되면, obtain one by calling new CoffeeMaker() and inject fields

- @inject한다고 해서 어디에서나 injecting 받을 수 있는건 아님

- interface는 안됩니다.

- third-party classes에는 annotation을 작성할 수 없습니다

- configurable objects를 configured해야 합니다.


- @Inject가 insufficient or awkward이면, @Provides method를 사용해서 dependency를 satisfy

@Provides static Heater provideHeater() {
  return new ElectricHeater();
}


- 자기 자신을 dependencies 가능

@Provides static Pump providePump(Thermosiphon pump) {
  return pump;
}


- 모든 @Provides method는 module에 있어야 한다.

@Module
class DripCoffeeModule {
  @Provides static Heater provideHeater() {
    return new ElectricHeater();
  }

  @Provides static Pump providePump(Thermosiphon pump) {
    return pump;
  }
}


- @Provide의 이름은 provide가 prefix

- module class의 이름은 module이 suffix


Building a graph

- @Inject and @Provides annotated class는 linked dependencies object graph를 구성

- application's main method or Android Application을 calling하면 well-defined set of roots graph에 access

- dagger2에서 이 set은 arguments가 없고, 원하는 type을 return하는 method가 있는 interface로 정의

- 이런 interface에 @Component 를 적용하고 module을 전달하면 dagger2는 해당 contract를 fully generates implementations

@Component(modules = DripCoffeeModule.class)
interface CoffeeShop {
  CoffeeMaker maker();
}


- implementation시에 Dagger가 prefixed 되어 만들어진다.

- invoking builder() method를 하여 dependencies를 설정하고, build()해서 new instance를 얻으면 된다.

CoffeeShop coffeeShop = DaggerCoffeeShop.builder()
    .dripCoffeeModule(new DripCoffeeModule())
    .build();


- @Component가 top-level type이 아니라면, component's name은 underscore가 포함된다.

class Foo {
  static class Bar {
    @Component
    interface BazComponent {}
  }
}


-> DaggerFoo_Bar_BazComponent


- accesible 할 수 없는 default constructor는 




























































'Mobile > DI' 카테고리의 다른 글

180918(화) - Koin (Koin docs)  (0) 2018.09.18
180911(화) - Koin  (0) 2018.09.11
170713(목) - Kotlin & Dagger2 crash  (0) 2017.07.13
170712(수) - Dependency Injection with Dagger2  (0) 2017.07.12
170707(금) - android-architecture-todo-mvp-dagger  (0) 2017.07.07

공유

댓글