Hi All . My name is murad and i'm a software developer .
this is an introduction to DI. i'll explain DI in Dagger2 step by step . inspired by : Mindork
Every Commit will be contains a step in how to create instances and provide those to who needs them.
- Description
- Technologies
- Old Way: I create what I need
- I Get What I need
- Someone Will give me What I need
this is a clean example to DI and will be guide to Dagger2 in android.(KOTLIN)
- Network Service. needs (apiKey , context)
- Database Service. needs (context , database name , version)
- MainView Model. needs ( network service , database service)
- MainActivity. needs ( viewmodel )
Project is created with:
- KOTLIN
- Dagger2
in first commit you will find the old way . ( when i need object I WILL CREATE IT ). so the viewmodel will create new instances from network service and database service. and the main activity will create instance from viewmodel.
we moved the creation of the instances (services) outside the viewmodel and we passes the Conf values of the service from outside each service, so now the code is more configurable and testable.!
we created new singleton class(object) -- Injector -- to manage our dependencies, this is the first step to start digging in the dagger and DI libraries world. this object contains 2 inject methods. the first one will inject the services instances in the application class. the second will inject the instances from the application into the viewmodel and inject the viewmodel in the mainactivity.
Dagger Main Architecture:
- Module(A class that provides-create the needed dependencies)
- Component(Dependency Manager a class will inject the consumers with their dependencies)
- Consumer (Classes has dependencies -they have fields from another classes-)
- Scope (The Dependency lifetime)
- Qualifier( if we had 2 provides method with same return type we need to distinguish between them using qualifier (Annotations)).
Main Annotations:
- @Module
- @Component
- @Provides
- @Inject
- @Qualifier
In the first commit we removed the inject(for the application) method from the injector class and we used the dagger2 to inject the services into the application.
We created Application module(@Module) that will provide 2 (@Singleton) services (Network , Database).
In the second commit we removed the Injector object and we used dagger2 to inject the viewmodel in the main activity. (Note : we added dependency in the activity component because the view model needs (network and database)services and they already been provided from application module).
in the first and second commit we used (Method injection using @provides). next we will use Constructor injection.
we used the constructor injection. we removed provides methods for the services and viewmodel and created new provides for the fields needed in both services. and since the apiKey and DatabaseName both strings we created new Qualifiers so dagger can inject each property with it's method.