I would just like to preface this by saying that this is not a "which is better" post; this is strictly a question about how I can build something using Dagger (and how I built it in Kodein to help illustrate the problem).
I've been using Kodein for a few years now in several work projects, and I've found it to be so easy to work with, that I never look at Dagger anymore. I started a new personal project, and I thought I'd give Dagger another shot.
To keep things simple, I have 3 modules (this is a regular desktop app not an Android one);
- app
- common
app
contains a single class App
:
class App(
private val api: GoogleApi,
private val argParser: ArgParser
) {
fun run() {
while(true) {
api.login(argParser.username, argParser.password);
}
}
}
common
contains a single class ArgParser
(implementation isn't important)
google
contains several classes:
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() = ...
}
The dependencies for google
are:
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"
}
I specifically use implementation
because I don't want anyone using the underlying Google libraries directly.
To get this to work in Kodein, I do the following in 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()
}
then in 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()
}
}
and finally in common
:
fun commonModule(args: Array<String>) = Kodein.Module("Common") {
bind<ArgParser>() with singleton { ArgParser(args = args) }
}
I tried implementing this in Dagger, and couldn't get it to work. My first attempt was to have a Component
in app
that relied on modules from common
and google
. This didn't work, because the generated code referenced classes that weren't exposed from google
(like Drive
). I could've fixed this by making them api
dependencies, but I don't want to expose them:
// 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()
}
Next I tried making a component per module (gradle module that is):
// 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
}
Then in app
the fun started:
// results in "AppComponent (unscoped) cannot depend on scoped components:"
@Component(dependencies = [CommonComponent::class, GoogleComponent::class])
interface AppComponent {
fun app(): App
}
OK so let's make it scoped:
// results in "This @Singleton component cannot depend on scoped components:"
@Singleton
@Component(dependencies = [CommonComponent::class ,GoogleComponent::class])
interface AppComponent {
fun app(): App
}
EDIT: tried making AppComponent
use a custom scope:
// results in "AppComponent depends on more than one scoped component:"
@AppScope
@Component(dependencies = [CommonComponent::class ,GoogleComponent::class])
interface AppComponent {
fun app(): App
}
How can I achieve this in Dagger? I've read the docs, I think I somewhat understand them, but I have no clue what to do next.