Yellowfog
Yellowfog

Reputation: 2343

how is the NinjectDependencyResolver hooked in?

I have integrated Ninject into an MVC 4 project using the Nuget Ninject.MVC3 package. I can see that App_Start now contains a class called NinjectWebCommon, with a 'bootstrapper'. Following the standard instructions I can inject a concrete instance of an interface into a controller, and the debugger shows me that this is being served up by the NinjectDependencyResolver class.

However: I can't find out, by poking into the Ninject code, where this NinjectDependencyResolver class is being set as the IDependencyResolver using the standard DependencyResolver.SetResolver method. So I'm not sure if I'm just being inept, or whether there's an alternative way this is getting hooked in. Can anyone enlighten me?

Upvotes: 2

Views: 1540

Answers (1)

Cybermaxs
Cybermaxs

Reputation: 24558

IDependencyResolver ships with asp.net MVC framework and allows implementing dependency injection into controllers and other components (asp.net MVC use it internaly) .

I won't explain benefits here but you can read a good introduction here.

When adding an IoC framework (Ninject, Unity, StructureMap, ...) into MVC, you have to plug the Ioc code to the native dependency resolver, with an implementation of IDependencyResolver. The DependencyResolver static class is the registration point, especially DependencyResolver.SetResolver(IDependencyResolver resolver)

But now, many Ioc framework come with an MVC integration package such as Ninject.MVC our StructureMap.MVC. It's easier for us (and often more robust) and you don't really need to known how it is done.

A quick look at NinjectMvcHttpApplicationPlugin in repositoty Ninject.Web.Mvc will show you the glue.

Upvotes: 2

Related Questions