InsertKoinIO / koin-annotations

Koin Annotations - About Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform insert-koin.io
https://insert-koin.io
Apache License 2.0
163 stars 44 forks source link

Automatic scan for Modules in dependencies #174

Open tprochazka opened 1 month ago

tprochazka commented 1 month 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.

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?

arnaudgiuliani commented 1 month ago

Hello, interesting feedback. In your case, you don't use directly MyLibraryModule but it's installed in the singleton components, right?

tprochazka commented 1 month ago

Do you mean in hilt? Yes. It is in the example in the first post.