Suppose I have a Windows WPF application (SampleApp) that is using IoC (Ninject). I have wired up everything in my composition root as follows.
[STAThread]
static void Main()
{
IKernel kernel = new StandardKernel();
var modules = new List<NinjectModule>
{
new ExternalDependencies(),
new DependencyResolver()
};
kernel.Load(modules);
var mainWindow = kernel.Get<IMainWindow>();
mainWindow.ShowDialog();
kernel.Release(mainWindow);
kernel.Dispose();
}
The Conents of ExternalDependencies can be ignored for this example, but here are the contents of DependencyResolver which is for internal dependencies.
class DependencyResolver : NinjectModule
{
public override void Load()
{
Bind<IMainWindow>().To<MainWindow>().InSingletonScope();
Bind<IProductWindow>().To<Product>().InSingletonScope();
}
}
As you can see, I have MainWindow and Product wired up. The ctor on MainWindow looks as follows.
public MainWindow(IDomain<TaskFields, TaskExtFields, TaskEmbeds> task)
{
_task = task;
InitializeComponent();
}
We're all good to this point. However, now I am at the point where I would like to have actions in the main window (button clicks, menu selections, etc) open various other forms. For example I have a Product window as follows and I want a button click to open the window and wire everything up appropriately.
public Product(IDomain<ProductFields, ProductExtFields, ProductEmbeds> productDomain)
{
_productDomain = productDomain;
InitializeComponent();
}
Looking to keep my IoC from leaking out of the Main method. In other words I don't want the code to look like...
var productWindow = GlobalIoC.Get<IProductWindow>();
which would violate the three R's principle, require me to make the IoC global and leak information that I hope is not needed out into the rest of the code.
Right now I am dealing with 2 or 3 windows for testing - however in a final application I can see this growing to a larger number, say perhaps hundreds of windows each with their own backing viewmodels (when I get through this I will be implementing viewmodels), domain, data models, repositories, etc.
I have all of this working for the main form but I cannot wrap my head around how to properly wire everything up so that it's available, and stays in the composition root.
To be clear, this is currently a learning project (IoC, MVVM, DI)
Suggestions?