ABUSING
DEPENDENCY INJECTION
WITH DAGGER
FOR DUMMIES
Saúl Díaz (@sefford)
Freakend ‘15
new Flipper(new Gintonic(new Gin(new Alcohol())))
FlipperGintonic GinAlcohol
SHOULD I USE DI WITH MY
PERSONAL/PRO PROJECT?
BASIC DAGGER FOR
DUMMIES
Flipper
Gin
Alcohol
Gintonic
Tonic
Trolling
Code
Google
Engineer
Tears
Android
@Module(
includes = RosieModule.class,
addsTo = {AndroidModule.class,
DrinkModule.class,
CodingModule.class
}
injects = FlipperFragment.class,
complete = true,
)
public class FlipperModule {
@Provides
@Named(“Tuenti”)
public FlipperInterface provideFlipper(Gintonic drink,
Trolling trolling, Android android) {
return new FlipperImpl(drink, trolling,
android);
}
}
public class FlipperFragment extends Fragment {
ObjectGraph graph;
@Inject
FlipperInterface flipper;
@Override
public void onCreate(Bundle bundle) {
graph = ObjectGraph.create(new FlipperModule());
graph.inject(this);
FlipperInterface flipper2 = graph.get(Flipper.class);
}
IDSPISPOPD FOR SINGLETONS
@Singleton
● By definition, Singletons cannot be tested
● Provides same instance of class for Graph
● This instance is not static
ADVANCED DAGGER
FOR DUMMIES
Lazy<Flipper> lazyFlipper;
● Creates the instance on lazyFlipper.get()
● Returns always the same instance (d’oh)
● Always done in UIThread!
LAZY INITIALIZATIONS
PROVIDING INSTANCES
Provider<Flipper> flipperProvider;
● Can help building your own Flipper army
● Returns a different instance each time
@Module(
override = true;
injects = FlipperFragment.class,
complete = true,
)
public class FlipperTestModule {
@Provides
@Singleton
public FlipperInterface provideFlipper() {
return mock(FlipperInterface.class);
}
}
MOCKING INSTANCES
DYNAMIC (SCOPED) INJECTION
public ObjectGraph extend() {
return graph.plus(new ExtensionModule1(),
new ExtensionModule2());
}
STATIC INJECTION
● Reduce boilerplate and mantenibility by
annotating classes and constructions
● Compilation-validated graph
SCOPED INJECTION
● Using a SOLID approach, change
implementations on runtime
● Graph runtime, validated
● Risk of “BOOOOOOM”
ABUSING INJECTION
FOR DUMMIES
CLASSIC EXAMPLE
@Module(injects = LoginActivity.class,
addsTo = AppModule.class)
// @author Antonio Leiva
public class LoginModule {
private LoginView view;
public LoginModule(LoginView view) {
this.view = view;
}
@Provides @Singleton public LoginView provideView() {
return view;
}
@Provides @Singleton
public LoginPresenter providePresenter(LoginView loginView,
LoginInteractor loginInteractor) {
return new LoginPresenterImpl(loginView, loginInteractor);
}
}
WHY STOPPING THERE?public class NoNfcModule() {
@Provides
public NfcInterfaceController provideNfcController() {
return new NullNfcController();
}
}
public class NfcModule() {
@Provides
public NfcInterfaceController provideNfcController(
NfcAdapter adapter) {
return new NfcController(adapter);
}
}
// This goes on Api15Module
@Provides
@Singleton
public StatusBarControllerInterface provideStatusBarControllerInterface() {
return new StatusBarAPI15Controller();
}
// This goes on Api19Module
@Provides
@Singleton
public StatusBarControllerInterface provideStatusBarControllerInterface() {
return new StatusBarAPI19Controller();
}
// This goes on Api21Module
@Provides
@Singleton
public StatusBarControllerInterface provideStatusBarControllerInterface() {
return new StatusBarAPI21Controller();
}
ONE MORE CUP OF ABUSEpublic class ActiveProfileModule() {
@Provides
public ProfilePresenterInterface provideProfilePres(...) {
return new
ActiveProfilePresenter(basePresenter,...);
}
}
public class RegularProfileModule() {
@Provides
public ProfilePresenterInterface provideProfilePres(...) {
return new
RegularProfilePresenter(basePresenter,...);
}
}
QUESTIONS?
THANKS!

More Related Content

PDF
Dagger 2. Right way to do Dependency Injection
PPTX
Dagger 2. The Right Way to Dependency Injections
PPTX
Антон Минашкин "Dagger 2. Right way to do Dependency Injections"
PDF
Advanced Dagger talk from 360andev
PPTX
Sword fighting with Dagger GDG-NYC Jan 2016
PDF
Dagger 2 vs koin
PPTX
Angular2 + rxjs
PPTX
Introduction to CDI and DI in Java EE 6
Dagger 2. Right way to do Dependency Injection
Dagger 2. The Right Way to Dependency Injections
Антон Минашкин "Dagger 2. Right way to do Dependency Injections"
Advanced Dagger talk from 360andev
Sword fighting with Dagger GDG-NYC Jan 2016
Dagger 2 vs koin
Angular2 + rxjs
Introduction to CDI and DI in Java EE 6

What's hot (20)

PDF
Sharper Better Faster Dagger ‡ - Droidcon SF
PDF
Dependency Injection with CDI in 15 minutes
PPTX
PDF
Architecting your GWT applications with GWT-Platform - Lesson 02
PDF
CDI: How do I ?
PDF
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
PPTX
Open sourcing the store
PDF
Тестирование на Android с Dagger 2
PDF
What’s new in Android JetPack
PDF
Guice tutorial
PDF
Using hilt in a modularized project
PDF
Under the Hood: Using Spring in Grails
PPTX
Angular Workshop_Sarajevo2
ZIP
継承だろJK
PDF
Intro to Retrofit 2 and RxJava2
PDF
Workshop 26: React Native - The Native Side
PDF
Di code steps
PDF
MCE^3 - Gregory Kick - Dagger 2
PDF
Android Design Patterns
PDF
Dependency injection with dagger 2
Sharper Better Faster Dagger ‡ - Droidcon SF
Dependency Injection with CDI in 15 minutes
Architecting your GWT applications with GWT-Platform - Lesson 02
CDI: How do I ?
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Open sourcing the store
Тестирование на Android с Dagger 2
What’s new in Android JetPack
Guice tutorial
Using hilt in a modularized project
Under the Hood: Using Spring in Grails
Angular Workshop_Sarajevo2
継承だろJK
Intro to Retrofit 2 and RxJava2
Workshop 26: React Native - The Native Side
Di code steps
MCE^3 - Gregory Kick - Dagger 2
Android Design Patterns
Dependency injection with dagger 2
Ad

Viewers also liked (20)

PPTX
Danielle\'s Visual Resume
PPSX
PPT
Hot skills исследование страховых компаний_2011
PDF
งานนำเสนอ1
PDF
สนใจ ไม่ได้แปลว่าได้ใจ
PPT
McCormack -EDIM510- Online Presentation Assignment
POT
Sampleslideshow
PPTX
Danielle\'s Visual Resume
KEY
2012.7.31 第二回 Stage3D 勉強会「モデリングを始めてみよう」
PPTX
In kor we Trust
PDF
Open im vol16_lt_hosi_factory
PPT
PPTX
Mirsis Corporate Overview
PPTX
Presentatie Astorium
PPT
PDF
Agile2015 short paper presentation: Development of Complex Software with Agil...
PDF
Online Training @ Nitade Chula
PDF
Subsalt Steep Dip Imaging Study with 3D Acoustic Modeling
PDF
2015 Tech Predictions
PPTX
Mirsis Autocontrol İç Kontrol Yönetimi
Danielle\'s Visual Resume
Hot skills исследование страховых компаний_2011
งานนำเสนอ1
สนใจ ไม่ได้แปลว่าได้ใจ
McCormack -EDIM510- Online Presentation Assignment
Sampleslideshow
Danielle\'s Visual Resume
2012.7.31 第二回 Stage3D 勉強会「モデリングを始めてみよう」
In kor we Trust
Open im vol16_lt_hosi_factory
Mirsis Corporate Overview
Presentatie Astorium
Agile2015 short paper presentation: Development of Complex Software with Agil...
Online Training @ Nitade Chula
Subsalt Steep Dip Imaging Study with 3D Acoustic Modeling
2015 Tech Predictions
Mirsis Autocontrol İç Kontrol Yönetimi
Ad

Similar to Dagger for dummies (20)

PDF
guice-servlet
PPTX
Google Plus SignIn : l'Authentification Google
PDF
Google GIN
PDF
Testable Android Apps using data binding and MVVM
PDF
Building maintainable app #droidconzg
PDF
It's complicated, but it doesn't have to be: a Dagger journey
PPTX
Dependency injection using dagger2
PPTX
Dependency Injection for Android
PPTX
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
PDF
Building maintainable app
PDF
Microservices Architecture: Labs
PDF
Building a Native Camera Access Library - Part II.pdf
PDF
Solid angular
PPTX
從零開始學 Android
PDF
Popup view on Mortar
PDF
Google App Engine in 40 minutes (the absolute essentials)
PPTX
Avoiding and dealing with conflicting updates in Oak
PPTX
Using and contributing to the next Guice
PDF
Dicoding Developer Coaching #30: Android | Mengenal Macam-Macam Software Desi...
guice-servlet
Google Plus SignIn : l'Authentification Google
Google GIN
Testable Android Apps using data binding and MVVM
Building maintainable app #droidconzg
It's complicated, but it doesn't have to be: a Dagger journey
Dependency injection using dagger2
Dependency Injection for Android
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Building maintainable app
Microservices Architecture: Labs
Building a Native Camera Access Library - Part II.pdf
Solid angular
從零開始學 Android
Popup view on Mortar
Google App Engine in 40 minutes (the absolute essentials)
Avoiding and dealing with conflicting updates in Oak
Using and contributing to the next Guice
Dicoding Developer Coaching #30: Android | Mengenal Macam-Macam Software Desi...

Recently uploaded (20)

PDF
Multiverse AI Review 2025: Access All TOP AI Model-Versions!
PPTX
Trending Python Topics for Data Visualization in 2025
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PPTX
Tech Workshop Escape Room Tech Workshop
DOCX
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
PDF
CCleaner 6.39.11548 Crack 2025 License Key
PDF
Website Design Services for Small Businesses.pdf
PDF
Autodesk AutoCAD Crack Free Download 2025
DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
PDF
Microsoft Office 365 Crack Download Free
PPTX
GSA Content Generator Crack (2025 Latest)
PDF
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
assetexplorer- product-overview - presentation
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PDF
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
PDF
Visual explanation of Dijkstra's Algorithm using Python
PDF
Types of Token_ From Utility to Security.pdf
Multiverse AI Review 2025: Access All TOP AI Model-Versions!
Trending Python Topics for Data Visualization in 2025
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Tech Workshop Escape Room Tech Workshop
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
CCleaner 6.39.11548 Crack 2025 License Key
Website Design Services for Small Businesses.pdf
Autodesk AutoCAD Crack Free Download 2025
How to Use SharePoint as an ISO-Compliant Document Management System
Microsoft Office 365 Crack Download Free
GSA Content Generator Crack (2025 Latest)
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Computer Software and OS of computer science of grade 11.pptx
assetexplorer- product-overview - presentation
Monitoring Stack: Grafana, Loki & Promtail
Weekly report ppt - harsh dattuprasad patel.pptx
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
Visual explanation of Dijkstra's Algorithm using Python
Types of Token_ From Utility to Security.pdf

Dagger for dummies

