Dependency Injection in Android - Dagger Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi Everyone. This is Belal Khan and you   are watching Simplified Coding. In the last video, we learned a   manual approach of doing dependency  injection in our Android project.   And we did it by generating the  AppContainer and factories ourselves.   We also discussed the problem that we have with  the Manual Approach of Dependency Injection   and that is why we simplify the things we will use  Dagger for dependency injection in our project.   So the first question is what is Dagger? Dagger is a fully static compile-time   dependency injection framework  for java, kotlin, and Android.   As we discussed manual dependency injection  can be problematic if our app size grows.   To avoid or eliminate the complexity  of manual dependency injection   we can use Dagger to handle dependencies. Dagger works at compile-time that means it   automatically generates the required code  for dependency injection that we have to   write by hand if we do not use Dagger. The benefit of using Dagger are: it generates   the AppContainer automatically at compile-time  that we created manually in the last video.   It creates factories for the classes  available in the application graph   to satisfied dependencies. Dagger can decide whether to   reuse an existing instance or create a  new instance with the help of scopes.   And we can easily create containers for specific  flows in Dagger with minimal BoilerPlate code.   Now let's see a sample Use Case according to  the current project that we are working on.   In our current project, we have an authentication  flow and for this flow, we have AuthViewModel   that requires AuthRepository and AuthRepository  requires AuthApi and user preferences.   So Auth repo is a dependency for  ViewModel and AuthApi and user   preferences are the dependencies for Auth repo. Now here we have defined AuthViewModel like this   but currently, Dagger do not know how to create  AuthViewModel and that is why we need to tell the   dagger with the @(at the rate) inject annotation  that you have to create this dependency.   And now Dagger knows that it  has to build AuthViewModel.   But again AuthViewModel requires AuthRepository  and Dagger do not know how to build AuthRepository   and that is why again we need to tell  dagger with the help of @(at the rate)   inject annotation that Hey you have  to build this dependency as well.   I hope you got the point. After this, we have Dagger component that makes   a graph for dependencies to create a component  we just create an interface and annotate it with   @(at the rate) component inside the interface we  define the functions with the return type and rest   of the things Dagger will do automatically. Now when we will get the repository from the graph   it will give the new instance every time  but it is not necessary and sometimes we   require the same instance of the dependency. For example, we need a singleton instance   sometimes especially when we are using  the same instance in the many places   we can achieve this thing by scoping that means  we need to tell Dagger that Hey I need a singleton   instance of this thing and we can do it  with the help of singleton annotation.   Sometimes we need to create a dependency  that is required for a specific flow.   For example, we created the AuthViewModel for  the authentication flow in the last video.   We can achieve this thing by creating our own  custom scopes for a specific flow and we will   learn all these things in coming videos if you  found this video helpful and learned something.   From the next video, we will start  implementing Dagger in our project   meanwhile PLEASE do subscribe to my channel  if you are not already a subscriber and if   you want to get in touch with me then you can  follow me at Facebook, Instagram, or Twitter.   So thanks for watching Everyone. This is Belal Khan now signing off.
Info
Channel: Simplified Coding
Views: 4,773
Rating: 4.9024391 out of 5
Keywords: dagger android, dagger2, dagger 2 android, dependency injection android, dagger java, dagger dependency injection, dagger in android, google dagger, android dagger2, dagger 2 tutorial, java dagger, dagger android tutorial, dagger 2 android tutorial, dagger inject, daggerappcomponent, android dagger 2 tutorial, dagger 2 dependency, dagger 2 github
Id: PMYkSqvhPtA
Channel Id: undefined
Length: 4min 29sec (269 seconds)
Published: Sat Apr 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.