dave08
01/31/2018, 3:45 AM@Provides
, only when I ran in terminal, I saw it, now everything works! Your 'did you see `kaptGenerateStubs`' question made me look there ... 😊gildor
01/31/2018, 3:48 AM@Provides
dave08
01/31/2018, 3:51 AMgildor
01/31/2018, 3:51 AMdave08
01/31/2018, 3:53 AMgildor
01/31/2018, 4:42 AMdave08
01/31/2018, 7:47 AMgildor
01/31/2018, 7:52 AM@Provides
@Named("some_config_property")
fun provideSomeProperty(config: MyConfigInterface): String {
return config.someProperty()
}
and use it on client side:
class SomeClientClass @Inject constructor(@Named("some_config_property") someProperty: String)
dave08
01/31/2018, 8:46 AMprovideDbClient
that has lots of `System.getenv("...")`s in the constructor, also I run a login function in an xmlrpc dep. in provide XmlrpcClient
, but I was wondering if that should also be abstracted in factory classes? Or maybe that's part of the point of DI is to take care of providing objects that are ready for use...
Also, I'm still not clear on when to use inject on a constructor, if I still have to write a provider function for it...gildor
01/31/2018, 8:51 AMSystem.getenv
in a class constructor or anywhere in your business logic. You can do that in DI (module) or in some special config implementation, something like MyConfigEvnVarsImplementation
when to use inject on a constructorThere are different opinions about that. Imo
@Inject constructor
is really useful, reduces boilerplate. Also you can use it even in case of polymorphic dependency with @Binds
annotation on module method. So our internal guideline: use @Inject constructor
as much is possible and use provide method when it’s impossible to do or you want to override some dependency.
But some people has opposite guidelines, to use only provide methods, but I’m not sure that “keep everything in one place” is good reason.I have aFor example in your case I would use constructor inject for you DbClien (probably withthat has lots ofprovideDbClient
System.getenv("...")
@Bind
if client is polymorphic) and to provide all client config properties use some class with configuration properties:
class XmlrpcClient @Inject constructor(config: DbConfig) // Probably use named config instead
class SystemEnvDbConfig @Inject constructor() // And just init instance using System.getenv in constructor
interface MyDbModule {
@Binds fun provideDbClient(impl: XmlrpcClient): DbClient
@Binds fun provideDbConfig(impl: SystemEnvDbConfig): DbConfig
}
@Provides
method that constructs DbClient using System.getenv