0

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?

SharpNoiZy
  • 1,099
  • 2
  • 11
  • 22
  • How do you create an instance of NinjectProxy? – Remo Gloor Feb 20 '13 at 08:31
  • normal, like var proxy = new NinjectProxy() – SharpNoiZy Feb 20 '13 at 08:46
  • possible duplicate of [Ninject ignoring \[Inject\] attribute](http://stackoverflow.com/questions/5369980/ninject-ignoring-inject-attribute) – Ruben Bartelink Feb 20 '13 at 09:29
  • 1
    @ChristianNeuß You only need to do a .Load() if there is a `Module` in there to be `Load`ed into the Kernel. Key thing here is that DI containers don't intercept `new`. Also, your `NinjectProxy` is a bad idea - you should be depending on the actual thing you need because http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx Highly recommended is http://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 Feb 20 '13 at 09:33
  • In my common.dll I have an Interface IUnitOfWork which is in the Implementation.dll inherited by the class "ProjectUnitOfWork". And now I have to get in common.dll somehow the ProjectUnitOfWork over Ninject and the interface IUnitOfWork. But how should I load in the common.dll a module/mapping which is configured in the Implementation.dll?? common.dll don't know the implementation.dll. – SharpNoiZy Feb 20 '13 at 09:45
  • See http://stackoverflow.com/questions/1675950/c-ninject-where-do-you-put-the-kernel-and-your-modules and http://stackoverflow.com/questions/2056409/what-is-the-intention-of-ninject-modules It is valid to have DLLs with modules in them that can be Loaded into a Kernel in a Composition Root. However these Modules should only Register stuff and nobody should be using Kernel directly as @RemoGloor is alluding to. In your example, the module is in implementation.DLL You could do `kernel.Load("MyApp.*.Implementation.dll")` and everyone else depends on the interfaces – Ruben Bartelink Feb 20 '13 at 14:06
  • BTW its worth searching the other questions for Ninject AND Module(s) as they cover most of the things you need to consider. Then vote to close this question and ask a question just about module structuring if you still have followups. ***Right now this question is a mess because it mixes: 1) Module layout 2) new not being hooked 3) how do I implement a Service Locator*** **and hence getting you all your answers based on this question as it is isnt going to be a good use of anyone's time** – Ruben Bartelink Feb 20 '13 at 14:07

1 Answers1

0

You are creating the instance manually using new This object is not handled by ninject and will never get dependencies.

Inject the instance somewhere instead or use a factory to have it created by ninject

But Ruben is absolutely right you are following a pattern you shouldn't use anyway. Instead configure Ninject completely in the bootstrapper. Either use conventions that match the whole project or reference all required assemblies and setup the bindings for them.

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • But I think, I'm trying to do a common task, to have a general lib "common" which is used in every project of the company and which declares an interface and have classes which depends on that interface and do some magic. And Project depending libs, like implementation which is different for every project, configs Ninject to use EF or Hibernate for this specific project. And my problem is, I have to get in the common.dll the type of the UnitOfWork which is used in this project, which is configured in the implementation.dll which is unknow to the common.dll. – SharpNoiZy Feb 20 '13 at 12:10
  • NB can you Vote to close please - the other post addresses the root issue (and also has lots of patterns of passing Kernels around!) – Ruben Bartelink Feb 20 '13 at 14:01
  • @ChristianNeuß As covered up top, it's worth reading the top questions on Module AND Ninject and then asking a question on just that if that doesnt help. (it is unclear whether you'll have multiple Units of Work in play and/or whether Named bindings are relevant, and/or using the facilities in Factory for using Named Bindings, or (hopefully) something much cleaner can be achieved by rationalising your architecture – Ruben Bartelink Feb 20 '13 at 14:12