Ninject: Shared DI/IoC container

Posted by joblot on Stack Overflow See other posts from Stack Overflow or by joblot
Published on 2010-05-19T03:41:21Z Indexed on 2010/05/19 4:20 UTC
Read the original article Hit count: 298

Filed under:

Hi

I want to share the container across various layers in my application. I started creating a static class which initialises the container and register types in the container.

public class GeneralDIModule : NinjectModule { public override void Load() { Bind().To().InSingletonScope();

}

}

public abstract class IoC { private static IKernel _container;

public static void Initialize()
{
    _container = new StandardKernel(new GeneralDIModule(), new ViewModelDIModule());
}

public static T Get<T>()
{
    return _container.Get<T>();
}

}

I noticed there is a Resolve method as well. What is the difference between Resolve and Get?

In my unit tests I don’t always want every registered type in my container. Is there a way of initializing an empty container and then register types I need. I’ll be mocking types as well in unit test so I’ll have to register them as well.

There is an Inject method, but it says lifecycle of instance is not managed?

Could someone please set me in right way?

How can I register, unregister objects and reset the container.

Thanks

© Stack Overflow or respective owner

Related posts about ninject