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
gildor
01/31/2018, 3:49 AMdave08
01/31/2018, 3:51 AMgildor
01/31/2018, 3:51 AMgildor
01/31/2018, 3:52 AMdave08
01/31/2018, 3:53 AMgildor
01/31/2018, 4:42 AMgildor
01/31/2018, 4:45 AMdave08
01/31/2018, 7:47 AMgildor
01/31/2018, 7:52 AMgildor
01/31/2018, 7:53 AMgildor
01/31/2018, 7:58 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
gildor
01/31/2018, 8:53 AMgildor
01/31/2018, 8:56 AMwhen 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.gildor
01/31/2018, 9:03 AMI 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
}
gildor
01/31/2018, 9:05 AMgildor
01/31/2018, 9:08 AM@Provides
method that constructs DbClient using System.getenv