1
votes

i studying by the book Pro asp.net mvc2 by apress, In the book the author uses ninject for dependency injection. He inherits the default control factory :

public class NinjectControllerFactory : DefaultControllerFactory

and then overrides : GetControllerInstance

I have 2 questions.

  1. how does the DefaultControllerFactory knows what controller to load ? i assume he uses the route table and then loads it with reflection. am i correct ?

  2. and how the ninject knows to do it, the only line i wrote is :

    private IKernel kernel = new StandardKernel(); return (IController)kernel.Get(controllerType);

Ninject can't possibly know about my route table and which control he should load .. so how does he knows ?

ps. i use the regular ninject, not some specific version that created for MVC (which i believe they have on their website also)

2

2 Answers

0
votes

NinjectControllerFactory.GetControllerInstance gets called by MVC. This means it is MVC that evaluates which controller type is required to process a request and it get it from the routing table.

If you want to use this I recommend using https://github.com/ninject/ninject.web.mvc It will save you time and work as it is maintained together with Ninject. Otherwise you will have to update to new Ninject versions yourself.