views:

1102

answers:

1

I have a solution with two relevant (to this question) projects, and a few others;

  1. Class library with functionality used by several other projects.
  2. ASP.NET MVC application.

My question is basically where I should do IoC with Ninject 2, considering...

  • The class library needs some DI love, among other things in the repository classes which need web request specific session objects (think Unit of Work).
  • The MVC app needs DI since with Ninject 2 you basically inherit from NinjectHttpApplication.
  • Unit tests for the class library need to be aware of this to inject a different set of repositories.
  • Unit tests for the web app need to be injected for the same reason.

I have painted myself into a mental corner here, because I only saw three options to begin with. DI in the class library, DI in the web app, or both, but there are problems with each:

  • I can't do DI only in the class library since the MVC app needs to inherit from NinjectHttpApplication to begin with.
  • I can't do DI only in the MVC app - the class library is used by other libraries, after all, and the MVC app shouldn't know too much about the internals of the library anyway.
  • I guess this is the only way out I can see: Independent IoC for both projects. The class library and the MVC app each have their own IoC setup and do DI for their stuff without really caring about each other.

Does anyone have some "best practices" or guidelines on how to do something like this? I can't imagine I'm the first person to end up in this situation, and it would sure be nice to know what the "proper" way to do this is...

Thanks!

+15  A: 

I don't know NInject, but unless it works vastly different than Windsor, StructureMap etc. the answers tend to stay the same, as there are some common DI patterns. With that in mind:

The first thing to realize is that DI isn't tied to a particular framework such as NInject or Windsor. It is a set of techniques and design patterns to follow. You can do DI manually using so-called Poor Man's DI, but obviously it gets much better with a DI Container.

Why is this relevant? It is relevant because once you realize this, the corrolary is that the vast majority of your application's code should have no knowledge of the DI Container whatsover.

So where do you use the DI Container? It should only be used in a Composition Root, which in your case would correspond to Global.asax. You can read a bit more about this in this SO answer - although that question is about Windsor, the principle remains the same.

How about your unit tests then? They should be completely ignorant of the DI Container as well. See this other SO answer for more details.

DI can be achieved in your library with copious use of Constructor Injection. You don't need to reference any DI Container to do that, but it makes life a lot easier if you use a DI Container to resolve all dependencies from the Composition Root.

Mark Seemann
Hi Mark - thanks for the answer, I think I get it now, at least most of it. However, if the MVC / Global.asax is responsible for setting up all DI, will it need to poke into the class library stuff as well? Consider that the class library might need to store a per web-request ISession or something like that, which will be used to construct all repository classes. Should the MVC app know about this? Just trying to wrap my head around "The Right Thing" to do. :)
Rune Jacobsen
Since you are using MVC your best option is to use a custom ControllerFactory to create the Controllers with all their dependencies. You can see an example of this with Windsor in the MvcContrib source code: http://github.com/mvccontrib/MvcContrib/blob/be0eb3addedf1775db719117e7fa73e516f74c8d/src/MvcContrib.Castle/WindsorControllerFactory.cs If some of the dependencies are types in your library, these should be imported by the Controller. If you need to pass ISession instances to your library, you can use the Controller's ctx to do that - it will already be set up by your custom Factory.
Mark Seemann
Thanks again, Mark. I'll have to try it out - nothing beats actually doing stuff, I guess. Ninject is module based so it is possible I can create a separate module in my class library that is automatically used by the IoC setup in the MVC app as well, I'll just have to try it out. Mucho gracias!
Rune Jacobsen