I know there are already questions on that topic, but the issues there are somewhat specific to other problems and do not offer conclusive answers.
Especially those here: Question1, Question2 and of course Question3 so please do not close this question too fast. They answer there just say "Do this, do that" and not why!
There are people out there that deny the need for a ViewModel
and say that
the "standard" way is to bind to the Model directly. This is what I deny and try to prove with technical arguments.
From my background in MVC
, MVP
, Presentation Model
, it just is natural
to me to use a ViewModel
. Perhaps I miss an important point?
So for me the default is to bind to a ViewModel
, no matter what the Model
is (and no matter if it implements INotifyPropertyChanged
or not).
There are several reasons that I see for binding to ViewModel
s, including
(As mentioned here CodeProject and here on another article)
1. Removing logic from the View
2. Security
3. Loose coupling
4. Speed of development
Prototype ViewModel
hierarchy and bind to thatPrototype Model
Model
and ViewModel
can be developed testdriven, no matter of the ViewView
can entirely be built by a designer, or developer with strong design background5. "Tricky synchronizion" is solved
6. Equal structure throughout the project
7. Scalability
8. Separation of concerns
On contrary, the guy from the other thread dumps some points, including
If the model is updated directly, the view-model won't know to fire a property changed event. This causes the UI to go out of sync. This severely limits your options for sending messages between parent and child view-models.
If the model has its own property changed notification, #1 and 2 aren't a problem. Instead, you have to worry about memory leaks if the wrapper VM goes out of scope but the model doesn't.
If your models are complex, with lots of children objects, then you have to walk the entire tree and create a second object graph that shadows the first one. This can be quite tedious and error prone.
Wrapped collections are particularly difficult to work with. Any time something (UI or backend) inserts or removes an item from a collection, the shadow collection needs to be updated to match. This kind of code is really hard to get right.
So, the question is: what is the default way to bind and why?
Do I miss points that make it necessary to have a ViewModel?
Are there any real reasons one would like to bind to a model?
Most important is the why, not the how to.
A model is usually more closely related to how your data is stored (database, services, etc.) and the model will closely resemble those. The ViewModel on the other hand is closely related to how your data is presented to the user. It is usually a flatten version of your model, denormalized, etc.
Data Binding allows you to effortlessly communicate across views and data sources. This pattern is important for many Android designs, including model view ViewModel (MVVM), which is currently one of the most common Android architecture patterns.
While a view should only have one viewmodel, a single viewmodel might be used by multiple views (imagine a wizard, for example, that has three views but all bind to the same viewmodel that drives the process).
At present, this means that every ViewModel must have a public constructor which has either no parameters or which has only string parameters.
View models usually contains members, which are intended to be used with view (e.g., properties like IsSomethingSelected
, IsSomethingExpanded
, IsSomethingVisible
, any implementation of ICommand
).
Do you see any reason to bring all those stuff into models? Of course, no. That's why view models exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With