Dependency Injection

In Mobile Jazz, we defined our own dependency injection pattern. We use it in all the language that we are currently supporting and allows us to have a standarize way. It has three main concepts: Provider, Component and Module.

Provider

A provider is a file that contains a component and a module. For example, it can be a feature or a topic that it's part of your application.

Component

A component is an interface that expose the public contract of your module.

Module

A module is a class that implements a component and specify how we are going to supply each component. A module has a constructor to provide every dependency that it needs. Also, inside the module we can build every other dependency that we need in order to supply the component.

Find an example in Kotlin:

All this code is inside a Provider named BikeProvider.
interface BikeComponent {
fun getBikeInteractor(): GetBikeInteractor
fun getBikesInteractor(): GetBikesInteractor
}
class BikeDefaultModule(private val coroutineScope: CoroutineScope,
private val networkConfiguration: NetworkConfiguration,
private val oauthComponent: OAuthComponent,
private val cacheDatabase: CacheDatabase,
private val otp: OTP) : BikeComponent {
override fun getBikesInteractor(): GetBikesInteractor =
GetBikesInteractor(coroutineScope, getBikes, oauthComponent.getPasswordTokenInteractor(), getSelectedBikeID)
override fun getBikeInteractor(): GetBikeInteractor =
GetBikeInteractor(coroutineScope, GetInteractor(coroutineScope, bikeRepository), oauthComponent.getPasswordTokenInteractor(), getSelectedBikeID, getBikeAdapterInteractor)
}
private val bikeRepository: RepositoryMapper<BikeEntity, Bike> by lazy {
val getNetworkDataSource = GetNetworkDataSource(
networkConfiguration.bikesURL,
networkConfiguration.httpClient,
BikeEntity.serializer(),
networkConfiguration.json
)
val putNetworkDataSource = PutNetworkDataSource(
networkConfiguration.bikesURL, networkConfiguration.httpClient, BikeEntity.serializer(),
networkConfiguration.json
)
val deleteNetworkDataSource = DeleteNetworkDataSource(
networkConfiguration.bikesURL,
networkConfiguration.httpClient
)
val singleDataSourceRepository = SingleDataSourceRepository(getNetworkDataSource, putNetworkDataSource, deleteNetworkDataSource)
return@lazy RepositoryMapper(singleDataSourceRepository, singleDataSourceRepository, singleDataSourceRepository, BikeEntityToBikeMapper,
BikeToBikeEntityMapper)
}