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.
- Create
UserRepositoryclass. - Create
EmailServiceclass. - Create
UserRegistrationServiceclass. - Add
registerUserfunction inUserRegistrationServiceclass. - Use manual dependency injection by constructor injection.
- Run the app.
- Add Dagger 2 dependency to your project.
- Create a component interface for
UserRegistrationService. - Annotate the component interface with
@Component. - Add
@Injectannotation to the constructor ofUserRegistrationService,UserRepository, andEmailService. @Injecttells Dagger 2 that the class can be injected.- Get the instance of
UserRegistrationServiceby callingDaggerUserRegistrationServiceComponent.builder().build().getUserRegistrationService(). - Run the app.
- Address the inefficiency of creating too many objects in the component interface.
- Use
@Injectannotation for field injection to solve this problem. - Add
@Injectannotation to the fields ofUserRegistrationService,UserRepository, andEmailService. - Pass the consumer class to the component interface.
- Get the instance of
UserRegistrationServiceby callingDaggerUserRegistrationServiceComponent.builder().build().inject(this). - Run the app.
- Introduce the need for more flexible and reusable code.
- Create
EmailServiceProviderandUserRepositoryProviderinterfaces. - Use
@Moduleand@Providesannotations to provideEmailServiceandUserRepositoryinstances.
- Understand the difference between
@Bindsand@Provides. @Bindsis used for interfaces and abstract classes, while@Providesis used for concrete classes.- Refactor the module to use
@Bindsinstead of@Provides.
- Handle scenarios where multiple services are needed, like sending SMS and sending Email.
- Use
@Namedannotation to differentiate between the two services. - Create a custom annotation with
@Qualifier,@Documented, and@Retentionannotations to avoid typo errors.
- Deal with situations where objects need to be created at runtime, not compile time.
- Create a
Moduleclass for runtime object creation. - Use
@Providesannotation to tell Dagger to create objects at runtime. - Implement the
@Component.Factoryinterface in the component interface to pass dynamic values at runtime.
- Make objects singleton to save memory space.
- Use
@Singletonannotation on the component, class, or method to make it singleton. - Address the problem of component-level singletons by creating custom scopes with
@Scopeannotation.
- Use different services in specific activities or fragments, not in the entire application.
- Create an
AnalyticsServicewith Application scope. - Introduce a new
AppComponentwithAnalyticsModulefor application-level services. - Create a relationship between
AppComponentandActivityComponent.
- Access all objects without defining them in the application-level component.
- Use subcomponents in Dagger to create dependent components.
- Replace
@Componentwith@Subcomponentin the dependent component. - Define the subcomponent relationship in the parent component.
- Learn how to create a component using
@Component.Builder. - Understand the differences between
@Component.Factoryand@Component.Builder. - Implement a component using
@Component.Builderfor projects requiring multiple parameters.
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!