Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine MVVM and Dependency Injection in a WPF app?

Can you please give an example of how you would use (your favorite) DI framework to wire MVVM View Models for a WPF app?

Will you create a strongly-connected hierarchy of View Models (like where every nested control's ViewModel is a property on a parent's ViewModel and you bind it to nested control's DataContext in XAML) or you would use some kind of even-more-abstract ""View Model" Manager", which maintains some weakly-connected hierarchy... like in CAB, maybe?

like image 537
Max Galkin Avatar asked Nov 26 '09 08:11

Max Galkin


People also ask

What is dependency injection in WPF MVVM?

As you know, dependency injection is a form of “inversion of the control” (IoC) programming principle. This means that classes don't create the objects they rely on. DI frameworks have containers responsible for revealing and resolving dependencies.

Do you have to use MVVM with WPF?

The Windows Presentation Framework (WPF) takes full advantage of the Model-View-ViewModel (MVVM) pattern. Though it is possible to create WPF applications without using the MVVM pattern, a little investment in learning can make building WPF applications much simpler.

Is WPF MVVM or MVC?

MVVM is written for desktop application with data binding capabilities – XAML and the INotifyPropertyChanged interface. If you want to do modification in the View-Model, the View-Model uses an observer pattern. The MVVM pattern is mostly used by WPF, Silverlight, nRoute, etc.


2 Answers

If a view model can only exist in conjunction with another, I create a strong relationship. That is the owning view model will have a direct reference to one or more of the dependent view models. If, on the other hand, a view model should be able to exist with or without another, I take a loosely-coupled approach where they communicate via an event bus.

In terms of using DI with MVVM, absolutely you can combine the two. It's as simple as:

public class MyViewModel
{
    private readonly IMyDependency _myDependency;

    public MyViewModel(IMyDependency myDependency)
    {
        _myDependency = myDependency;
    }
}

Note, however, that this assumes a "view model first" approach to MVVM, which has its drawbacks.

like image 188
Kent Boogaart Avatar answered Sep 18 '22 14:09

Kent Boogaart


In WPF it's normally pretty easy and it doesn't really depend on any particular DI Container. Have you read Josh Smith's article on MVVM? It pretty much describes how to set up a hierarchy of ViewModels.

What it doesn't go into much is how to create those ViewModels from dependencies (such as Repositories), but it's not a difficult extrapolation to do.

I've often experienced that liberal use of Abstract Factories helps quite a lot in this regard. Instead of directly new'ing up ViewModels I let an injected Factory do it for me.

You can use Poor Man's DI or any kind of DI Container to wire up such Factories for you.

like image 36
Mark Seemann Avatar answered Sep 18 '22 14:09

Mark Seemann