[AssistedInject] Support Generic Factory
See original GitHub issueRight now we use a Factory and provider to inject generic factories:
I.e:
class ViewModelFactory<VM : ViewModel>
@Inject constructor(private val provider: Provider<VM>) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T = provider.get() as T
}
@Inject lateinit var factory: ViewModelFactory<MyViewModel>
This works great in many places, I was trying to do the same with the assisted factory to achieve something similar, but the AssistedFactory
requires a 1-1 mapping from what I can tell and will return an error if there is no matching @AssistedInject
@AssistedFactory
interface StateViewModelFactory<VM : ViewModel> {
fun create(handle: SavedStateHandle): VM
}
/// An assisted factory's abstract method must return a type with an @AssistedInject-annotated constructor.
Would love to be able to do something similar:
@Inject lateinit var factory: StateViewModelFactory<MyViewModel>
Issue Analytics
- State:
- Created 3 years ago
- Reactions:11
- Comments:6
Top Results From Across the Web
Problem with generic return type in guice assisted inject factory
Does anybody know, why guice 3 doesn't like the generic parameter in the factory method or what I generally misunderstood about assisted inject...
Read more >assisted inject and generic factory - Google Groups
So far i got the following code to be working: class Node<T>{} - a base class for nodes, lets say nodes in a...
Read more >FactoryModuleBuilder (Google Guice 5.0.1 API)
Provides a factory that combines the caller's arguments with injector-supplied values to construct objects. Defining a factory.
Read more >Assisted Injection - Dagger
A factory is typically responsible for combining all of the parameters and creating the object. (Related: guice/AssistedInject). Dagger assisted injection. To ...
Read more >How to produce the SavedStateHandle in your ViewModel ...
... a generic ViewModel Factory. (you can check the implementation in the article provided above). So, what does AssistedInject do to help ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@gusuly0rum the work around right now isn’t ideal but not terrible.
I have a generic abstract factory:
Then I just create a concrete factory for each one. Would be good to simpliy this down, but for the most part it’s just a one liner still:
The whole point of the ticket is the AssistedInject doesn’t work…