简体   繁体   中英

ninject NinjectWebCommon black magic

I am just learning how to use Ninject and I understand the basic principles of it, but my first project that I am applying it to is an MVC 5 project, needing to do some constructor injection on a controller.

I followed the guids on the site ie

  • I pulled in the Nuget package
  • I set up the binding in my NinjectWebCommon's RegisterServices method
  • I made sure that my controller has an interface as a parameter of the constructor

This then works like a bomb, but there is just waaaay too much black magic in this for my liking.

I have worked out that because of WebActivator.PreApplicationStartMethod the NinjectWebCommon's start method gets called right at the beginning, and my bindings get setup. I am cool with this, but where I am puzzled is how on earth is the kernal.get method being called, and who is calling my controller with the paramater in the constructor (which would normally throw a nasty runtime error if I didnt have Ninject).

I can only assume that somewhere ninject is using the kernel to instantiate my controller (which in turn picks up the dependency on my interface, and moving down the chain to resolve all the others)

But where does this happen?

* Queue eerie voodoo music in the background *

it also happens in me, but in my case I just did

1). I pulled in the Nuget package 2). I create a constructor that accepts a parameter

and it works. I didn't put anything in NinjectWebCommon's RegisterServices.

I also put a breakpoint in NinjectWebCommon's start but it was not hit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM