I'm starting with Ninject and I use it in a MVC 4 scenario and configured my bindings in "NinjectWebCommon". And everything works fine.
Now I want in an library somehow get the kernel with the configuration from MVC app.
For example: In my MVC project I have class "BaseController" with a property
[Inject]
public IKernel Ninject { get; set; }
works perfect, means in every action in a controller which inherits from BaseController the property "Ninject" is fine instance and not null!
Now I have class "NinjectProxy" in my external lib with the exact same Property, but every time I create a new Instance of "NinjectProxy" the prop "Ninject" is null!
public class NinjectProxy
{
[Inject]
public IKernel Ninject { get; set; }
public T Resolve<T>()
{
return Ninject.Get<T>();
}
}
My complete solution looks like:
MVC app
- Reference on Common.dll and Ninject
- Contains ControllerBase
Common.dll
- This project contains the NinjectProxy class and have a reference on Ninject
- Here I want somehow get the kernel config that I configured in the mvc app to resolve dependecies
Implementation.dll
- References on Common.dll and Ninject
The lib is loaded in "NinjectWebCommon" with:
kernel.Load(Assembly.Load("lib"))
If this is important.
Have someone an Idea what I'm doing wrong?
Module
in there to beLoad
ed into the Kernel. Key thing here is that DI containers don't interceptnew
. Also, yourNinjectProxy
is a bad idea - you should be depending on the actual thing you need because blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx Highly recommended is manning.com/seemann (Note it doesnt cover Ninject, but it's that good (and is about architecture more than low level) that it doesnt matter) – Ruben Bartelink