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
UserRepository
class. - Create
EmailService
class. - Create
UserRegistrationService
class. - Add
registerUser
function inUserRegistrationService
class. - 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
@Inject
annotation to the constructor ofUserRegistrationService
,UserRepository
, andEmailService
. @Inject
tells Dagger 2 that the class can be injected.- Get the instance of
UserRegistrationService
by callingDaggerUserRegistrationServiceComponent.builder().build().getUserRegistrationService()
. - Run the app.
- Address the inefficiency of creating too many objects in the component interface.
- Use
@Inject
annotation for field injection to solve this problem. - Add
@Inject
annotation to the fields ofUserRegistrationService
,UserRepository
, andEmailService
. - Pass the consumer class to the component interface.
- Get the instance of
UserRegistrationService
by callingDaggerUserRegistrationServiceComponent.builder().build().inject(this)
. - Run the app.
- Introduce the need for more flexible and reusable code.
- Create
EmailServiceProvider
andUserRepositoryProvider
interfaces. - Use
@Module
and@Provides
annotations to provideEmailService
andUserRepository
instances.
- Understand the difference between
@Binds
and@Provides
. @Binds
is used for interfaces and abstract classes, while@Provides
is used for concrete classes.- Refactor the module to use
@Binds
instead of@Provides
.
- Handle scenarios where multiple services are needed, like sending SMS and sending Email.
- Use
@Named
annotation to differentiate between the two services. - Create a custom annotation with
@Qualifier
,@Documented
, and@Retention
annotations to avoid typo errors.
- Deal with situations where objects need to be created at runtime, not compile time.
- Create a
Module
class for runtime object creation. - Use
@Provides
annotation to tell Dagger to create objects at runtime. - Implement the
@Component.Factory
interface in the component interface to pass dynamic values at runtime.
- Make objects singleton to save memory space.
- Use
@Singleton
annotation on the component, class, or method to make it singleton. - Address the problem of component-level singletons by creating custom scopes with
@Scope
annotation.
- Use different services in specific activities or fragments, not in the entire application.
- Create an
AnalyticsService
with Application scope. - Introduce a new
AppComponent
withAnalyticsModule
for application-level services. - Create a relationship between
AppComponent
andActivityComponent
.
- Access all objects without defining them in the application-level component.
- Use subcomponents in Dagger to create dependent components.
- Replace
@Component
with@Subcomponent
in 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.Factory
and@Component.Builder
. - Implement a component using
@Component.Builder
for 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!