Documentation
Documentation copied to clipboard
WPF integration recipe - maybe there's a better way?
I have read the WPF Integration docs and they tell me to do it like this (compact version shown):
static class Program
{
[STAThread]
static void Main()
{
var container = Bootstrap();
var app = new App();
var mainWindow = container.GetInstance<MainWindow>();
app.Run(mainWindow);
}
}
On the other hand, a StackOverflow guruThe Man recommends to override the App.OnStartup
event:
The appropriate entry point depends on the framework:
- In console applications it's the Main method
- In ASP.NET MVC applications it's global.asax and a custom IControllerFactory
- In WPF applications it's the Application.OnStartup method
- In WCF it's a custom ServiceHostFactory
etc. (you can read more about framework-specific Composition Roots in chapter 7 of my book.)
I am, thus, using Simple Injector like this, with the same visible behaviour, and - IMO - a lot less invasiveness:
// after removing StartupUri from App.xaml...
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var bootstrapper = new BootStrapper();
MainWindow = bootstrapper.GetMainWindow();
MainWindow.Show();
}
}
Besides some minor variations, the whole point is the idea of not bypassing the normal WPF App instantiation, but instead just to override OnStartup
event.
What do you think of changing the documentation to reflect this?
Actually, there is a pending pull request changing exactely that:
#24
https://github.com/simpleinjector/Documentation/pull/24/commits/784c418dd5215c57561637c4540e124782e23cf4