Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How not to use the default constructor using the MVVM UI Pattern?

Using DI in a WPF application along with the MVVM UI Architecture Design Pattern.

When setting the Window.DataContext property, the compiler complains:

The type [("my view model type")] does not include any accessible constructors.

Which had to be that there was no default constructor set in my view model class.

ProductManagementViewModel

public class ProductManagementViewModel 
    : ViewModel<ObservableCollection<Product>, Product> {
    public ProductManagementViewModel(ObservableCollection<Product> model)
        : base(model) { }

    public Product Current { get; set; }
}

ProductManagementView.xaml

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:Models="clr-namespace:WMI.Courses.DesignPatterns.Mvvm.Models" 
    xmlns:ViewModel="clr-namespace:MyProject.ProductManagement.Management" 
    mc:Ignorable="d" 
    x:Class="MyProject.ProductManagement.Management.ProductManagementView"      
    ResizeMode="NoResize"
    Title="{Binding ViewTitle}"
    Width="800">
    <Window.DataContext>
        <ViewModel:ProductManagementViewModel />
    </Window.DataContext>
    [...]
</Window>

Besides, it's best to use Constructor Injection, so since my View Model class depends on an ObservableCollection, it has to accept it through the constructor. And then, the only way found to solve the problem was to have a default constructor within the class.

ProductManagementViewModel

public class ProductManagementViewModel 
    : ViewModel<ObservableCollection<Product>> {
    public ProductManagementViewModel() 
        : this(new ObservableCollection<Product>()) { }
    [...]
}

This makes me feel somehow dirty, and it's like I have no other choice than that.

How not to use the default constructor using the MVVM UI Pattern?

like image 393
Will Marcouiller Avatar asked Sep 02 '25 02:09

Will Marcouiller


1 Answers

In the sample code you provided, you are using a view-first approach where the view model is bound to the view in XAML, which is limiting you to having to have a default constructor in your view model. The fastest way to get what you want would be to simply set the view data context in the code-behind, but since I think you are looking for a more clean solution, this article lists a few more.

However, I would recommend looking into using an MVVM framework. Not only do they help solve this problem (for example in Caliburn.Micro view models can be managed independently of views, using DI or whatever you prefer, and the wiring is done based on class names) but they typically provide many more useful tools to help implement the MVVM pattern.

like image 120
mnistic Avatar answered Sep 04 '25 16:09

mnistic