title: Dependency Injection WPF MVVM Navigation .NET Core
body: <p>I need some help to understand how to instantiate ViewModels without having all of them as parameters in the MainViewModel Class constructor.</p>
  <p>Could any of you help me to get my head around and get rid of so many parameters in the constructor. I've read about FactoryPatterns but I don't understand how to implement it or maybe that is not the solution?. Anyway here's the code.</p>
  <p>Thank you. Pls help me this is driving me nuts!</p>
  <p>app.xaml.cs</p>
  <pre><code>    private readonly ServiceProvider _serviceProvider;
      public App()
      {
          ServiceCollection services = new ServiceCollection();
          ConfigureServices(services);
          _serviceProvider = services.BuildServiceProvider();
      }
  
      private void ConfigureServices(ServiceCollection services)
      {
          services.AddSingleton&lt;MainWindow&gt;();
          // Services
          services.AddSingleton&lt;ICustomerService, CustomerService&gt;();
          // ViewModels
          services.AddScoped&lt;MainViewModel&gt;();
          services.AddScoped&lt;CustomerViewModel&gt;();
          services.AddScoped&lt;CustomerAddViewModel&gt;();
          services.AddScoped&lt;CustomerEditViewModel&gt;();
          services.AddScoped&lt;ServiceViewModel&gt;();
      }
  
      private void OnStartup(object sender, StartupEventArgs e)
      {
          var mainWindow = _serviceProvider.GetService&lt;MainWindow&gt;();
          mainWindow.DataContext = _serviceProvider.GetService&lt;MainViewModel&gt;();
          mainWindow.Show();
      }
  </code></pre>
  <p>MainViewMode.cs</p>
  <pre><code>public class MainViewModel : ViewModelBase
  {
      private CustomerViewModel _customerViewModel;
      private CustomerAddViewModel _customerAddViewModel;
      private CustomerEditViewModel _customerEditViewModel;
  
      private ViewModelBase _selectedViewModel;
      public ViewModelBase SelectedViewModel
      {
          get =&gt; _selectedViewModel;
          set
          {
              _selectedViewModel = value;
              NotifyPropertyChanged();
          }
      }
  
      public RelayCommand CustCommand { get; set; }
      public RelayCommand ServCommand { get; set; }
  
      **public MainViewModel(
          CustomerViewModel customerViewModel,
          CustomerAddViewModel customerAddViewModel,
          CustomerEditViewModel customerEditViewModel)
      {
          _customerViewModel = customerViewModel;
          _customerAddViewModel = customerAddViewModel;
          _customerEditViewModel = customerEditViewModel;
          CustCommand = new RelayCommand(OpenCustomer);
      }**
  
      private void OpenCustomer()
      {
          SelectedViewModel = _customerViewModel;
      }
  
  }
  </code></pre>
  <p>CustomerViewModel</p>
  <pre><code>    public class CustomerViewModel : ViewModelBase
      {
      private ICustomerService _repo;
      private ObservableCollection&lt;Customer&gt; _customers;
      public ObservableCollection&lt;Customer&gt; Customers
      {
          get =&gt; _customers;
          set
          {
              _customers = value;
              NotifyPropertyChanged();
          }
      }
  
      public CustomerViewModel(ICustomerService repo)
      {
          _repo = repo;
      }
  
      public async void LoadCustomers()
      {
          List&lt;Customer&gt; customers = await _repo.GetCustomers();
          Customers = new ObservableCollection&lt;Customer&gt;(customers);
      }
  
      
  }
  </code></pre>