0
votes

I'm currently learning about WPF and MVVM so I've decided to create a sample application.

To my knowledge a Model is for the back end. E.g. if you have "Spaghetti" as a Model, it would have properties like "Name", "colour", "length" etc. It follows a 1:1 relationship

View Model is for the front end so the View will use what you add to the View Model. e.g. displaying a list of the different names of Spaghetti.

I'm trying to read data from the database and display all the spaghetti names to the View. I've managed to do it but I feel like I'm missing the objective of MVVM as the way I have done it does not require the Model.

Here is the code:

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    MainWindowSpaghettiViewModel vm = new MainWindowSpaghettiViewModel();
    Service businessLogic = new Service();

    public MainWindow()
    {
        InitializeComponent();
        vm.SpaghettiNameCollection = businessLogic.GetSpaghettiNames();
        DataContext = vm;
    }
}

Model: Spaghetti.cs

public class Spaghetti
{
    public string Name { get; set; }
    public double Length { get; set; }
    public string Colour { get; set; }
    public decimal Price { get; set; }
}

View Model: MainWindowSpaghettiViewModel.cs

public class MainWindowSpaghettiViewModel
{
    public List<string> SpaghettiNameCollection { get; set; }
}

BusinessLogic Layer: Service.cs

public class Service : IService
{
    DBHelper db = new DBHelper();

    public List<string> GetSpaghettiNames()
    {
        return db.GetSpaghettiNames();
    }
}

DataAccess Layer : DBHelper.cs returns a list of spaghetti names by using a select statement.

Am I approaching this correctly as I've seen examples before where others have populated the View Model properties directly from the database?

How would the application flow? For example, in this window, I would like the user to select the name of the spaghetti they would like more information for, from the list I have just created.

After they select a spaghetti, they go to the next window where they can see all the information about the spaghetti (like the properties in the Model). Do I just create a new ViewModel with the same properties and do what I did above or do I populate the Model and then populate the ViewModel from that?

1

1 Answers

0
votes

Looks good so far but you could use List<Spaghetti> instead of List<string> and directly read the items into this List in the ViewModel from the database. So the Model is actually the template.

Do I understand you correctly? Your question is why do you need the Model class while you load the data directly into a ViewModel class?

The Model actually holds the data and provides the data for the ViewModel. The ViewModel is the logic for the View.

If you need more data for the view than the Model can provide but the Model also should not have and provide you create a ViewModel for data Model exclusive for the View but populate it with the Data from the Model which gets populated from for example an SQL Database. Otherwise, it's the same. If you only need a small portion from an entire Model for the View you create a ViewModel which has only the Properties needed for that purpose.

An example would be you have a TreeView and you need a Property named "IsExpanded" to have control about that. This Property has nothing to do with the pure Data and therefore should not be in a Model class itself. A non purist would say okay, but you could write a partial class for that model and ignore some View needed properties via attributes for the database.

If you only show a Name of that Model to the user, it would be enough to have a List of type string.

So there is no direct answer. You may expose your Data directly via ViewModel or via another ViewModel for the Model class with less or more properties, fields etc.

After they select a spaghetti, they go to the next window where they can see all the information about the spaghetti (like the properties in the Model). Do I just create a new ViewModel with the same properties and do what I did above or do I populate the Model and then populate the ViewModel from that?

It looks like you need the same properties stored in the database for the View itself. You could load all items directly and populate a List of Spaghettis with it or just populate strings but load the required data for the item if requested on the next page or wherever.

In general if you got loads of data many would say:

You shouldn't load all the data from the Database, load only what you need.

It think it just depends on the data. Some MVVM purist would always say, create a ViewModel for the Model and load and expose only the Data needed at that time.

I suggest you to read this, I hope it helps. If you have further questions ask.

https://www.wintellect.com/model-view-viewmodel-mvvm-explained/