Skip to content

Learn Dagger 2 step-by-step! Master dependency injection in Android development. From manual injection to advanced topics like custom scopes and subcomponents, our comprehensive tutorials cover it all. Level up your skills and build efficient, well-organized apps.

Notifications You must be signed in to change notification settings

ProHussain/DependencyInjectionTraining

Repository files navigation

Dagger in Android Development - Step by Step Tutorials

This repository contains a series of tutorials on how to use Dagger in Android development. Each training lecture builds upon the previous one to gradually introduce Dagger concepts and best practices.

Training 1 - Manual Dependency Injection

  1. Create UserRepository class.
  2. Create EmailService class.
  3. Create UserRegistrationService class.
  4. Add registerUser function in UserRegistrationService class.
  5. Use manual dependency injection by constructor injection.
  6. Run the app.

Training 2 - Introducing Dagger 2

  1. Add Dagger 2 dependency to your project.
  2. Create a component interface for UserRegistrationService.
  3. Annotate the component interface with @Component.
  4. Add @Inject annotation to the constructor of UserRegistrationService, UserRepository, and EmailService.
  5. @Inject tells Dagger 2 that the class can be injected.
  6. Get the instance of UserRegistrationService by calling DaggerUserRegistrationServiceComponent.builder().build().getUserRegistrationService().
  7. Run the app.

Training 3 - Field Injection with Dagger

  1. Address the inefficiency of creating too many objects in the component interface.
  2. Use @Inject annotation for field injection to solve this problem.
  3. Add @Inject annotation to the fields of UserRegistrationService, UserRepository, and EmailService.
  4. Pass the consumer class to the component interface.
  5. Get the instance of UserRegistrationService by calling DaggerUserRegistrationServiceComponent.builder().build().inject(this).
  6. Run the app.

Training 4 - Using Interfaces with @Provides and @Module

  1. Introduce the need for more flexible and reusable code.
  2. Create EmailServiceProvider and UserRepositoryProvider interfaces.
  3. Use @Module and @Provides annotations to provide EmailService and UserRepository instances.

Training 5 - @Binds vs. @Provides

  1. Understand the difference between @Binds and @Provides.
  2. @Binds is used for interfaces and abstract classes, while @Provides is used for concrete classes.
  3. Refactor the module to use @Binds instead of @Provides.

Training 6 - Custom Annotations and @Qualifier

  1. Handle scenarios where multiple services are needed, like sending SMS and sending Email.
  2. Use @Named annotation to differentiate between the two services.
  3. Create a custom annotation with @Qualifier, @Documented, and @Retention annotations to avoid typo errors.

Training 7 - Creating Objects at Runtime with @Component.Factory

  1. Deal with situations where objects need to be created at runtime, not compile time.
  2. Create a Module class for runtime object creation.
  3. Use @Provides annotation to tell Dagger to create objects at runtime.
  4. Implement the @Component.Factory interface in the component interface to pass dynamic values at runtime.

Training 8 - @Singleton Scope and Custom Scopes

  1. Make objects singleton to save memory space.
  2. Use @Singleton annotation on the component, class, or method to make it singleton.
  3. Address the problem of component-level singletons by creating custom scopes with @Scope annotation.

Training 9 - Component Dependencies and Scopes

  1. Use different services in specific activities or fragments, not in the entire application.
  2. Create an AnalyticsService with Application scope.
  3. Introduce a new AppComponent with AnalyticsModule for application-level services.
  4. Create a relationship between AppComponent and ActivityComponent.

Training 10 - Subcomponents in Dagger

  1. Access all objects without defining them in the application-level component.
  2. Use subcomponents in Dagger to create dependent components.
  3. Replace @Component with @Subcomponent in the dependent component.
  4. Define the subcomponent relationship in the parent component.

Training 11 - Component Builders

  1. Learn how to create a component using @Component.Builder.
  2. Understand the differences between @Component.Factory and @Component.Builder.
  3. Implement a component using @Component.Builder for projects requiring multiple parameters.

Usage

Please follow the tutorials in numerical order, starting with Training 1 and progressing to Training 11. The code for each training session is available in its respective directory.

Feel free to explore the code and adapt it to your specific project needs. If you encounter any issues or have questions, don't hesitate to reach out.

If you are looking for professional App development services or need a free consultation for your project, I invite you to visit my Fiverr profile. I offer high-quality and reliable services to help you bring your app ideas to life. Whether you need assistance with Android development, UI/UX design, or any other mobile app related task, I'm here to help. Let's collaborate and create something amazing together! Visit my Fiverr profile at Hussain Sahir to get started.

Happy Daggering!

About

Learn Dagger 2 step-by-step! Master dependency injection in Android development. From manual injection to advanced topics like custom scopes and subcomponents, our comprehensive tutorials cover it all. Level up your skills and build efficient, well-organized apps.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages