SlideShare a Scribd company logo
Dependency injection using dagger 2
Dependency Injection Using Dagger 2
Mahmoud El-Naggar
Content
1. Dependency Injection Definition and Importance
2. Dependency Inversion Principle (SOLID Principles)
3. What and Why Dagger 2 ?
4. Building Dependency Graph
Dependency Injection Definition and Importance
Definition : Object defines and Supply dependencies for
another Object.
It is implementation of the Inversion of Control Principal(IoC)
that state each class must get its dependencies from outside.
Importance :
1- Decouple the objects from each others and make the Classes
more abstracted.
2- simplifying the Unit Test
The Problem.
Here the problem is Car class
depend on Engine Class so they
are tightly coupled , if we want
to change the Engine we have
to change the Car too
Dependency injection using dagger 2
The Solution
Here instead of initialize the Engine Object
Inside the Car , We Inject it to the Car from
Outside.
We delegated the creation of the Engine
Object to another object so we can
inject any Engine object even mock objects
Dependency Inversion Principle (DIP)
This principle means class depends on abstraction not
implementation.
The problem.
Let's say we want to change the
Engine to GasEngine ,then we have
to change Car class and replace all
The Engine to GasEngine
The Solution.
According to DIP ,Class depend
On Abstractions not the implementations
The Solution.
What is Dagger 2 ?
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.
Building Dependency Graph (Version +2.10)
1. Dagger Flow
2. Injecting Activity object
3. Scoped Objects
4. Qualifiers
Building Dependency Graph
1. Make a Module class annotated with @Module Annotation
2. Annotate each method that return a dependency with @Provide
Annotation
3. Make an Interface annotated with @Component
4. Specify which Module will be used with this Component interface
5. Use @Inject Annotation to inject dependency when needed
Subcomponents
Subcomponents are components that inherit and extend the object
graph of a parent component. You can use them to partition your
application’s object graph into subgraphs either to encapsulate
different parts of your application from each other or to use more
than one scope within a component.
1. Declaring a subcomponent
2. Adding a subcomponent to a parent component
Injecting Activity object
1. Install AndroidInjectionModule in your application component to ensure
that all bindings necessary for these base types are available.
1. Start off by writing a @Subcomponent that extends
AndroidInjector<YourActivity>, with a @Subcomponent.Builder that
extends AndroidInjector.Builder<YourActivity>
1. After defining the subcomponent, add it to your component hierarchy by
defining a module that binds the subcomponent builder and adding it to the
component that injects your Application:
4. Next, make your Application implement HasActivityInjector and @Inject
a DispatchingAndroidInjector<Activity> to return from the
activityInjector() method:
4. Finally, in your Activity.onCreate() method, call
AndroidInjection.inject(this) before calling super.onCreate()
Scoped Objects
Keep the objects among defined scope
Scope are user defined annotations like ActivityScope ,UserScope and
ApplicationScope
Qualifiers
Dagger may not be able to disguish between returned objec from same Type
Like ‘return context’ or ‘return getApplicationContext’
References
1. http://tutorials.jenkov.com/dependency-injection/index.html
2. https://google.github.io/dagger/
3. https://dzone.com/articles/an-introduction-to-dagger-2-android-di-part-1-
3?fromrel=true
4. https://www.youtube.com/playlist?list=PLuR1PJnGR-Ih-
HXnGSpnqjdhdvqcwhfFU
5. https://www.youtube.com/playlist?list=PLuR1PJnGR-
IgeDuzxoGe3hHV_8OfbHy8c
Time for Questions :)

More Related Content

PDF
Android Develpment vol. 2, MFF UK, 2015
PDF
Writing testable Android apps
PPTX
An Introduction to Dependency Inversion Principle
PPTX
Android Dagger 2
PDF
Angular2 with type script
PPTX
How to implement dependency injection in c#
PPTX
Dependency injection: koin vs dagger
PPTX
Dependency injection using dagger2
Android Develpment vol. 2, MFF UK, 2015
Writing testable Android apps
An Introduction to Dependency Inversion Principle
Android Dagger 2
Angular2 with type script
How to implement dependency injection in c#
Dependency injection: koin vs dagger
Dependency injection using dagger2

Similar to Dependency injection using dagger 2 (20)

PDF
Building blocks of Angular
PPTX
mobile development using node js and java
PDF
Di code steps
PDF
Dagger for android
PDF
Spring Framework
ODP
Angular js-crash-course
PDF
MPointInc_AndroidSDK_Documentation
PPTX
React JS Interview Question & Answer
PPTX
Introduction to dependency injection in Scala (Play)
PPTX
Dependency injection using Google guice
PPTX
Dependency injection and inversion
PDF
Dependency injection with dagger 2
PPT
17612235.ppt
PPTX
Android architecture
ODP
Angular2
PDF
Dependency Injection
PDF
Fundamental concepts of react js
PPTX
Implementing the Adapter Design Pattern
PPTX
Dependency Injection або Don’t call me, I’ll call you
PPTX
Dependency Injection in .NET
Building blocks of Angular
mobile development using node js and java
Di code steps
Dagger for android
Spring Framework
Angular js-crash-course
MPointInc_AndroidSDK_Documentation
React JS Interview Question & Answer
Introduction to dependency injection in Scala (Play)
Dependency injection using Google guice
Dependency injection and inversion
Dependency injection with dagger 2
17612235.ppt
Android architecture
Angular2
Dependency Injection
Fundamental concepts of react js
Implementing the Adapter Design Pattern
Dependency Injection або Don’t call me, I’ll call you
Dependency Injection in .NET
Ad

