我只想说这不是一篇“哪个更好”的帖子。这严格来说是一个关于如何使用 Dagger 构建某些东西的问题(以及我如何在 Kodein 中构建它以帮助说明问题)。
几年来,我在几个工作项目中一直在使用 Kodein,我发现它很容易使用,以至于我不再看 Dagger。我开始了一个新的个人项目,我想再给 Dagger 一次机会。
为简单起见,我有 3 个模块(这是一个普通的桌面应用程序,而不是 Android 应用程序);
- 应用程序
- 常见的
- 谷歌
app
包含一个类App
:
class App(
private val api: GoogleApi,
private val argParser: ArgParser
) {
fun run() {
while(true) {
api.login(argParser.username, argParser.password);
}
}
}
common
包含一个类ArgParser
(实现并不重要)
google
包含几个类:
class GoogleApi(
driveProvider: () -> Drive
) {
private val drive by lazy {
driveProvider()
}
fun login(username: String, password: String) {
drive.login() // not real call
}
}
internal class CredentialRetriever(
private val transport: NetHttpTransport,
private val jsonFactory: JacksonFactory
) {
fun retrieveCredentials() = ...
}
的依赖项google
是:
dependencies {
implementation "com.google.api-client:google-api-client:$googleApiVersion"
implementation "com.google.oauth-client:google-oauth-client-jetty:$googleApiVersion"
implementation "com.google.apis:google-api-services-drive:v3-rev110-$googleApiVersion"
}
我特别使用implementation
,因为我不希望任何人直接使用底层的谷歌库。
为了让它在 Kodein 中工作,我在main
:
fun main(args: Array<String>) {
val kodein = Kodein {
import(commonModule(args = args))
import(googleModule)
import(appModule)
bind<App>() with singleton {
App(
api = instance(),
argParser = instance()
)
}
}
kodein.direct.instance<App>().run()
}
然后在google
:
val googleModule = Kodein.Module("Google") {
bind<CredentialRetriever>() with provider {
CredentialRetriever(jsonFactory = instance(), transport = instance())
}
bind<Drive>() with provider {
Drive.Builder(
instance(),
instance(),
instance<CredentialRetriever>().retrieveCredentials()
).setApplicationName("Worker").build()
}
bind<GoogleApi>() with singleton {
GoogleApi(drive = provider())
}
bind<JacksonFactory>() with provider {
JacksonFactory.getDefaultInstance()
}
bind<NetHttpTransport>() with provider{
GoogleNetHttpTransport.newTrustedTransport()
}
}
最后在common
:
fun commonModule(args: Array<String>) = Kodein.Module("Common") {
bind<ArgParser>() with singleton { ArgParser(args = args) }
}
我尝试在 Dagger 中实现它,但无法让它工作。我的第一次尝试是依赖于Component
和的模块。这不起作用,因为生成的代码引用了未从(如)公开的类。我可以通过使它们成为依赖项来解决这个问题,但我不想公开它们:app
common
google
google
Drive
api
// CredentialRetriever and GoogleApi were updated to have @Inject constructors
// GoogleApi also got an @Singleton
@Module
object GoogleModule {
@Provides
internal fun drive(
transport: NetHttpTransport,
jsonFactory: JacksonFactory,
credentialRetriever: CredentialRetreiver
): Drive =
Drive.Builder(
transport,
jsonFactory,
credentialRetriever.retrieveCredentials()
).setApplicationName("Worker").build()
@Provides
internal fun jsonFactory(): JacksonFactory =
JacksonFactory.getDefaultInstance()
@Provides
internal fun netHttpTransport(): NetHttpTransport =
GoogleNetHttpTransport.newTrustedTransport()
}
接下来我尝试为每个模块制作一个组件(即gradle模块):
// in google module
@Singleton
@Component(modules = [GoogleModule::class])
interface GoogleComponent {
fun googleApi(): GoogleApi
}
// in common module
@Singleton
@Component(modules = [CommonModule::class])
interface CommonComponent {
fun argParser(): ArgParser
}
然后在app
有趣的开始:
// results in "AppComponent (unscoped) cannot depend on scoped components:"
@Component(dependencies = [CommonComponent::class, GoogleComponent::class])
interface AppComponent {
fun app(): App
}
好的,让我们将其设为范围:
// results in "This @Singleton component cannot depend on scoped components:"
@Singleton
@Component(dependencies = [CommonComponent::class ,GoogleComponent::class])
interface AppComponent {
fun app(): App
}
编辑:尝试AppComponent
使用自定义范围:
// results in "AppComponent depends on more than one scoped component:"
@AppScope
@Component(dependencies = [CommonComponent::class ,GoogleComponent::class])
interface AppComponent {
fun app(): App
}
我怎样才能在 Dagger 中实现这一点?我已经阅读了文档,我想我对它们有所了解,但我不知道下一步该做什么。