69988

Ninject: Shared DI/IoC container

Question:

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<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 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.

Answer1:

Ninject by default binds components in a transient lifestyle and Ninject does not track transient instances. The Resolve is used internally and shouldn't 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 inject method you are referring to is for instances that you have created yourself. Use the Get and TryGet methods.

Recommend

  • Ninject + NHibernate with two or more databases
  • Randomly-Occurring Ninject Exception: “Error loading Ninject component ICache”
  • Ninject: GetAll instances that inherit from the same abstract class
  • What did I get wrong, DI or Design, and how should I go about it?
  • ASP.NET MVC3 - Use DependencyResolver AND Windsor Castle: Why?
  • So we've got MEF working with MVC4, how about the convention based model?
  • Ninject: Bind multiple types to the same singleton instance
  • How to change the scope of an existing binding in Ninject
  • Dependency Injection - Choose DLL and class implementation at runtime through configuration file
  • Help designing a order manager class
  • Mocking Non-Standard Events in F# Foq
  • Configuring Ninject
  • How to override __call__ in celery on main?
  • What is the reason that Policy.getPolicy() is considered as it will retain a static reference to the
  • Magento site down due to mysql error General error: 1030 Got error -1 from storage engine
  • Projection media query: browser support and workarounds?
  • Javascript + PHP Encryption with pidCrypt
  • jquery mobile loadPage not working
  • Apache 2.4 - remove | delete | uninstall
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?