My first attempt on using an IoC container. A program has a number of different type of windows that are typically opened through a menu in MainWindow
. However, in principle it could also be further down the chain. These windows usually need injection of one or more singleton-classes, lets name them here IManager1/2/3
. So, do I need to bubble these IManagers
up through MainWindow
, or can I resolve each window at the root (here in App class
)?
In either case, how would this be done?
I don't need to use DryIoc, but that's just what I chose to test.
public partial class App : Application
{
DryIoc.Container container = new DryIoc.Container();
private void Application_Startup(object sender, StartupEventArgs e)
{
RegisterIoc();
var mainwindow = new MainWindow();
mainwindow.Show();
}
public void RegisterIoc()
{
container.Register<IManager1, Manager1>(Reuse.Singleton);
container.Register<IManager2, Manager2>(Reuse.Singleton);
container.Register<IManager3, Manager3>(Reuse.Singleton);
}
}
public MainWindow()
{
public MainWindow()
{
}
void OpenNewWindow2()
{
var w = new Window2(?, ?, ?);
w.Show();
}
}
public class Window2
{
IManager1 man1;
IManager2 man2;
IManager3 man3;
public Window2(IManager1 man1, IManager2 man2, IManager3 man3)
{
this.man1 = man1;
this.man2 = man3;
this.man3 = man3;
}
}