Auto-generate components and more #139
Replies: 2 comments
-
Been thinking more about moving @Provides
@AppScope
fun apiService(): ApiService = ...
...
@Provides
@TestScope
fun testApiService(): ApiService = ...
...
@AppScope
@Component
interface AppComponent {
val main: Main // can only access @AppScope provides
}
@TestScope
@Component
interface TestComponent {
val main: Main // can only access @TestScope provides
} an unscoped There would then be a separate annotation to make the component retain the instance: @Provides
@AppScope
@Retain
fun json(): Json = ... In theory this would be enough to not even have provides defined inside a component. Pros
Cons
Open Questions
|
Beta Was this translation helpful? Give feedback.
-
Unfortunately after looking into it I don't think there's a good way to get it to work. The problem is you can't easily find top-level |
Beta Was this translation helpful? Give feedback.
-
I would like to extend the @dave08 idea combined with what are doing other libraries like
PopKorn
or this Hilt library.Summary
ViewModel
injection.1. Auto-generate components based on some annotation.
Similar to
PopKorn
or theHilt library
I mentioned before, they avoid the creation of components or modules. An example is better to explain that.And that's it. No modules, no components.
I don't know if this magic can be done with the same exact annotations that currently exist (
Inject
,Provides
, and so on). Probably for a simple setup it is possible but for a difficult setup, no. For example, multiple inherency, and so on.For the basic use case, based on
Inject
andProvides
, I would like to suggest:And, if possible, for providing third-party dependencies I would use top-level functions:
Under the hood,
kotlin-inject
will generate the components. Becausekotlin-inject
doesn't provide components, we should create empty interfaces, for example, as Hilt does, we can have:Because KMP is coming soon, custom annotations are going to be necessary (I don't know what are the plans for KMP, maybe expect/actual using javax
Inject
annotation in the JVM part, but for this idea, it is necessary to add more annotations andInject
will not be enough.2. Create a specific Android module that helps with the
ViewModel
injection.A pluggable solution to injecting
ViewModel
should be great (maybe with Compose this becomes useless, but maybe adding support to scoping to the Navigation library as Hilt does could be great too).3. Create a specific module that provides an opinionated bundle of components for popular platforms like Hilt does in Android.
I think it is self-explanatory, the previous approaches let you focus on injecting dependencies and not on how to deal with the DI framework, so this is the last thing to just inject dependencies and doing no more things.
I think Android should be easy in the way it is possible to just copy the Hilt behavior, but I would like to see this feature in the future in other frameworks like desktop JVM apps, native apps (iOS, macOS...), and so on.
Beta Was this translation helpful? Give feedback.
All reactions