Wpf – MVVM model instantiation


Following WPF MvvmFoundation, linking the View with the ViewModel has many choices like described on http://www.paulstovell.com/mvvm-instantiation-approaches.

However their example has nothing about how to link the ViewModel with the Model.

Traditionally I created the model first and then one or more views that render it. It seems that MVVM pushes people to create the View, which creates the ViewModel, which create the Model. I hope it's not the case as wiring a complex business model with various ModelView can else be tough.

How do you instantiate your business model classes in MVVM and link them with your ViewModels?

Best Solution

I normally pass Model objects as constructor params to VM. I use App class as the controller which will initialize MainWindow, MainWindowViewModel with the main model. There after the MainWindowViewModel takes care of initializing other VMs with appropriate model objects.

    private void Application_Startup(object sender, StartupEventArgs e)
        mainWindow = new MainWindow();
        mainWindow.DataContext = new MainWindowViewModel(new Model());