  • 1. ABUSING DEPENDENCY INJECTION WITH DAGGER FOR DUMMIES Saúl Díaz (@sefford) Freakend ‘15
  • 2. new Flipper(new Gintonic(new Gin(new Alcohol()))) FlipperGintonic GinAlcohol
  • 3. SHOULD I USE DI WITH MY PERSONAL/PRO PROJECT?
  • 6. @Module( includes = RosieModule.class, addsTo = {AndroidModule.class, DrinkModule.class, CodingModule.class } injects = FlipperFragment.class, complete = true, ) public class FlipperModule { @Provides @Named(“Tuenti”) public FlipperInterface provideFlipper(Gintonic drink, Trolling trolling, Android android) { return new FlipperImpl(drink, trolling, android); } }
  • 7. public class FlipperFragment extends Fragment { ObjectGraph graph; @Inject FlipperInterface flipper; @Override public void onCreate(Bundle bundle) { graph = ObjectGraph.create(new FlipperModule()); graph.inject(this); FlipperInterface flipper2 = graph.get(Flipper.class); }
  • 8. IDSPISPOPD FOR SINGLETONS @Singleton ● By definition, Singletons cannot be tested ● Provides same instance of class for Graph ● This instance is not static
  • 10. Lazy<Flipper> lazyFlipper; ● Creates the instance on lazyFlipper.get() ● Returns always the same instance (d’oh) ● Always done in UIThread! LAZY INITIALIZATIONS
  • 11. PROVIDING INSTANCES Provider<Flipper> flipperProvider; ● Can help building your own Flipper army ● Returns a different instance each time
  • 12. @Module( override = true; injects = FlipperFragment.class, complete = true, ) public class FlipperTestModule { @Provides @Singleton public FlipperInterface provideFlipper() { return mock(FlipperInterface.class); } } MOCKING INSTANCES
  • 13. DYNAMIC (SCOPED) INJECTION public ObjectGraph extend() { return graph.plus(new ExtensionModule1(), new ExtensionModule2()); }
  • 14. STATIC INJECTION ● Reduce boilerplate and mantenibility by annotating classes and constructions ● Compilation-validated graph
  • 15. SCOPED INJECTION ● Using a SOLID approach, change implementations on runtime ● Graph runtime, validated ● Risk of “BOOOOOOM”
  • 17. CLASSIC EXAMPLE @Module(injects = LoginActivity.class, addsTo = AppModule.class) // @author Antonio Leiva public class LoginModule { private LoginView view; public LoginModule(LoginView view) { this.view = view; } @Provides @Singleton public LoginView provideView() { return view; } @Provides @Singleton public LoginPresenter providePresenter(LoginView loginView, LoginInteractor loginInteractor) { return new LoginPresenterImpl(loginView, loginInteractor); } }
  • 18. WHY STOPPING THERE?public class NoNfcModule() { @Provides public NfcInterfaceController provideNfcController() { return new NullNfcController(); } } public class NfcModule() { @Provides public NfcInterfaceController provideNfcController( NfcAdapter adapter) { return new NfcController(adapter); } }
  • 19. // This goes on Api15Module @Provides @Singleton public StatusBarControllerInterface provideStatusBarControllerInterface() { return new StatusBarAPI15Controller(); } // This goes on Api19Module @Provides @Singleton public StatusBarControllerInterface provideStatusBarControllerInterface() { return new StatusBarAPI19Controller(); } // This goes on Api21Module @Provides @Singleton public StatusBarControllerInterface provideStatusBarControllerInterface() { return new StatusBarAPI21Controller(); }
  • 20. ONE MORE CUP OF ABUSEpublic class ActiveProfileModule() { @Provides public ProfilePresenterInterface provideProfilePres(...) { return new ActiveProfilePresenter(basePresenter,...); } } public class RegularProfileModule() { @Provides public ProfilePresenterInterface provideProfilePres(...) { return new RegularProfilePresenter(basePresenter,...); } }