Trying MVVM with Simple Injector, I followed this guide: https://simpleinjector.readthedocs.io/en/latest/wpfintegration.html
Even with the simplest example having just MainWindow.xaml + MainWindowViewModel it creates double instances of View and ViewModel (I did try to ommit verification of container too). Moreover, after application gets closed, process still remains running.
[STAThread]
static void Main()
{
var container = new Container();
container.Register<MainWindow>();
container.Register<MainWindowViewModel>();
var app = new App();
app.InitializeComponent();
var mainWindow = container.GetInstance<MainWindow>();
app.Run(mainWindow);
}
I did however replace StartupUri
in App.xaml
to Startup
method which is empty as if I hadn't done that app.InitializeComponent()
method wouldn't be available.
Any suggestions on DI Container to use for MVVM purpose?
What you are witnessing is a behavioral and breaking change introduced in Simple Injector v5. This change is described in more detail here and noted in the release notes of v5.
In short, the container now automatically verifies the complete configuration on first resolve. In your case, verification causes all windows to be created, while the windows aren't closed after verification (because Simple Injector doesn't know they should be closed). This causes the application to eventually stay open.
Instead of using auto verification, you can turn it of using:
container.Options.EnableAutoVerification = false
and instead fall back on verifying the container from within a unit test.