Jai
Jai

Reputation: 8363

WPF MVVM - What is the Model comprises of

Firstly, I am quite new to WPF and MVVM. I have read quite a lot regarding Views and ViewModels over the past two months, and I think I have adequate understanding on both of them. What most articles rarely cover is the Model part, and I find myself struggling to understand it well.

Currently, what I understand is that Models tend to be those entities that we know in Entity-Relationship (ER) models for relational databases. Once a while, I do see terms like Services, but I am not sure if they are Models.

Now, there are two questions I have in mind:

  1. If I have program states, which will affect how various different Views present themselves, are these considered Models? So far, I would put them under those so-called Services, which are simply singletons that live only during the application lifespan. Then, I would make ViewModels interact with them, either by calling the Services' methods, or by subscribing to changes in the Services via INotifyPropertyChanged. But doing this confuses me because they don't feel like Models; in fact, they feel like something totally outside of MVVM.

  2. My application does not use databases. In a rather odd way, I need to save my persistent information/data in XML files. So, say, if I have this Car model class, and my application needs to save a list of Car objects. Again, should I have those so-called Services, which would be responsible for serializing and de-serializing Car objects, and also to maintain that in a List<Car>? Then the same question comes again - is that Service part of the Model?

Edit

I was told my question 1 isn't clear. So let me give an example.

For example, the program has a single window with multiple Views at different part of the window.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <view:ViewA Grid.Column="0" />
    <view:ViewB Grid.Column="1" />
</Grid>

So in ViewA, there is a Button that has its Command binding. So when the button is clicked, it will cause something to run in the background (can be computational task, for example). When that something runs, the program enters into a special state. When the program enters this state, one of the Controls (for example, a TextBox) will go into disabled mode (IsEnabled="False"), which will temporarily not accept any inputs, until that background task finishes and that the program goes back to normal state.

So, where does this whole thing fit into the whole MVVM concept? I know this may not be a Model related question anymore, but when I started this thread I was thinking it most likely fits in Model.

Upvotes: 1

Views: 113

Answers (1)

ViVi
ViVi

Reputation: 4464

Models are not which those present the views or change the views. Models are data classes. For example say you have a View(Car.xaml) that displays the details of a car. The view may have a picture, a heading, name of the car, top speed, mileage, and so many features in common. You bind this view to a view model(CarViewModel.cs) where you bind the properties of the view to either a List or independent properties like string, double and all.

If you are binding the details to a list, you definitely need to define a data class for that list. For this you create a data class. Say CarData.cs.

public class CarData : BindableBase
{
   private string carName;
   private double mileage;

   public string CarName
   {
      get {return carName;}
      set {SetProperty(ref carName, value);}
   }
   ........
}

and so on.. You can define all the properties in this dataclass and reuse the same class for each view or even create a list of cars in the viewmodel.

When the model is updated, the view will also be updated since the NotifyPropertyChanged event is triggered. For saving the datam you can serialize them to an xml file when required, like during the click of a button or while closing the application. Just serialize the list in the view model to an xml file. I hope you got the point. If any doubts, please revert.

Upvotes: 1

Related Questions