1

I am creating a WPF application. Naturally my entry point is MainWindow.xaml, which is opened up by App.xaml

var mainWindow = container.Resolve<MainWindow>();
Application.Current.MainWindow = mainWindow;
Application.Current.MainWindow.Show();

I am using Dependency Injection and so far all the dependencies are passed as parameters in the ctor of the MainWindow's View Model.

i.e. my Main Window is

public partial class MainWindow : MetroWindow
{
  private readonly MainWindowModel mainViewModel;

  public MainWindow(MainWindowModel mainViewModel)
  {

and its View model is:

public MainWindowModel(IDataRepository dataRepo, ICommand command1, ICommand command2, etc ...)
{

However, I am now starting to realize this might be a problem. Given that the MainWindow is the entry point to the entire app, it seems like any dependency, anywhere in the application will have to first pass through the MainWindow View Model constructor. This seems crazy.

I am coming from the background of ASP.NET MVC and there we have Controllers, which receive only the dependencies that they need. i.e. the concept of a main entry point there is missing and this makes things easier and more manageable.

Here is an example in my WPF app. A control, on the Main View needs to open up a dialog. This dialog is another Window and of course that window receives its ViewModel in its ctor. To me, it seems like to be able to resolve the dialog properly, I need to pass it through the Main Window View Model ctor first, keep it as private readonly field of the Main Window View Model and launch it when necessary. Ok, but what if I have 100 dialogs. That's just one of the examples. I have such issue with the ICommand implementations too.

To sum up my question:

How do I manage the dependencies in WPF properly, without using the Service Locator anti-pattern and without passing every single abstraction through the ctor of the main window view model? I could very easily pass a Container around and let, e.g., the create ABC command solve the ABCDialog before opening it, but I feel this will cause more issues than it would solve.

I am probably doing something wrong. Please advise me what is the best practice.

hyankov
  • 4,049
  • 1
  • 29
  • 46
  • Is this a good approach - http://stackoverflow.com/questions/25366291/how-to-handle-dependency-injection-in-a-wpf-mvvm-application, if I have to make a compromise about a Service Locator? – hyankov Dec 31 '16 at 13:56
  • Why do you need to "pass through the MainWindowViewModel" when you open a dialog? You could inject the MainWindowViewModel with a dialog service and create any child view model and child views from this dialog service. The child view models and the child views have no dependency upon the MainWindowViewModel at all. The control on the MainWindow binds to a command of the MainWindowViewModel that calls the dialog service. – mm8 Jan 01 '17 at 20:10
  • The issue was the who the resolution root is. Without a View Model Service Locator or a similar approach the only resolution root I had was the Main Window. I have come to a conclusion that to have a DI in WPF, you need a View Model Service Locator, view-first. – hyankov Jan 01 '17 at 21:54

0 Answers0