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.
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 - 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 Sep 25 '09 at 10:03 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: github. Com/mvccontrib/MvcContrib/blob/… 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 Sep 25 '09 at 10:42 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 Sep 25 '09 at 11:03 I'm currently working with a home-grown version of a dependency resolver and want to switch to one of the popular frameworks.My current method has all concrete classes in my business assembly marked as internal.
To keep the DI Container in the Global. Asax I would have to expose my concrete classes so they can get resolved, correct? Is there another way to keep the DI container in the composition root while hiding (or at least preventing the instantiation of) my concrete classes directly?
P.S. I tried posting this on your blog but got an error. – adam0101 Mar 15 at 23:08 1 Yes, you'll need to make the classes public. That's appropriate in any case, since the underlying purpose of DI is loose coupling, and thus maintainability.
This means that you'd want well-designed classes to stay maintainable, and if you have that, there's no reason to hide them away. In any case, interfaces act as a level of insulation as well: blog.ploeh.Dk/2011/02/28/InterfacesAreAccessModifiers. Aspx – Mark Seemann 01 Mar1 at 8:51.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.