Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPF integration recipe - maybe there's a better way? #46

Open
heltonbiker opened this issue Aug 29, 2016 · 1 comment
Open

WPF integration recipe - maybe there's a better way? #46

heltonbiker opened this issue Aug 29, 2016 · 1 comment

Comments

@heltonbiker
Copy link

heltonbiker commented Aug 29, 2016

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?

@heltonbiker
Copy link
Author

Actually, there is a pending pull request changing exactely that:

#24

784c418

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant