Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have heard of ninject and I have to build a lot of apps that have to integrate with other tools.

SO i was wondering what this was and how it is used.

share|improve this question

closed as too broad by MichaelT, Wayne M, jwenting, GlenH7, Bart van Ingen Schenau Jun 15 at 10:41

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

-1 for lazy and repetitive question. –  Boris Yankov Nov 9 '11 at 18:15
An old article but Martin Fowler describes it perfectly: martinfowler.com/articles/injection.html –  maple_shaft Nov 9 '11 at 18:15
add comment

1 Answer

What are dependency Injections :- I will try to help you understand this with examples

Without DI Container --

LinqValueCalculator lc = new LinqValueCalculator();
ShoppingCart sc = new ShoppingCart(lc);
decimal total = sc.CalculateStockValue();
Console.WriteLine("Total: {0:c}", total);

With DI Container -- (Ninject is used in this example)

IKernel ninjectKernel = new StandardKernel();

IValueCalculator calcImpl = ninjectKernel.Get<IValueCalculator>();
ShoppingCart cart = new ShoppingCart(calcImpl);
Console.WriteLine("Total: {0:c}", cart.CalculateStockValue());

How often do I use a DI Container?

Very often. Close to always.

What requirements make me do so?

In ASP.NET MVC I always use a container, because when one uses Constructor Injection in Controllers one breaks the default convention of having default constructors. This means that a custom IControllerFactory is required, and while it's possible to write and maintain one by hand, it's more work. Using a DI Container that supports convention-based configuration, one can use Constructor Injection in a convention-based manner and less maintenance is required.

Can we do without them?

Yes, but at the cost of more maintenance of infrastructure code: http://stackoverflow.com/questions/5667801/arguments-against-inversion-of-control-containers/5668093#5668093

share|improve this answer
That first piece of code looks better then the second. –  Raynos Nov 9 '11 at 18:29
I know, thats why there are two sections What requirements make me do so and Can we do without them defined in the answer ;-) –  Pankaj Upadhyay Nov 9 '11 at 18:31
I still don't get how you "break default constructors". You just changed the constructor from ShoppingCart(LinqValueCalculator) to ShoppingCart(IValueCalculator). As an aside, what's the point of ninject when you can just make LinqValueCalculator inherit from IValueCalculator? –  Raynos Nov 9 '11 at 18:40
@PankajUpadhyay: Make it clearer that the second code is separated out into module code and usage code and go into autowiring a bit (thus removing the Get<>) and you've got an excellent answer there. –  pdr Nov 9 '11 at 21:24
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.