I'm trying to understand idea of the IoC Containers. I found some sources that say static class for IoC Container is bad idea, but I can't find good example of not using static in this container. I'm trying to figure it out on my own, but with none results. I'm using Unity IoC Container in my WPF application, but I don't need help with views and viewmodels connection using IoC, because I understood it. I want to inject some registered objects into constructor of my viewmodel:
public class MyViewModel
{
private readonly IExceptionLogger _exceptionLogger;
public MyViewModel(IExceptionLogger exceptionLogger)
{
_exceptionLogger = exceptionLogger;
}
}
My class of container:
public class Bootstrapper
{
private readonly IUnityContainer _container;
public Bootstrapper()
{
_container = new UnityContainer();
}
public void RegisterTypes()
{
_container.RegisterType<IExceptionLogger, ExceptionLogger>();
}
}
Now, I'm stuck. I hoped that if I would add this to my App.xaml.cs, new instance of ExceptionLogger will be sent to constructor:
protected override void OnStartup(StartupEventArgs e)
{
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.RegisterTypes();
base.OnStartup(e);
}
But, unfortunately nothing is sent to it. I know that I miss something, but I don't know what.
If somebody knows any good articles about non-static classes for IoC Containers, I will be grateful. If somebody knows what do I miss in my code, please, tell me also.
MyViewModelwhy is your constructor namedStartupVM? - maccettura