Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Dagger 2
Search
José Caique Oliveira
February 16, 2017
Programming
1
120
Dagger 2
Slides da palestra sobre Dagger 2
José Caique Oliveira
February 16, 2017
Tweet
Share
More Decks by José Caique Oliveira
See All by José Caique Oliveira
Kotlin Flow
jcaiqueoliveira
0
110
Coroutines And Flow
jcaiqueoliveira
2
100
Testing your app
jcaiqueoliveira
0
270
Modularizando seu app
jcaiqueoliveira
0
68
Nova Api de Localização Android
jcaiqueoliveira
0
72
Arquitetura para android
jcaiqueoliveira
6
320
Kotlin por onde começar?
jcaiqueoliveira
1
88
Introdução ao Android
jcaiqueoliveira
1
79
Arquitetura para projetos Android
jcaiqueoliveira
1
210
Other Decks in Programming
See All in Programming
月刊 競技プログラミングをお仕事に役立てるには
terryu16
1
1.2k
AWS Lambda functions with C# 用の Dev Container Template を作ってみた件
mappie_kochi
0
190
快速入門可觀測性
blueswen
0
500
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
400
Vue.jsでiOSアプリを作る方法
hal_spidernight
0
100
DevinとCursorから学ぶAIエージェントメモリーの設計とMoatの考え方
itarutomy
1
330
20241217 競争力強化とビジネス価値創出への挑戦:モノタロウのシステムモダナイズ、開発組織の進化と今後の展望
monotaro
PRO
0
310
SRE、開発、QAが協業して挑んだリリースプロセス改革@SRE Kaigi 2025
nealle
1
410
混沌とした例外処理とエラー監視に秩序をもたらす
morihirok
15
2.5k
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.4k
ISUCON14公式反省会LT: 社内ISUCONの話
astj
PRO
0
100
DMMオンラインサロンアプリのSwift化
hayatan
0
210
Featured
See All Featured
Writing Fast Ruby
sferik
628
61k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
39
1.9k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Statistics for Hackers
jakevdp
797
220k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
500
Become a Pro
speakerdeck
PRO
26
5.1k
Navigating Team Friction
lara
183
15k
BBQ
matthewcrist
85
9.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.2k
Transcript
Dagger 2 Caique Oliveira
Introdução 1
Introdução ◦ Inversão de controle ◦ Injeção de dependência
O que é ◦ Framework para injeção de dependência ◦
Fork do Dagger 1 ◦ JSR 330
Alguns ganhos ◦ Evita boilerplate ◦ Organização das dependências ◦
Fácil criação de testes
Vamos ao Dagger 2 2
Annotations ◦ Module ◦ Provides ◦ Component ◦ SubComponent ◦
Scope ◦ Inject ◦ Reusable ◦ Singleton
Module Identifica uma classe provedora de dependências @Module public class
LoginModule
Provides Identifica que um método é um provedor de dependência
@Module public class LoginModule { @Provides AuthenticationLayer providesAuthenticationLayer(Context context) { return new AuthenticationLayer(context); } }
Quem prover a dependência de um provider ?
Outro “provedor” @Module public class ApplicationModule { private Application application;
public ApplicationModule(Application application) { this.application = application; } @Provides Application providesApplication() { return application; } @Provides Context providesContext(Application application) { return application.getBaseContext(); } }
Component Define quais módulos podem ser utilizados e quem pode
utilizar tais módulos @Component(modules = { ApplicationModule.class}) public interface MainComponent { void inject(LoginActivity activity); }
SubComponent Repartição do grafo de dependências @ActivityScope @Subcomponent(modules = {LoginModule.class})
public interface ActivityComponent { void inject(LoginActivity activity); } @Component(modules = {ApplicationModule.class}) public interface MainComponent { ActivityComponent activityComponent(); FragmentComponent fragmentComponent(); }
Scopo Altera o ciclo de vida de um objeto @Scope
@Retention(RetentionPolicy.RUNTIME) public @interface ActivityScope { }
Reusable Indica que um dado objeto pode ser reutilizado @Module
public class ManagerModule { @Reusable @Provides DataController providesDataController(Context context) { return new DataController(context); } @Provides @Reusable AuthenticationLayer providesAuthenticationLayer(Context context, AuthenticationClient authenticationClient) { return new AuthenticationLayer(context,authenticationClient); } }
Singleton Indica que se precisa da mesma instância do objeto
@Module public class ManagerModule { @Reusable @Singleton DataController providesDataController(Context context) { return new DataController(context); } @Provides @Singleton AuthenticationLayer providesAuthenticationLayer(Context context, AuthenticationClient authenticationClient) { return new AuthenticationLayer(context,authenticationClient); } }
Inject Indica que é necessário injetar uma depêndencia public class
LoginActivity extends BaseActivity { @Inject public LoginPresenter loginPresenter; @Override public void onStart() { super.onStart(); ((Application) getApplication()).getMainComponent().activityComponent().inject(this); loginPresenter.bindView(this, this); } }
Inject Indica que é necessário injetar uma depêndencia public class
Application extends CoreApplication { private MainComponent mainComponent; @Override public void onCreate() { super.onCreate(); initDagger(); } private void initDagger() { mainComponent = DaggerMainComponent.builder().applicationModule(new ApplicationModule(this)).build(); } public MainComponent getMainComponent() { return mainComponent; } }
Refatorando para o Dagger 2 3
Refatorando - LoginPresenterImpl class LoginPresenterImpl implements LoginPresenter, OnLoginFinishedListener { private
Context context; private LoginView loginView; private LoginInteractor loginInteractor; LoginPresenterImpl(Context context, LoginView loginView) { this.context = context; this.loginView = loginView; this.loginInteractor = new LoginInteractorImpl(context); } ... }
Refatorando - LoginPresenterImpl class LoginPresenterImpl implements LoginPresenter, OnLoginFinishedListener { private
Context context; private LoginView loginView; private LoginInteractor loginInteractor; @Inject LoginPresenterImpl(LoginInteractor loginInteractor) { this.loginInteractor = loginInteractor; } @Override public void bindView(Context context, LoginView view) { this.context = context; this.loginView = view; } }
Refatorando - LoginInteractorImpl class LoginInteractorImpl implements LoginInteractor { private Context
context; LoginInteractorImpl(Context context) { this.context = context; } @Override public void authenticate(String email, String password, OnLoginFinishedListener listener) { //... new AuthenticationLayer(context).postAuthenticate(new AuthenticateRequest(email, password)); } @Override public void busAuthenticate(AuthenticateResponse authenticateResponse, OnLoginFinishedListener listener) { if (authenticateResponse.isSuccess()) { saveInformations(authenticateResponse); new UserManager().deleteAll(); new UserLayer(context).getUsers(); }else{ //... } } private void saveInformations(AuthenticateResponse authenticateResponse) { DataController dataController = new DataController(context); dataController.writeData(Constants.SharedPreferences.CONTROLLER_EMAIL, authenticateResponse.getEmail()); } }
Refatorando - LoginInteractorImpl class LoginInteractorImpl implements LoginInteractor { private UserLayer
userLayer; private Context context; private AuthenticationLayer authenticationLayer; private DataController dataController; private UserManager userManager; @Inject LoginInteractorImpl(Context context, AuthenticationLayer authenticationLayer, DataController dataController, UserManager userManager, UserLayer userLayer) { this.authenticationLayer = authenticationLayer; this.context = context; this.dataController = dataController; this.userManager = userManager; this.userLayer = userLayer; } … }
LoginModule @Module public class LoginModule { @Provides AuthenticationLayer providesAuthenticationLayer(Context context)
{ return new AuthenticationLayer(context); } @Provides @Reusable LoginInteractor providesLoginInteractor(Context context, AuthenticationLayer layer, DataController dataController, UserManager userManager, UserLayer userLayer) { return new LoginInteractorImpl(context, layer, dataController, userManager, userLayer); } @Provides @Reusable LoginPresenter providesLoginPresenter(LoginInteractor loginInteractor) { return new LoginPresenterImpl(loginInteractor); }
LoginModule @Module public class LoginModule { @Provides @Reusable DataController providesDataController(Context
context) { return new DataController(context); } @Provides @Reusable UserLayer providesUserLayer(Context context) { return new UserLayer(context); } @Provides @Reusable UserManager providesUserManager() { return new UserManager(); } }
Grafo de dependências Presenter Interactor UserManager AuthenticationLayer UserLayer DataController
Obrigado! Dúvidas?
Olá! Contate-me https://github.com/jcaiqueoliveira https://www.linkedin.com/in/caique-oliveira-43806895/
[email protected]