Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 1.44 KB

07_set_up_dependency_injection.md

File metadata and controls

38 lines (29 loc) · 1.44 KB

Set up Dependency Injection

MvvmGen does not depend on a specific dependency injection library. That means, you can use your favorite library.

The EmployeeManager application from the MvvmGen Samples repository uses the NuGet package Microsoft.Extensions.DependencyInjection. When you take a look at the App.xaml.cs file of the WPF project there, you can see the following code:

public partial class App : Application
{
  public static ServiceProvider? ServiceProvider { get; private set; }

  protected override void OnStartup(StartupEventArgs e)
  {
    base.OnStartup(e);

    var serviceCollection = new ServiceCollection();
    serviceCollection.AddSingleton<IEventAggregator, EventAggregator>();
    serviceCollection.AddTransient<MainWindow>();
    serviceCollection.AddTransient<MainViewModel>();
    serviceCollection.AddTransient<IEmployeeDataProvider, EmployeeFileDataProvider>();
    serviceCollection.AddTransient<NavigationViewModel>();
    serviceCollection.AddTransient<IEmployeeViewModelFactory, EmployeeViewModelFactory>();

    ServiceProvider = serviceCollection.BuildServiceProvider(true);

    var mainWindow = ServiceProvider.GetService<MainWindow>();
    mainWindow?.Show();
  }
}

The important part is that you register the EventAggregator as a singleton. All ViewModels should get the same EventAggregator instance, so that the ViewModel communication works.