Reputation: 29720
I have set up ninject on my project in the global.asax file...
protected void Application_Start()
{
RegisterDependencyResolver();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
private void RegisterDependencyResolver()
{
var kernel = new StandardKernel();
kernel.Bind<PteDotNetCore.IBlogManager>().To<PteDotNetCore.BlogManager>();
DependencyResolver.SetResolver(new PteDotNet.Resolution.PteDotNetDependencyResolver(kernel));
}
I have a controller with 2 consturctors....
public CodeController()
{
}
public CodeController(IBlogManager injectedManager)
{
manager = injectedManager;
}
When I remove the line:
kernel.Bind<PteDotNetCore.IBlogManager>().To<PteDotNetCore.BlogManager>();
from the first block of code my defaul constructor is used when the line is added then the constructor which takes the interface is used.
Please can someone tell me what decisions are being made by MVC to select the constructor? Its not quite clear.
Upvotes: 2
Views: 99
Reputation: 34299
The decision is actually made by ninject. For constructor injection it selects the one with the most parameters, see the wiki for details on exactly how this works
https://github.com/ninject/ninject/wiki/Injection-Patterns/1e462388cee1887a4bb90067cf334d91604f7ea8
Upvotes: 3
Reputation: 47375
The controllers are built using a controller factory. By setting DependencyResolver, you are telling MVC to use your IoC container to construct controllers.
By default there is no IoC, and as such the default no-arg constructors will be used.
When there is an IoC container (Ninject), the controller factory sees that your controller constructors have args. It will look in your IoC container to resolve those dependencies and pass instances to the arg constructors.
Upvotes: 1