Why Repository Sends To Constructor Of ViewModel?
I'm studying Google Architecture Components with MVVM
and noticed that repository sends to constructor of ViewModel
. The question is why repository can't exist as a property, field in ViewModel
? Some developers create Factories
where repository is stored and initialize ViewModel with it. What is the reason to do so? MVVM
tells that View
does not have to know about Model
at all. So, why repository does not just stay at ViewModel
as a field?
Answer
Typically the repository doesn't change over the lifetime of the ViewModel. Supplying it as a parameter of the contrustor instead of a property ensures that it can't be changed, and code in the ViewModel can rely on it.
If it was a property, it could be changed during the lifetime of the ViewModel. Your code could load an object from one database and find the repository pointing to a different database when it needed to save.
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View