title: Should I use additional container in my MVVM project?
body: <p><strong>Source to the sample project:</strong> <a href="https://github.com/AntwanReno/navi" rel="nofollow noreferrer">https://github.com/AntwanReno/navi</a></p>
  
  <p>I did a project in WPF MVVM. It has three sub-projects: WPF app, ViewModels (PCL) and Domain (PCL). WPF is just a single <code>Window</code> with single <code>Frame</code> and two <code>Pages</code>. I will present the code, but recommend to clone/fork my prepared sample in repo.</p>
  
  <p>Here is the code for WPF Client:</p>
  
  <p><strong>App.xaml.cs</strong></p>
  
  <pre><code>namespace NaviWPFApp
  {
      using System.Windows;
      using NaviWPFApp.Views;
      using NaviWPFApp.Views.Pages;   
  
      public partial class App : Application
      {
          public static NavigationService Navigation; 
  
          protected override void OnStartup(StartupEventArgs e)
          {
              base.OnStartup(e);  
  
              MainWindow mainWindow = new MainWindow();
              mainWindow.Show();  
  
              Navigation = new NavigationService(mainWindow.MyFrame);
              Navigation.Navigate&lt;FirstPage&gt;();
          }
      }
  }
  </code></pre>
  
  <p><strong>App.xaml</strong> It's just:</p>
  
  <pre><code>&lt;Application x:Class="NaviWPFApp.App"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:local="clr-namespace:NaviWPFApp"
               x:Name="Application"&gt;
      &lt;Application.Resources&gt;
          &lt;local:ViewModelLocator x:Key="ViewModelLocator"/&gt;
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p>I Have one main window with frame, and two very similar pages (no code-behind):</p>
  
  <pre><code>&lt;Window x:Class="NaviWPFApp.Views.MainWindow"
          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"
          mc:Ignorable="d"
          Title="NaviWPFApp" Height="300" Width="300"&gt;
      &lt;Grid&gt;
          &lt;Frame x:Name="MyFrame" Margin="10" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;   
  
  &lt;Page x:Class="NaviWPFApp.Views.FirstPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        mc:Ignorable="d" 
        d:DesignHeight="300" d:DesignWidth="300" Title="FirstPage" 
        DataContext="{Binding FirstPageViewModel, Source={StaticResource ViewModelLocator}}"&gt;
      &lt;Grid&gt;
          &lt;Button Command="{Binding GoToSecondPageCommand}"  Height="30" Content="Go to second page" /&gt;
      &lt;/Grid&gt;
  &lt;/Page&gt; 
  
  
  &lt;Page x:Class="NaviWPFApp.Views.SecondPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        mc:Ignorable="d" 
        d:DesignHeight="300" d:DesignWidth="300"
        Title="SecondPage"
        DataContext="{Binding Path=SecondPageViewModel, Source={StaticResource ViewModelLocator}}"&gt;
      &lt;StackPanel Margin="0, 100, 0, 0"&gt;
          &lt;Button Command="{Binding CountSomethingCommand}" Content="Count something" Height="30" /&gt;
          &lt;Button Command="{Binding BackToFirstPageCommand}" Content="Go back to page 1" Height="30" /&gt;
      &lt;/StackPanel&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p>I my client I have also two additional classes ViewModelLocator and NavigationService - used for naviation between pages:</p>
  
  <pre><code>namespace NaviWPFApp
  {
      using NaviWPFApp.ViewModels.Pages;  
  
      public class ViewModelLocator
      {
          public FirstPageViewModel FirstPageViewModel =&gt; new FirstPageViewModel(App.Navigation);
          public SecondPageViewModel SecondPageViewModel =&gt; new SecondPageViewModel(App.Navigation);
      }
  }   
  
  namespace NaviWPFApp
  {
      using System;
      using System.Linq;
      using System.Reflection;
      using System.Windows.Controls;
      using NaviWPFApp.ViewModels.Common; 
  
      public class NavigationService : INavigationService
      {
          readonly Frame frame;   
  
          public NavigationService(Frame frame)
          {
              this.frame = frame;
          }   
  
          public void GoBack()
          {
              frame.GoBack();
          }   
  
          public void GoForward()
          {
              frame.GoForward();
          }   
  
          public bool Navigate(string page)
          {
              var type = Assembly.GetExecutingAssembly().GetTypes().SingleOrDefault(a =&gt; a.Name.Equals(page));
              if (type == null) return false; 
  
              var src = Activator.CreateInstance(type);
              return frame.Navigate(src);
          }
  
          public bool Navigate&lt;T&gt;(object parameter = null)
          {
              var type = typeof(T);
              return Navigate(type, parameter);
          }
  
          public bool Navigate(Type source, object parameter = null)
          {
              var src = Activator.CreateInstance(source);
              return frame.Navigate(src, parameter);
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Here is my <strong>ViewModels (Portable)</strong> project:</p>
  
  <p>It's only two ViewModel classes for each Page in UI, INavigationService (I don't want to know anything about NavigationService implementation and UI client), MyObservableObject and MyCommand.</p>
  
  <p><code>MyObservableObject</code> and <code>MyCommand</code> are typical implementations of <code>INotifyPropertyChanged</code> and <code>ICommand</code> interfaces.</p>
  
  <p>So this is an interface and two viewmodels:</p>
  
  <pre><code>public interface INavigationService
  {
      void GoForward();
      void GoBack();
      bool Navigate(string page);
  }
  
  
  namespace NaviWPFApp.ViewModels
  {
      public class FirstPageViewModel : MyObservableObject
      {
          private readonly INavigationService navigationService;
  
          public FirstPageViewModel(INavigationService navigationService)
          {
              this.navigationService = navigationService;
          }
  
          public MyCommand GoToSecondPageCommand
          {
              get { return new MyCommand(x =&gt; navigationService.Navigate("SecondPage")); }
          }
      }
  }   
  
  namespace NaviWPFApp.ViewModels
  {
      public class SecondPageViewModel : MyObservableObject
      {
          private readonly INavigationService navigationService;
          private readonly BusinessLogic businessLogic;   
  
          public SecondPageViewModel(INavigationService navigationService, BusinessLogic businessLogic = null)
          {
              this.navigationService = navigationService;
              this.businessLogic = businessLogic;
          }   
  
          public MyCommand BackToFirstPageCommand
          {
              get { return new MyCommand(x =&gt; navigationService.Navigate("FirstPage")); }
          }   
  
          public MyCommand CountSomethingCommand
          {
              get { return new MyCommand(x =&gt; businessLogic?.CountSomething()); }
          }
      }
  }
  </code></pre>
  
  <p>And my Business logic, just this:</p>
  
  <pre><code>public class BusinessLogic
  {
      private int counter = 0;
  
      public bool CountSomething()
      {
          return ++counter &gt; 10;
      }
  }
  </code></pre>
  
  <p>Dependencies are simple: <strong>Domain</strong> knows nothing, besides its own operations, <strong>ViewModel</strong> knows about Domain, but nothing about View and <strong>View</strong>... well, here is my problem - it knows about ViewModel, but should View know about Domain? I will explain my concerns, but here is what I meant:</p>
  
  <p><a href="https://i.stack.imgur.com/RpVyi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RpVyi.png" alt="enter image description here"></a></p>
  
  <p><strong>First concern:</strong> As you can see, the navigation is all done in ViewModel, and Business logic is used only in second page view model. SecondPage view doesn't need to know about the logic. </p>
  
  <p><strong>Second concern:</strong> Because I'm trying to stick with dependency injection, I want to create my domain object (which I need only one) at the beginning of the program. So in <code>protected override void OnStartup(StartupEventArgs e)</code>, so in VIEW. And I have no idea, how to pass it to second view model, which is created in <code>ViewModelLocator</code>.</p>
  
  <p>So my question is: How to convert this code, so it will be more ViewModel orientated? I want just inject my Domain object into ViewModel (where it belongs), not into View. </p>
  
  <p>Thanks for any advice!</p>