I want to use Ninject for IoC for an ASP.NET MVC web application. I have the following code sample:
// resides in a MyApplication.Web assembly
public class SomeController {
...
public ActionResult myControllerAction() {
Service service = <--- should this be instantiated by Ninject?
service.doSomeLogic();
...
}
}
// resides MyApplication.Common assembly
public class Service {
public void doSomeLogic() {
...
}
}
My concern is that I do not want both assemblies to have a dependency on Ninject, if possible. It seems to me like I’d want to allow the .Web project to have a dependency on Ninject, but not the .Common assembly.
What’s the strategy to use here?
The service being created need not have a dependency on Ninject. Only the web project would need this. You should probably be using constructor injection on the controller (and Service, if it also has dependencies). You can wire these up in your web project in global.asax, or more likely a configuration class invoked from there. Use the Ninject NuGet package for MVC, follow the examples for configuring your services.