views:

232

answers:

2

Hello,

I was trying Ninject in a Asp.net Mvc application and I was wondering what the best practice is for using Ninject if you have more than 1 project in your solution.

I guess all projects need some kind of Loader which you initialize in the global.asax?

Kind regards,

Pickels

+1  A: 

In general you should only be bringing in an IoC container (in this case Ninject) in the actual executable (or ASP.NET application). Reusable libraries should be agnostic toward which container is used.

To use Ninject in an ASP.NET MVC application specifically, you should use the Ninject controller factory in the Ninject.Web.Mvc extension.

Aaronaught
I was already using Ninject.Web.Mvc but thanks for that tip anyway.
Pickels
Do you have some information or a pattern that shows how I can have each library have it's own IoC container? The example code I started from uses the kernel from the domain library to also load the Ninject modules in the web project.
Pickels
@Pickels: Each library *doesn't* have its own IoC container. That was my point - you only start using the IoC container at the last possible stage.
Aaronaught
Exaclty, there only needs to be one IoC container per *application*, no matter how many projects it may be comprised of. This is set up in your entry point project.
UpTheCreek
Thanks for the info I really appreciate it. Going to do some testing and try to implement it this way.
Pickels
+1  A: 

Usually your IoC container is initialised and configured in your entry point project (the web project in your case). And yes, this would normally take place in (directly or indirectly) the global.asax. You shouldn't need to bother with config in any of the other projects though in most cases.

UpTheCreek