Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM and nested view models

Tags:

mvvm

wpf

I have a simple example where I'm creating a View consisting of a list box, and the list box displays a bunch of items. I'm wondering if I'm going about the creation of the View Model and Model classes correctly here. Use whatever value of correctly works in this context, I understand it's a bit subjective, but my current solution doesn't feel right. Here's a simplified version.

The ViewModels and Models:

namespace Example
{
  public class ParentViewModel
  {
      public ParentViewModel()
      {
          // ... Create/Consume ChildViewModel * n
      }

      public List<ChildViewModel> ChildViewModels { get; set; }
  }

  public class ChildViewModel
  {
      public ChildViewModel()
      {
          // ... Create/Consume ChildModel
      }

      public ChildModel Model { get; set; }
  }

  public class ParentModel
  {
      public List<ChildModel> ChildModels { get; set; }

      public ParentModel()
      {
          // ... Create/Consume ChildModel * n;
      }
  }

  public class ChildModel
  {
      public ChildModel()
      {
          // ... Contains actual data.
      }

      public string Data { get; set; }
  }    
}

The View:

<Window x:Class="Example.View"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Example="clr-namespace:Example" Title="View" Height="300" Width="300"
    DataContext="{StaticResource TheViewModel}">
    <Window.Resources>
    <Example:ParentViewModel x:Key="TheViewModel" />
</Window.Resources>
<Grid>
    <ListBox Height="261" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="278" ItemsSource="{Binding ChildViewModels}"/>
</Grid>

In the proper code, the listbox will use a data template to display the child view models. But as you can see I'm not sure how the to instantiate the child related objects. It feels like the ParentViewModel will have a reference to the ParentModel and create ChildViewModel objects based on the ParentModel's ChildModel objects. Now I've said that it doesn't sound so daft, but I'd be interested in your thoughts.

like image 221
Ian Avatar asked Dec 06 '10 13:12

Ian


People also ask

Can a ViewModel have multiple models?

ViewModel is nothing but a single class that may have multiple models. It contains multiple models as a property. It should not contain any method. In the above example, we have the required View model with two properties.

What is the use of ViewModel in MVVM?

The purpose of ViewModel is to encapsulate the data for a UI controller to let the data survive configuration changes. For information about how to load, persist, and manage data across configuration changes, see Saving UI States.

What is the purpose of MVVM?

The Model-View-ViewModel (MVVM) pattern helps to cleanly separate the business and presentation logic of an application from its user interface (UI).

Should I always use MVVM?

For trivial projects MVVM is unnecessary. Using only the View is sufficient. For simple projects, the ViewModel/Model split may be unnecessary, and just using a Model and a View is good enough. Model and ViewModel do not need to exist from the start and can be introduced when they are needed.


1 Answers

You are on the right track.

The parent model would naturally contain a list of child models, e.g. a customer having multiple orders.

When ParentViewModel is created and loaded by a third-party, it is passed a ParentModel. Then the ParentViewModel will:

  1. Assign the ParentModel to a local variable
  2. Create a ChildViewModel for each ChildModel by passing the ChildModel to the ChildViewModel constructor
  3. Add each of those ChildViewModels to a list

By the way, you want

public List<ChildViewModel> ChildViewModels { get; set; } 

to be

public ObservableCollection<ChildViewModel> ChildViewModels { get; set; } 
like image 189
Aliostad Avatar answered Oct 04 '22 19:10

Aliostad