This library is very similar to dagger-2 library, but it generates kotlin files (*.kt)
instead of java files (*.java)
. It uses apt
to generate components and factories.
You can create a component interface with modules
and dependencies
fileds (@Component.Factory
is optional):
@Component(
modules = [MainModule::class, DatabaseModule::class, RepositoryModule::class],
dependencies = [DataDependencies::class]
)
interface MainComponent : ProfileDependencies, GlobalConfigDependencies {
fun getSubcomponent(): MainSubcomponent
fun getTransactionViewModel(): TransactionViewModel
fun getSampleConfig(): GlobalConfig
fun inject(view: TransactionView)
@Component.Factory
interface Factory {
fun createNewInstance(
@BindsInstance nConfig: NetworkConfig,
dataDependencies: DataDependencies,
@BindsInstance dConfig: DatabaseConfig,
mainModule: MainModule
): MainComponent
}
}
You can use a module with @Provide
annotated methods:
@Module
class DatabaseModule {
@Singleton
@Provide
fun getDatabase(): Database {
return Database()
}
}
Yuu can use a module with @Binds
annotated methods:
@Module
interface RepositoryModule {
@Binds
fun provideSampleRepository(impl: CardRepositoryImpl): CardRepository
@Binds
fun provideTransactionRepository(impl: TransactionRepositoryImpl): TransactionRepository
}
You can create a subcomponent interface with a modules
filed:
@Subcomponent(modules = [ParameterModule::class])
interface MainSubcomponent : ProfileDependencies {
fun getTransactionViewModel(): TransactionViewModel
fun getParameterConfig(): ParameterConfig
}
You can use @Inject
in constructors:
class TransactionRepositoryImpl @Inject constructor(
private val config: GlobalConfig,
private val profile: ProfileModel
) : TransactionRepository {
override fun getTransactions(card: CardModel): List<TransactionModel> {
return if (config.transactionAvailable && profile.id.isNotBlank()) {
when (card.id) {
"1" -> {
listOf(
TransactionModel("1"),
TransactionModel("2")
)
}
"2" -> {
listOf(
TransactionModel("3"),
TransactionModel("4")
)
}
else -> emptyList()
}
} else emptyList()
}
}
You can use @Inject
in fileds:
class TransactionView {
@Inject
lateinit var transactionViewModel: TransactionViewModel
@Inject
lateinit var accountViewModel: AccountViewModel
}
Gradle:
repositories {
maven {
url "https://dl.bintray.com/dvpermyakov/maven"
}
}
dependencies {
implementation 'com.dvpermyakov:dagger-kotlin:0.0.2'
}