Recently uploaded (20)

PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
medical staffing services at VALiNTRY
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
System and Network Administraation Chapter 3
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Transform Your Business with a Software ERP System
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Nekopoi APK 2025 free lastest update
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
history of c programming in notes for students .pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Digital Strategies for Manufacturing Companies
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
medical staffing services at VALiNTRY
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
System and Network Administraation Chapter 3
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Computer Software and OS of computer science of grade 11.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Transform Your Business with a Software ERP System
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Nekopoi APK 2025 free lastest update
Design an Analysis of Algorithms II-SECS-1021-03
Internet Downloader Manager (IDM) Crack 6.42 Build 41
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
history of c programming in notes for students .pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Digital Strategies for Manufacturing Companies
CHAPTER 2 - PM Management and IT Context
Which alternative to Crystal Reports is best for small or large businesses.pdf
Ad

Dependency injection using dagger 2

  • 2. Dependency Injection Using Dagger 2 Mahmoud El-Naggar
  • 3. Content 1. Dependency Injection Definition and Importance 2. Dependency Inversion Principle (SOLID Principles) 3. What and Why Dagger 2 ? 4. Building Dependency Graph
  • 4. Dependency Injection Definition and Importance Definition : Object defines and Supply dependencies for another Object. It is implementation of the Inversion of Control Principal(IoC) that state each class must get its dependencies from outside. Importance : 1- Decouple the objects from each others and make the Classes more abstracted. 2- simplifying the Unit Test
  • 5. The Problem. Here the problem is Car class depend on Engine Class so they are tightly coupled , if we want to change the Engine we have to change the Car too
  • 7. The Solution Here instead of initialize the Engine Object Inside the Car , We Inject it to the Car from Outside. We delegated the creation of the Engine Object to another object so we can inject any Engine object even mock objects
  • 8. Dependency Inversion Principle (DIP) This principle means class depends on abstraction not implementation.
  • 9. The problem. Let's say we want to change the Engine to GasEngine ,then we have to change Car class and replace all The Engine to GasEngine
  • 10. The Solution. According to DIP ,Class depend On Abstractions not the implementations
  • 12. What is Dagger 2 ? 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.
  • 13. Building Dependency Graph (Version +2.10) 1. Dagger Flow 2. Injecting Activity object 3. Scoped Objects 4. Qualifiers
  • 14. Building Dependency Graph 1. Make a Module class annotated with @Module Annotation 2. Annotate each method that return a dependency with @Provide Annotation 3. Make an Interface annotated with @Component 4. Specify which Module will be used with this Component interface 5. Use @Inject Annotation to inject dependency when needed
  • 15. Subcomponents Subcomponents are components that inherit and extend the object graph of a parent component. You can use them to partition your application’s object graph into subgraphs either to encapsulate different parts of your application from each other or to use more than one scope within a component. 1. Declaring a subcomponent 2. Adding a subcomponent to a parent component
  • 16. Injecting Activity object 1. Install AndroidInjectionModule in your application component to ensure that all bindings necessary for these base types are available. 1. Start off by writing a @Subcomponent that extends AndroidInjector<YourActivity>, with a @Subcomponent.Builder that extends AndroidInjector.Builder<YourActivity> 1. After defining the subcomponent, add it to your component hierarchy by defining a module that binds the subcomponent builder and adding it to the component that injects your Application:
  • 17. 4. Next, make your Application implement HasActivityInjector and @Inject a DispatchingAndroidInjector<Activity> to return from the activityInjector() method: 4. Finally, in your Activity.onCreate() method, call AndroidInjection.inject(this) before calling super.onCreate()
  • 18. Scoped Objects Keep the objects among defined scope Scope are user defined annotations like ActivityScope ,UserScope and ApplicationScope
  • 19. Qualifiers Dagger may not be able to disguish between returned objec from same Type Like ‘return context’ or ‘return getApplicationContext’
  • 20. References 1. http://tutorials.jenkov.com/dependency-injection/index.html 2. https://google.github.io/dagger/ 3. https://dzone.com/articles/an-introduction-to-dagger-2-android-di-part-1- 3?fromrel=true 4. https://www.youtube.com/playlist?list=PLuR1PJnGR-Ih- HXnGSpnqjdhdvqcwhfFU 5. https://www.youtube.com/playlist?list=PLuR1PJnGR- IgeDuzxoGe3hHV_8OfbHy8c