tags:

views:

471

answers:

1

I'm using Ninject in an MVC project and I've used the autoregistration features in Ninject.Mvc and have my bindings set up in my application class. However, I have a place where I want to create an instance separate from those bindings. In StructureMap, you can do var foo = ObjectFactory.GetInstance<IFoo>(); and it will resolve it for you. Is there an equivalent in Ninject 2? I can't seem to find it anywhere.

+3  A: 

AFAIK, NInject doesn't have static method like this so all resolving should go to some kernel.

But you can implement it easily;

 class ObjectFactory
 {
     static IKernel kernel = new StandardKernel(.....);
     public static T GetInstance<T>()
     {
          return kernel.Get<T>();
     }
 }

Although, IMO, NInject is much more useful as DI container than as service locator.

elder_george
Yeah, it is very simple to add a static container accessor. We have one that gets reused when the accessor is needed: http://github.com/idavis/ninject.extensions.wcf/blob/master/source/Ninject.Extensions.Wcf/KernelContainer.cs
Ian Davis