Ninject: a generic DI / IoC container

I want to share a container with different layers in my application. I started creating a static class that initializes the container and registers types in the container.

public class GeneralDIModule : NinjectModule { public override void Load() { Bind<IDataBroker>().To<DataBroker>().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 that there is a Resolve method. What is the difference between Resolve and Get?

In my unit tests, I do not always want every registered type in my container. Is there a way to initialize an empty container and then register the types I need. I will also mock unit test types, so I must also register them.

There is an Inject method, but it says the instance life cycle is not controlled?

Can someone please configure me correctly?

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

+1
ninject ninject-2
source share
1 answer

Ninject, by default, binds transitional components, and Ninject does not track temporary instances. The resolution is used internally and should not be used by your code unless you really know what you are doing. If you want to mock your container, use the ninject.moq extension on github. The injection method you are referring to refers to the instances you created yourself. Use the Get and TryGet methods.

+2
source share

All Articles