-
-
Notifications
You must be signed in to change notification settings - Fork 60
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
Feature Request: Support Provider-like API for Instance Creation #457
Comments
Mmm, maybe this feature request is not necessary, am I wrong or I can just create a new instance using a lambda? class BigCoffeeMaker(private val filterProvider: () -> Filter) {
} |
Yes you can use a lambda |
Nice, is this documented anywhere? I thought it was a missing feature because I couldn't find how to do it |
It's under https://github.com/evant/kotlin-inject?tab=readme-ov-file#function-support--assisted-injection I do understand the documentation can be improved, feel free to add your feedback to #389 |
Sure, done: #389 (comment) |
Use
Provider<T>
to enable flexible and explicit creation of new instances of a dependency directly within the injection target. This avoids boilerplate factory definitions in the component and allows injection targets to control when and how many instances of a dependency are created.Currently, kotlin-inject supports creating new instances through factory methods in
@Component
but, as far as I know, there is no built-in equivalent for aProvider<T>
-like API directly available in injection targets.Proposed Solution
Introduce a
Provider<T>
-like interface into kotlin-inject that can be injected into classes. This would allow injection targets to request new instances of a dependency explicitly and on demand.The proposed interface:
The kotlin-inject compiler would generate providers automatically for any class or dependency annotated with
@Inject
. TheProvider<T>
would invoke the binding logic forT
each timeget()
is called.Example: New Instance Creation in Injection Target
Here’s an example adapted to kotlin-inject:
Target Class
Filter Class
AppComponent
Main Function
Benefits
Eliminates the need for repetitive factory definitions in components or manual object creation in injection targets.
The text was updated successfully, but these errors were encountered: