this project is inspired by Koin and for learning purposes GDK Jakarta 2019
In software engineering, dependency injection is a technique whereby one object supplies the dependencies of another object. A "dependency" is an object that can be used, for example as a service. Instead of a client specifying which service it will use, something tells the client what service to use. The "injection" refers to the passing of a dependency (a service) into the object (a client) that would use it. The service is made part of the client's state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.
In simple word Dependency Injection is passing dependency to other objects or framework with some techniques.
- Reusability of code
- Ease of refactoring
- Ease of testing
Dependencies supplied as parameters
class EngineX
data class Plane(private val engine: EngineX)
fun main() {
val engine = EngineX()
// Constructor Injection
Plane(engine)
}
Dependencies injected by accessing properties of object
class EngineX
class Plane {
lateinit var enginex: EngineX
}
fun main() {
val enginex = EngineX()
val plane = Plane()
//Property Injection
plane.enginex = enginex
}
Dependencies to be obtained from super class function that implemented by child of super class
interface SuperEngine
class EngineX: SuperEngine
class ShipEngine: SuperEngine
abstract class Vehicle {
//Method Injection
private val enginex: SuperEngine = getEngine()
protected abstract fun getEngine(): SuperEngine
}
class Plane : Vehicle() {
override fun getEngine(): SuperEngine {
return EngineX()
}
}
class Ship : Vehicle() {
override fun getEngine(): SuperEngine {
return ShipEngine()
}
}
- Constructor Injection
- Property Injection
- release-0.2 Injection with qualifier
- release-0.1 The simplest
- Android
- GSON
- add qualifier 3 December 2019
- created 1 December 2019 tobe update