3

I read this guide about recommended app architecture for Android. Here is a graphical summery: enter image description here

The role of ViewModel is clear, but I don't understand why we need it if we use Repository. The purpose of ViewModel is to keep the data persistent during the Activity life cycle, but if the Repository anyway exists as long as the process exists, so what advantage is to use also ViewModel? I can make the Repository a singelton and retrieve it's instance directly from the Activity. That way I reduce complexity and achieve the same results.

  • 1
    The Repository and the ViewModel have different responsibilities. You can combine them if you like, but I would argue that you are violating the Single Responsibility Principle at that point. It's just a principle, of course, and you're free to violate it if you think your approach is better. – Robert Harvey May 12 '19 at 17:32
1

Yes you can combine them. This mean fewer code and achieve the same result. But for testing and when work in team is better keep them separe. The viewmodel has the responsibility of manage the data from the repository and his representation in the view (activities). The repository give the data to the viewmodel, the real data or mock data for testing proposes, and the view model get it no matter where it from.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.