17

I am in the process of selecting a dependency injection framework for a new .NET application. There is a bewildering plethora of these frameworks out there, including Ninject, StructureMap, Unity, Castle Windsor, Spring.NET, etc., etc. I never believe that any one tool is absolutely better or worse than any other, however; it always depends on context and constraints.

With that in mind, can anyone offer some pros and cons of the various .NET dependency injection frameworks, vis-a-vis each other? Which are more suitable for small projects, large projects, etc.? For what types of projects, architectures, and development teams do each work best or not so well? Any wisdom from those who have worked with several of these frameworks would be very helpful...

KP Taylor
  • 2,100
  • 1
  • 17
  • 15

2 Answers2

25

Here is a nice performance benchmark:

Steven
  • 166,672
  • 24
  • 332
  • 435
5

Actually there are two comprehensive comparisons already:

I personally prefer Ninject as it favors convention over configuration. It also makes use of lambda expressions and other C# 3.0 goodies (at a cost of .net 2.0) which makes it's syntax crystal clear.

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
  • MEF isn't mentioned in either article, however it is another good candidate to consider. – STW Apr 14 '11 at 03:26
  • 1
    @Teoman I've read the Hanselman article before; it does list some general characteristics of the various frameworks (as of 2008, too, so it is a bit dated), but doesn't really put them head-to-head in a project/application context. I had not seen the Rich Newman series, and you're right, it is comprehensive! ...but it doesn't look at some popular frameworks for .NET like StructureMap, Ninject, etc. Still, +1 for a helpful answer.... – KP Taylor Apr 14 '11 at 03:27
  • and +1 for good question:) As far as I see, Ninject is the neatest IoC container but as it is relatively new (especially the 2.0 release which made it really competitive), there are not many reviews on it.. – Teoman Soygul Apr 14 '11 at 03:34
  • 2
    @STW - http://stackoverflow.com/questions/216565/why-exactly-isnt-mef-a-di-ioc-container – Phill Apr 14 '11 at 03:38
  • @Teoman I have recently used Ninject 2.0 and was very happy with it...but I keep seeing a lot of lip service being given to StructureMap (especially) followed by a few others, which is what got me wondering. – KP Taylor Apr 14 '11 at 03:45
  • @Ken - Personally I prefer StructureMap, they are all much of a muchness. – Phill Apr 14 '11 at 04:11
  • @Teoman: What do you mean Ninject favors convention over configuration? – richard Apr 14 '11 at 05:13
  • @Richard DesLonde I mean you can get singleton instance with a single line of code (convention) where as with Castle you would be settings properties first and calling the getter afterwards (which is configuration). – Teoman Soygul Apr 14 '11 at 05:22
  • @Teoman: I understand the use of the word configuration, but not convention. What does it mean that its convention? Sorry to be so dull . . . – richard Apr 14 '11 at 05:47
  • @Richard DesLonde well read it here: http://stackoverflow.com/questions/448266/is-there-a-net-ioc-that-prefers-convention-over-configuration and here: http://en.wikipedia.org/wiki/Convention_over_configuration – Teoman Soygul Apr 14 '11 at 05:50