Howdy,
I am new to using Unity and IoC/DI concepts. I started with the concept by rolling my own via James Kovacs' show on dnrTV in a test.
His example had the Container run as a singleton accessed through a static method in an IoC class so you could register types at a startup and resolve the type throughout your application.
I know this was not full featured and was to mainly show the concepts of IoC.
I am now attempting to use Unity in a project.
In my Main() I create a new container, but once my WinForms opens, the container falls out of scope and is disposed. Later on in the program, when I try to resolve a type I no longer have the original container and its registered types.
Is there a concept or implementation construct I am missing?
My current thought is to create something like this:
public static class Container
{
private static readonly object syncRoot = new object();
private static volatile IUnityContainer instance;
public static IUnityContainer Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
instance = new UnityContainer();
}
}
}
return instance;
}
}
}
I'm pretty sure this will work, it just doesn't seem right.
Thank you,
Keith