A K Night With Dagger
A K Night With Dagger
A K Night With Dagger
with Dagger
Slicing problems of
newbies with DI
Why Dagger is important nowadays ?
Like you love to watch Thor
with his Hammer
Interviewers watch for Dagger in
you
What is Dependency Injection ?
Please don’t react this way!
Ok, What is Dependency ?
Dependency
● Class Black needs object from class Red,
used.
And, What is Injection ?
To inject something..
So, what is dependency injection ?
Dependency Injection
● Injection: Passing of one or more objects
to the depending class
● Dependency Injection: A form of
Inversion of Control, a design principle,
which say that any class should be getting
its dependencies from outside.
● In other words, class should not be
instantiating other class within it, but rather
there should be another configuration
class which can provide with those
instances.
So, what does that mean ? You can...
class in runtime.
● Drawbacks:
○ Performance Overhead
○ Exposure of Internals
○ java.lang
○ java.lang.reflect
● Class class:
● Typically, you are not gonna use DI for injecting just one dependency
Dagger 1 Dagger 2
What is Annotation ?
Annotation is...
● But we were not known to the same capability of reading metadata at compile
● Are code generators that eliminate boilerplate code by generating code for
● Even you can create your own Annotations and a Processor for it using this
stage.”
● Hence, Dagger2 is free from Performance Overhead. Also, Dagger2 errors are
highly traceable.
Modes of Dependency Injection
Modes of DI
● Java Specification Request 330 (JSR 330) has defined standard Java
● Modes of DI:
○ Field Injection: Injecting the member variable (Required that it must not be private)
constructor arguments.
Dependency
Graph
Make sure to add below Dependencies
dependencies {
implementation 'com.google.dagger:dagger:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
}
Annotations with Dagger2
dependencies
method
injection
Let's start with Component
Component is...
● Best Practise: Exposes only your top level dependency. Internal dependencies
RandomUserServiceModule.
on.
● Our modules, now, are just lacking with the dependency of Context.
So, my module’s @Module annotation got updated with includes attribute as:
@Module(includes = OkHttpClientModule.class)
public class RandomUserServiceModule {}
@Module(includes = OkHttpClientModule.class)
public class PicassoModule {}
@Module(includes = ContextModule.class)
public class OkHttpClientModule {}
By now,
our modules are connected and can
communicate to each other.
We are simply remaining to tell our
Component that
which Modules you need to depend on..
Component annotation has modules attribute
@Component(modules = {RandomUserServiceModule.class,
PicassoModule.class})
public interface RandomUserApplicationComponent {
...
}
Now, hit gradle build…
And if you are lucky...
injected.
● How do we tell Dagger to create just a single instance for any dependency ?
There should be something to control instance
creations
Limit the instances by Scope
Scope...
@Provides
@RandomUserApplicationScope
public Retrofit retrofit(OkHttpClient okHttpClient,
GsonConverterFactory gsonConverterFactory) {}
Typically, we need two type of Contexts:
Application and Activity. We can have
ActivityModule to provide Activity
context.
● But Dagger gets confused which Context to use because it has got two modules
that has methods that provide Context and it will throw error on build.
● How can we tell Dagger that this dependency should use Application Context or
Activity Context ?
Named Annotations comes in...
@Named annotation does this job
In ContextModule: In ActivityModule:
@Provides @Provides
@RandomUserApplicationScope @RandomUserApplicationScope
@Named("application_context") @Named("activity_context")
public Context context() { public Context context() {
return context.getApplicationContext(); return context;
} }
Tell Dagger to use Application context as
In OkHttpModule: In PicassoModule:
@Provides @Provides
@RandomUserApplicationScope @RandomUserApplicationScope
public File public Picasso
file(@Named("application_context") picasso(@Named("application_conte
Context context) {} xt") Context context,
OkHttp3Downloader
okHttp3Downloader) {}
Alternative to Named Annotations
@Qualifier is of my type
Create an interface as: Use it in OkHttpModule and PicassoModule:
@Qualifier @Provides
public @interface ApplicationContext { @RandomUserApplicationScope
} public File file(@ApplicationContext Context
context) {}
@Provides
@RandomUserApplicationScope @Provides
@ApplicationContext @RandomUserApplicationScope
public Context context() { public Picasso picasso(@ApplicationContext
return context; Context context, OkHttp3Downloader
} okHttp3Downloader) {}
Till now...
● We have created Application Level Dependencies
● But what about some dependencies that we need just on Activity Level ?
itself.
● Best practises: When you are injecting dependencies into clients who have
life cycle different from where dependencies are coming, it's better to
● We will apply this scope to component and modules we will create next
looking for any other dependencies, you can look into components specified in
RandomUserApplicationComponent
And we have activity-scoped MainActivityModule
Now, check changes in Activity and Application class
A lot more manageable code base has
been established by now...
That’s it…???!!!
What if I have 50 dependencies in your component ?
randomUserService = mainActivityComponent.randomUserService();
randomUserAdapter = mainActivityComponent.randomUserAdapter();
...
Well, I am lazy at this and we are already learning lazy loading of objects.
You might think..
“Ohh..
I don’t care.
I am good at writing utility classes... ”
Well, This is not my style… Guess why...
@Inject is waiting for you...
annotations.
● But to use the magic of @Inject, we will need to modify our code a little.
Modify MainActivityComponent as
return type, “Omg… there’s no return type…!!! There must be something I need
to look into this class. Ohh yes, I can see fields annotated with @Inject, let me
initialize them”
- Chintan Soni
Chintan Soni
Senior Android Developer @
Simform Solutions Pvt. Ltd.
Follow me on:
FB: chintansoni202
Twitter: @chintansoni202