InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.98k stars 712 forks source link

Automatic scan for Modules in dependencies #2007

Closed tprochazka closed 5 days ago

tprochazka commented 5 days ago

I'm migrating from Hilt and it has one great feature that is missing in Koin.

Inside the library, you can create a module that will be part of the default singleton component.

@Module
@InstallIn(SingletonComponent.class)
public final class MyLibraryModule {

When you add that library to the project, that library will automatically and without any setup provide implementations for all objects defined in that module.

In Koin you need to add a module to the Koin setup closure manually. Would be great to have as part of the Koin Annotation project the possibility to do something similar, scan for all modules in all libraries used in the project and somehow marked as public (shared). Libraries still can have their own internal modules.

To explain motivation for that. We have many libraries in the company and setting up them is quite complex and tedious work. I would like to make it plug and play. For example, one library will provide OkHttpClient and all other libraries can just use it by injection, without necessary to call something like init(okHttpClient) for every library. Or when some library needs configuration, it can just expose the interface and use the injection, and the app using that library will be responsible to provide the implementation. If you do not provide it, it will crash during compile time and you will immediately know that you miss something. Or tracking, one library can provide just a general interface for tracking and all other libraries will use just this interface. In the app I will just add a dependency with tracking implementation, like Firebase, and done, it will immediately start tracking. Or I can simply decide that I will do my own tracking directly in the app, so I will just provide my own implementation of that tracking interface in the app.

tprochazka commented 5 days ago

Hilt/Dagger automatically generates a special package in the library with all the information about what provides and needs. And during the compilation, KSP checks everything in that one package on the classpath, so it will very fastly get information from all libraries.

But there will be an issue that Koin does not have @Inject annotation so it doesn't know during the compile name all od the requested dependencies that need to be fulfilled, right?

tprochazka commented 5 days ago

I moved it here https://github.com/InsertKoinIO/koin-annotations/issues/174