C
C#16mo ago
ivi

✅ MVVM: Are models observable?

I never quite understood what the Model is in MVVM. My assumption is that this is any data you feed into the application, from any source, and can be swapped out for any other, and viewmodels have to support that. Yet it seems very convenient to put Observable collections/properties in your Model to make the rest of the code work. But would you be able to even expect that from the model you put in? Or is the Model a bespoke part of your application which in turn grabs & stores that external data, and therefore is free to be designed with Observability?
5 Replies
Denis
Denis16mo ago
A model is supposed to be a dumb class/record with properties. At most, it can be also reactive and implement inotifypropertychanged on its properties. This means that yes, you could have an observable collecting in your model. Models are filled with data from, e.g., a db, or a file. Then the models are supplied to the view via the viewmodel a viewmodel can call a service to get models, or it can create them itself. Afaik models can't and should be swappable. They describe the domain of your application. Thus, changing the models outright changes your application The most important swappable part is the view Viewmodels can also be swappable,.but then you'd need an interface that defines the required properties of the viewmodel. Thus, swapping a VM primarily allows you to change some behavior. But imo, this is very unnecessary and you can achieve.better results by ensuring that the VM is UI and OS agnostic
Joreyk ( IXLLEGACYIXL )
the backend should be independent from your model that you are choose which means the model is dumb as it has to work with everyhting , your backend is the money maker frameworks like javafx spring asp try to get "into" the backend so you are bound to the framework in javafx you would implement observablees in the model so you cant get out of it even though you shouldnt have to but by design you are forced to do so
Florian Voß
Florian Voß16mo ago
Wouldn't it be better to implement INotifyPropertyChanged on the viewmodel tho? I assume that's why you said "At most" right? Let's keep the model as dumb as possible
Aurumaker72
Aurumaker7216mo ago
INPC in models is fine, as long as you don't reference the model layer from the view. For example, by binding. Binding to a Model is a violation of MVVM
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server