Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce boiler-plate for binding singletons where all constructors parameters are DI instances #408

Closed
rocketraman opened this issue Mar 9, 2022 · 4 comments

Comments

@rocketraman
Copy link
Contributor

Currently, binding singletons where all the constructor dependencies are coming from Kodein is quite verbose, and requires update every time the number of constructor parameters changes.

It would be nice if there were a shortened syntax to automatically use the primary constructor of a class, passing in the result of instance() for every parameter.

e.g.

// replace
bindSingleton { Foo(instance(), instance(), instance(), instance(), instance()) }
// with something like this
bindSingleton { ::Foo }

This feature is now available in Koin. See https://insert-koin.io/docs/reference/koin-core/dsl-update.

@rocketraman
Copy link
Contributor Author

Here is a gist that implements this for Kodein, based on the Koin implementation: https://gist.github.com/rocketraman/84302f408d34576052e07589e9e90b26.

@romainbsl
Copy link
Member

romainbsl commented Apr 22, 2022

Hi,
This is available with a SNAPSHOT version.

You can use it with le maven repository https://s01.oss.sonatype.org/content/repositories/snapshots/ under the version 8.0.0-ktor-2-SNAPSHOT.

You can now bind with

val di = DI {
   bindSingleton { new(::A) }
   bindSingleton { new(::B) }
}

I would like to introduced an API like

val di = DI {
   bindSingletonOf(::A)
   bindSingletonOf(::B)
}

But I have some crashes with Kotlin/JS and the heavy usage of inlined function typeOf.

@rocketraman
Copy link
Contributor Author

This is available with a SNAPSHOT version.

Great!

But I have some crashes with Kotlin/JS and the heavy usage of inlined function typeOf.

Hmm, have you reported this to YouTrack? Is that with the IR compiler, or legacy compiler, or both?

@romainbsl
Copy link
Member

It is an existing issue, that tackle the problem with a "don't use to many inline functions with typeOf()".

I will need to investigate on this, to know why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants