Hi, does any one knows a good dependency container...
# announcements
a
Hi, does any one knows a good dependency container to use in Kotlin for backend projects? Good means: work without any configuration and extra plumbing code in trivial cases. E.g. val di = Container.scanAllProject(); di.get(SomeService::class) would create and return a singleton of class "SomeService" with autowiring/resolving constructor injected dependencies automatically.
r
I use Koin everywhere I can use Koin (JVM & Android)
a
but for Koin there is a need to specify each class which will be created?
r
Of course, what else do you want to do?
s
Kodein is another DI framework. If I'm not mistaken, it supports multi-platform Kotlin
s
His question implies auto discovery
r
Does that exist?
c
Spring, you still need annotations though. There is no magic 🙂
s
Well, you could use ie. https://github.com/classgraph/classgraph to find all subclasses of an interface and register them in your DI framework assuming it allows that on runtime.
t
Just noticed that Koin uses neither annotation processing nor reflection ... what does it use then ? DSL configuration at runtime ?
a
Yes, I'm thinking about using classgraph and then somehow call e.g. Koin or Kodein and register all found classes, but then It will need to register also all classes (dependencies) which appears on constructor
and on their constructors also
which seems is what I'm searching for, but using jitpack it not publishing any classes in .jar https://jitpack.io/#davidwhitney/kotlinject/52dbace6b7
s
Maybe you can use classgraph to find all classes which have a constructor with
@Inject
annotation (if that is what Koin etc. uses)
a
Koin seems is designed with reified inline functions in mind
so I don't know how to use their API in runtime
Looks like currently there is only spring container
t
Have you considered just not using a DI container?
a
yes, it's best approach and we're using just object with by lazy properties, but with a smaller project
t
It scales fine to larger projects too
a
in current project there is a need to have some type of "auto discover" of datasources commands mixins without explicit registration
s
declaring dependencies in koin feels close to Spring's JavaConfig: val depModule = module(createOnStart = true) { single<CaseClient> { RESTCaseClient(get()) } single<TaskClient> { RESTTaskClient(get(), get()) } ... }
so not very much auto discovery going on here
s
We use Koin on a server-side project but Im due to rip it out. Koin seems to be a good fit for Android where you dont have full control over how your app is insitated but outside of that it's horrid imo.
a
I'm currently testing Kotlinject and it seems best of all, it's port from .NET Ninject If someone also want to try it you can use jitpack: https://jitpack.io/#antanas-arvasevicius/Kotlinject/3e06747737 because original repository has only misconfigured maven and is not usable with jitpack.
c
IFoo -> Foo naming convention
🤬
😂 1
a
yes.. see sharp 😃
managed to initialize magic container:
Copy code
private val container = Container().apply {
    registrations.scan
        .fromClasspathWhere({ true }) {
            it.bindClassesAndInterfaces(
                condition = null,
                lifecycle = Lifecycle.Singleton
            )
        }
}
usage: val datasource = container.resolve(UserDataSource::class)
c
tripple backticks for multi-line snippets
Copy code
one
two
three
a
ty
👍 1
j
fwiw, guice does this kind of 'just in time' binding by default if it can. recommend kotlin-guice for doing
injector.Instance<SomeService>()
instead of having to do a
::class.java
call.
a
how can I be forgotten about guice..
thanks @Joe I'll investigate if it fits our needs
I'd thought that guice always need to define everything explicit
I generally dislike annotation based configurations which guice suggests e.g.
@Inject @Named("SMSComms")
j
yeah we mostly just
@Inject
primary constructors and use modules to specify anything more that needs it, with a few exceptions where we need
@Named
to qualify things