Reputation: 5997
I have a little problem with designing DI architecture of my app. I am using Architecture Components ViewModels and I was inspired how to provide ViewModels via Dagger in their own GitHub repository .. I want to create scoped component/module for example for product detail that would provide ViewModels/Repositories and other stuff dependent on that single product.. problem is that all ViewModels are provided from the same ViewModelModule as in given example. But ProductViewModel needs ProductRepository that is provided from scoped subcomponent ProductModule. And apparently thats not possible because Dagger throws error at build time that I need to provide ProductRepository with @Provides annotation.
Ok so I thought that I would provide ProductViewModel in my ProductModule but then it crashes at runtime, because provider of my ViewModel can't be found in this method
Is it possible at all to have this architecture or do I need to redesign it to not use the same method as Google in their sample?
Upvotes: 4
Views: 638
Reputation: 60701
I was able to solve a similar problem by removing the @Singleton
annotation on the ViewModelProvider.Factory
subclass.
It shouldn't matter whether the factory is a singleton or not; it's only a performance issue, really. I ended up using @Reusable
instead, which tells Dagger that the factory instance can be safely reused if needed, but without the restrictions of a specific scope.
Upvotes: 6