How to make Repository not static in MVVM?

Hello, I am android SDK(library) developer, who uses MVVM pattern in SDK, because it has several activities and 8 fragments. Basically it works well, however I don’t know how can I inject Repository inside of ViewModel, without making it static… I have created a discussion in Reddit and got lots of opinions and suggestions. Discussion Basically everyone recommends creating static instance of Repository and inject it inside of ViewModel. The problem is that the Book about advanced architecture somehow made Repository not static, I could not realize that… Or maybe I am overthinking and static is the only way ?

@yuncheng13 @aldominium Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi @wellbranding,

First of all, I hope you are enjoying the book :smiley: We are about to release the full version in a couple of months and it will contain much more information about MVVM and other patters like MVP and MVC.

To answer you question I would suggest that you use the book more like a set of guidelines rather than rules. You might have noticed that the Sample App that we developed was a rather simple one with only 3 Activities so there was not much need to make our repo static. Your App seems more complex and you might need to be much more careful with your dependencies. So if you want to make your repo static go for it :slight_smile:


That being said, remember that yours was an early version of the book, so things might change a little :wink:

This topic was automatically closed after 166 days. New replies are no longer allowed.