Reputation: 6272
I'm trying to learn MVVM, and i'm struggling a little on differentiating between a model and viewmodel.
If someone could answer these 2 questions it would help clear a lot up for me:
Say I have an Objects
class, which is a viewmodel that contains multiple ObservableCollections of Object
.
The Object
class contains an ObservableCollection of strings that are displayed on the GUI.
Is the Object
class a model or viewmodel?
What if the Object
class contains just a string and a integer (name and value), is it a model or viewmodel?
Upvotes: 1
Views: 99
Reputation: 17385
The Model
is the class that holds your data. The data can be strings /integers or whatever.
The Model
can also be a list / collection of those objects. For example a List of Person
objects can still be your Model
.
The ViewModel
is the tier between your Model and the View. It should be used to perform whatever tasks you need on the data (for instance, if your Model is a list of Person
objects but you only want to show in your view people that are aged older then 18, this logic is done in the ViewModel
)
So to answer your question:
If you have an object which contains the data (in your example a list of strings) it is the Model
.
Even if the object is a little more complex (with relation to the number of properties it holds) it's probably still the Model.
Business Logic should be kept separate from the model. On the other hand Validation can be added to the Model (for instance to make sure the Age
property of a person is non-negative) since this is still rules on how your data should behave
Upvotes: 1