--
Dependency Injection
What is Dependency Injection?
Ans- In software engineering, dependency injection is a technique in which an object receives other objects that it depends on. These other objects are called dependencies. … The “injection” refers to the passing of a dependency (a service) into the object (a client) that would use it.
Dagger2
Dagger 2 is a compile-time android dependency injection framework that uses Java Specification Request 330 and Annotations. Some of the basic annotations that are used in dagger 2 are:
- @Module This annotation is used over the class which is used to construct objects and provide the dependencies.
- @Provides This is used over the method in the module class that will return the object.
- @Inject This is used over the fields, constructor, or method and indicates that dependencies are requested.
- @Component This is used over a component interface that acts as a bridge between @Module and @Inject. (Module class doesn’t provide dependency directly to requesting class, it uses component interface)
- @Singleton This is used to indicate only a single instance of dependency object is created.
Mode of Injection in Dagger2
- Constructor Injection: Injecting the method parameters.
- Field Injection: Injecting the member variable (must not be private).
- Method Injection: Injecting the method parameter.
KOIN
Koin is a pragmatic lightweight DSL dependency injection framework for Kotlin. Koin is easy, simple & well documented. It is perfect for encapsulated feature/library modules.
Koin is based around its DSL which consists of these keywords:
startKoin() — call module to start it in application
module { } — create a Koin module or a submodule (inside a module)
factory { } — provide a factory bean definition
single { } — provide a bean definition
get() — resolve a component dependency (eager)
inject() — resolve a component dependency in Android components runtime
bind — additional Kotlin type binding for a given bean definition
getProperty() — resolve a property
DAGGER HILT
The new Hilt library defines a standard way to do DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically for you.
The hilt is currently in alpha but it’s already quite powerful.
But in Hilt, we don’t need to create a component, include every module, and build for generating DaggerAppComponent class.
What are the main components of Hilt?
@HiltAndroidApp: We need to apply this annotation to our application class, which triggers the code generation and creates the base component.
@AndroidEntryPoint: Hilt creates a dependency container to the Android component that it was assigned so it can inject the dependencies.
@Module: This is used on the classes that create the objects of dependency classes (for the classes that you don’t own — for example, third-party library classes like OkHttp or Retrofit).
@InstallIn: The Hilt module class should also be annotated with @InstallIn to specify the scope of the module. For example, if we annotate the module with @InstallIn(ActivityComponent::class), then the module will bind to the activity lifecycle.
@Provides: This is used on the methods that are inside the module class and provides the dependency objects.
Advantages of DI?
Ans- Reduced Dependency Carrying, More Reusable Code, More Testable Code, More Readable Code.
the basic difference between Dagger2 & Koin is the Error handling and Build time.
From the above explanations, All the DI have the same functional requirements but the differences are very few. so we can use it According to need or choice.
ref- geeksforgeeks, MindOrks, TechInsider