Where to place and configure IoC container in a WP

2019-02-06 01:37发布

问题:

I am working on a middle sized WPF application (MVVM) that should be extensible and maintainable in the future. Thus I decided to use an IoC container (Unity in this case) to keep things flexible.

However I am not sure where to place and configure Unity in a WPF application.

I guess container should be accessible globally so it should probably go to Application class. But should I make it as static property? Should I configure it in Application_Startup() event handler?

Eg:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    public static UnityContainer MyUnityContainer;


    private void Application_Startup(object sender, StartupEventArgs e)
    {
        // instantiate and configure Unity
    }
}

This way I will be able to access container from any place in the application via static property:

App.MyUnityContainer

I guess this is one way to do it but I am not sure if there are better practices for this issue, specifically for WPF apps.

回答1:

Have a look at the Composition Root Pattern. What you want to do is to initialize it in your Startup event handler and forget about its existence for the rest of the application.

You are trying to implement the Service Locator Pattern, which according to many is an inferior solution to this problem.



回答2:

Let me post what I've concluded and hopefully it'll help people out. Correct if there's anything wrong! :P

I guess we'd be looking into something like this:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        UnityContainer myUnityContainer = new UnityContainer();
        //make sure your container is configured
        myUnityContainer.RegisterType<ISomeDependency, SomeDependencyImplementation>();
        myUnityContainer.RegisterType<IMainWindow, MainWindow>();

        myUnityContainer.Resolve<IMainWindow>().Show();
    }
}

public partial class MainWindow : Window, IMainWindow
{
    private ISomeDependency _someDependency;

    public MainWindow(ISomeDependency someDependency)
    {
        _someDependency = someDependency;
    }
}

Note there are no globals or singletons, the container survives as long as MainWindow does and all dependencies behind this point of entry further into the composition graph are automagically resolved as long as the container knows about them.



回答3:

As per new version of Unity container, we have to register it's own instance as well to get it in view models via constructor injection.

App.xaml.cs file:

protected override void OnStartup(StartupEventArgs e)
{
       var unityIoC = new UnityContainer();
       unityIoC.RegisterTypes(AllClasses.FromAssembliesInBasePath(), WithMappings.FromMatchingInterface, WithName.Default);
       unityIoC.RegisterInstance(typeof(IUnityContainer), unityIoC);
}

View Model class

[InjectionConstructor]
public MyViewModel(IUnityContainer container)
{
}

Now unity container would be available for us in view model and can be used to resolve.