Android and dagger2
-
Upload
seongug-jung -
Category
Mobile
-
view
3.343 -
download
5
Transcript of Android and dagger2
Dependency Inject 과 Dagger2● 특정 동작을 하는데 필요한 컴포넌트들을 할당해주는 과정
○ 생성자 주입○ Setter 사용○ 의존성 주입을 하는 동작을 포함한 구현체를 실행하여 동작○ 한글 위키, 영문 위키
● Dagger2 는 Annotation 을 이용하여 Compile 단계에서 의존성 주입하는 구현체를 생성해주고 실행해주는 라이브러리
어노테이션
@Component : 실제 DI 주체 명시
@Module : DI 용 인스턴스를 제공하는 클래스 명시
@Inject : DI 되는 인스턴스 명시
@Provides : DI 인스턴스를 반환하는 메소드 명시
- @Named : Provides 와 Inject 관계의 명시적 선언- @Singleton : 싱글톤용 인스턴스 명시
Module, Provide@Module 은 DI에 필요한 인스턴스를 제공(@Provide)하는 Class 에 사용한다.
필요한 추가적 Module 선언- MovieListModule 에서 제공해주는 인스턴스 중에 ApiModule 에서 제공해주는 것이 필요하다는 것을 명시적으로 표시
DI 인스턴스 제공- MovieListModule 을 호출하는 곳에서
MovieListPresenter 를 필요로 할 때 사용- 참조 변수로 선언된 경우
MovieListPresenterImpl 내의 @Inject 가 선언된 생성자가 존재 해야 한다.
- 참조 변수가 없는 경우 new 로 선언해서 반환해도 된다.
ComponentDI 를 수행하는 Module 의 연관성을 선언하는 Interface
필요한 Module 선언- DI 에 필요한 인스턴스를 전달받는
Module 을 선언한다.
DI 대상 선언- 참조변수가 실제 DI 를 대상이다.- 메소드명은 의미 없음.
Module 의 실제 DI 객체 받기- DI 용 인스턴스를 별도로 접근해야 할 경우 사용
실제 동작 알아보기
Github 소스
실제 동작 알아보기Fragment(View)
Component(ListComponent)
Module1(ListModule)
Module2(ApiModule)
Presenter(PresenterImpl)
Component.build
Component.inject
실제 동작 알아보기Fragment(View)
Component(ListComponent)
Module1(ListModule)
Module2(ApiModule)
Presenter(PresenterImpl)
Component.build
Component.inject
Load Module2Load Module1
실제 동작 알아보기Fragment(View)
Component(ListComponent)
Module1(ListModule)
Module2(ApiModule)
Presenter(PresenterImpl)
Component.build
Component.inject
Load Module2Load Module1
Fragment.presenter = module.providePresenter
실제 동작 알아보기Fragment(View)
Component(ListComponent)
Module1(ListModule)
Module2(ApiModule)
Presenter(PresenterImpl)
Component.build
Component.inject
Load Module2Load Module1
Fragment.presenter = module.providePresenter
module1 .providePresenter
실제 동작 알아보기Fragment(View)
Component(ListComponent)
Module1(ListModule)
Module2(ApiModule)
Presenter(PresenterImpl)
Component.build
Component.inject
Load Module2Load Module1
Fragment.presenter = module.providePresenter
find @inject constructmodule1 .providePresenter
실제 동작 알아보기Fragment(View)
Component(ListComponent)
Module1(ListModule)
Module2(ApiModule)
Presenter(PresenterImpl)
Component.build
Component.inject
Load Module2Load Module1
Fragment.presenter = module.providePresenter
view = module1.get
find @inject constructmodule1 .providePresenter
module1.provideView
실제 동작 알아보기Fragment(View)
Component(ListComponent)
Module1(ListModule)
Module2(ApiModule)
Presenter(PresenterImpl)
Component.build
Component.inject
Load Module2Load Module1
Fragment.presenter = module.providePresenter
view = module1.get
module1.provideViewretrofit = module2.get
find @inject constructmodule1 .providePresenter
module2 .provideRetrofit
실제 동작 알아보기Fragment(View)
Component(ListComponent)
Module1(ListModule)
Module2(ApiModule)
Presenter(PresenterImpl)
Component.build
Component.inject
Load Module2Load Module1
Fragment.presenter = module.providePresenter
view = module1.get
module1.provideViewretrofit = module2.get
module2 .provideRetrofit
new PresenterImpl()
find @inject constructmodule1 .providePresenter
주의 사항1. DI 대상 - Component - Module 의 관계를 잘 설계 해야 컴파일시 에러가 나지 않는다.
○ DI 대상에 Component 내에 선언된 Module 에서 찾을 수 없는 provide 대상을 찾게 될 경우 에러가 발생한다.
2. Module 은 범위를 최소화 해야 Composition 화 하기 쉽다.○ Module 을 크게 생각해서 불필요한 provide 대상을 만들지 말자○ 특정 범위와 공용 범위의 Module 을 구분하자. (ex: Http용 Module - MVP Module)
3. 생성자에 @Inject 사용시 Module 에서 전달 받을 수 있는 객체를 참조변수로 사용한다.
4. @Inject, @Component, @Module, @Provide 가 모두 선언되어야만 컴파일 된다.
○ 중간 결과물을 보기 어렵기 때문에 최소한 4개의 관계를 모두 작성 후 컴파일 할 것!!
참고
Google Slide : https://docs.google.com/presentation/d/1fby5VeGU9CN8zjw4lAb2QPPsKRxx6mSwCe9q7ECNSJQ/pub?start=false&loop=false&delayms=3000&slide=id.p
Jakewharton Slide:http://jakewharton.com/presentation/2014-11-13-devoxx/