Having some spare time and interest, I decided to learn a bit more about the features of modern .NET IoC frameworks. Being aware of what’s available always helps, and I haven’t yet seen a comprehensive feature comparison.

For the comparison, I have selected the frameworks that I feel to be most documented and popular:

If you feel that your framework should be here, just send me the values for all feature columns, and I’ll add it.

0. Terminology

The first thing I learned is that common concepts are sometimes named differently between the frameworks.

So I think it makes sense to start with some terminology.

Components are classes registered within the container. They can be instantiated by the container to satisfy service dependencies of other components. Services are what components provides and depends on.

In most of the cases services are interfaces, and components are classes implementing them.

Autowiring is an automatic detection of dependency injection points. If the framework is able to  match the constructor parameter types with registered services and provide these services, it is an example of autowiring.

Transient components, as compared to singletons, are created each time they are requested from container. The exact term used for transients differs quite much between frameworks.

Automatic registration is a way to automatically detect and register components within an assembly (or larger environment). It can also be called batch registration.

1. Licensing and core size

All reviewed frameworks work under .NET 2.0, and have liberal licenses allowing commercial and open-source usage.

The first table lists the reviewed frameworks with some basic parameters.

The color map is easy: green is great, yellow is ok, red is bad. All colors are extremely subjective, things bad for me may mean that I am doing something wrong.

I do not feel library count or size to be important for developments, so no red points this time.

Ninject is the most interesting case, since it has a very naive auto-wiring implementation by default. However, it has an official Ninject.Extensions.AutoWiring library, so in all following tables I included its features in Ninject functionality.

2. Container configuration

The first thing to do with container is to register something in it. So let’s review the configuration options provided by various framework.

As you can see, all frameworks support programmatic registration. Even better, most of them have fluent interfaces for registration API. Spring.Net and Castle are behind others in this regard, since Spring.Net API is somewhat cumbersome and not fluent, while Castle does not support fluent registration in RC3.

As I noted before, automatic registration is a way to register all types in given set of assemblies/namespaces based on attributes or other conventions. I find it very useful, since the convention-driven approach saves time on configuration and lessens a chance of a configuration error.

Castle’s BatchRegistrationFacility is obsolete and well-hidden, also it uses GetExportedTypes instead of GetTypes (The Sin of Shallow Digging), so it’s useless if you want to hide implementations and expose only interfaces. For Spring.NET I didn’t find an easy way, but it still might be possible, so you can correct me on this one.

Some people consider attributes to be POCO, some not, and I tend to agree with the latter when we are talking about DI frameworks. So no red points or yellow points if framework has no attributes, this column just for your information.

As for XML, I have a very strong opinion — only if actually needed, only as much as actually needed. Fortunately, no one of reviewed frameworks required XML (while most of them support it when needed). Spring.Net is somewhat differently oriented for this question, since its XML API is a better, more documented and encouraged approach. However, since Spring is a Java port, the approach is probably a compatibility heritage.

3. Basic injection/autowiring

The second thing after configuring the framework is knowing what exactly can be automatically injected. And, as I explained earlier, autowiring is just a name for framework-discovered injection points.

Basic constructor injection works great in all frameworks. For property injection Unity and StructureMap require explicit opt-in and treat the dependency as mandatory, which is a bit counterintuitive and does not provide any benefit over constructor injection.

As for multiple constructors, I really like the Castle/Autofac choice of constructor with most resolvable parameters. This is precisely what the human programmer will do, given a number of constructors and dependencies. With approach taken by Unity and StructureMap, it is harder to keep object DI-ignorant — you have to think whether your most-parameter constructor is resolvable, add DI-specific attributes or specify the constructor explicitly in the XML/code configuration.

The most interesting behavior was shown by the Spring.Net — it selected default (no-parameter) constructor first, and only if default did not exists, Spring went to the most resolvable.

Recursive dependency resolution is, in my opinion, one of the most important things in a DI framework. In a large project, when you have a lot of components and services, solving recursion easily is a must. In my tests only Castle and Autofac gave a useful error message, all other frameworks just crashed with unrecoverable StackOverflowException, which is a bad, bad thing to get in automatic test runner.

4. To be continued

In the second post of the series, I will review advanced features of DI containers (with StructureMap demonstrating unexpected feature), and show an automated feature tests I used to fill the blanks in these tables.

kick it on DotNetKicks.com

  • http://blog.ashmind.com/index.php/2008/08/20/google-reader-and-zoho-spreadsheets/ Google Reader and Zoho Spreadsheets » Blog Archive » I Think It’s Interesting

    [...] Comparing .NET DI (IoC) Frameworks, Part 1Making a Visual Studio Custom Project Type buildableSome thoughts on ASP.NET AJAX RoadmapNMock 3.5Client-side databinding with ASP.Net AJAX FuturesDI Framework Challenges, 1: Simple FactoriesDI Framework Challenges, 2: ListsMocking internal interfaces with NMock2Microformats are Web 2.0 virusSimple typeswitch in C# 3.0, Part 2: The Solutions [...]

  • sbussinger

    Nicely done! I'm looking forward to seeing part 2.

  • Damian Powell

    Great review. When are we likely to see part 2?

  • http://blog.ashmind.com ashmind

    I have been somewhat busy, but it is a next big thing I have in my blog backlog.

  • http://www.tavaresstudios.com Chris Tavares

    Interesting comparison, thanks for doing it. If I may, I'd like to clarify a couple things about Unity. You don't have to use attributes to specify which properties or constructors to call, you can configure it in the container either via API calls or via XML config.

  • http://blog.ashmind.com ashmind

    Thank you for your feedback, I added it to the comparison matrix and rephrased the text accordingly.

  • http://blog.ashmind.com/index.php/2008/09/08/comparing-net-di-ioc-frameworks-part-2/ Comparing .NET DI (IoC) Frameworks, Part 2 » Blog Archive » I Think It’s Interesting

    [...] thoughts on ASP.NET AJAX RoadmapXHTML and microformats revisitedComparing .NET DI (IoC) Frameworks, Part 1Making a Visual Studio Custom Project Type buildableClient-side databinding with ASP.Net AJAX [...]

  • http://dotnet.org.za/rudi/archive/2008/09/05/learning-prism-05-09-2008.aspx Learning PRISM – Unity – Rudi Grobler

    [...] Shchekin has a nice 2 part post about comparing popular the IoC containers (Part 1 & Part [...]

  • Matteo Baglini

    In Spring.NET you must set the properties “depends-on” and “lazy-init” in the configuration file for manage recursive dependency. See:
    http://springframework.net/docs/1.2.0-M1/refere…
    http://springframework.net/docs/1.2.0-M1/refere…

  • http://blog.ashmind.com ashmind

    It seems to be a solution to recursive dependency, however I am interested in a way to _detecting_ the recursion.
    Debugging StackOverflowException (basically, dead test runner/asp.net hosting process) is the most complex detection method.

  • http://blog.ashmind.com/index.php/2008/09/28/putting-web-snapshots-to-practical-use/ Putting Web snapshots to practical use » Blog Archive » I Think It’s Interesting

    [...] .NET DI (IoC) Frameworks, Part 2Some thoughts on ASP.NET AJAX RoadmapComparing .NET DI (IoC) Frameworks, Part 1XHTML and microformats revisitedClient-side databinding with ASP.Net AJAX FuturesDI Framework [...]

  • http://www.dbones.co.uk/blog David

    Enjoyable Read! thanks for your time..

  • http://www.dbones.co.uk/blog/post/2008/10/DI-frameworks.aspx DI frameworks

    [...] The DI Framework Comparison - Valla, this is a very well thought of compairson, an would help you all on which DI framework you would choose. [...]

  • http://www.color-chart.org color_chart

    great article… Ive never really been into .net myself but its certainly something im thinking about… your comparisons make interesting reading and maybe changed my mind in what direction to take.. very well written article thank you.

  • http://pavkul.blogspot.com Pavan Kulkarni

    Good article. I was looking for something like this.
    Thanks for sharing!

  • http://codemonkeylabs.com/weekly-web-nuggets/29/ Weekly Web Nuggets #29 : Code Monkey Labs

    [...] Comparing .NET IoC Frameworks Part 1: Andrey Shchekin takes a look at 6 of the most popular IoC frameworks available for .NET. [...]

  • http://ngocthanhit.wordpress.com/2009/03/27/inversion-of-control-unity-with-wcf/ Inversion of Control: Unity with WCF « Ngocthanhit Homepage
  • http://seminyak-villa.com/seminyak-luxury-villas/ luxury villas seminyak

    Enjoyable Read! thanks for your time. thanks for this great post and thanks for sharing this information.

  • http://dotnetforum.dk/blogs/janus/archive/2009/03/30/dependency-injection-med-structuremap.aspx Dependency Injection med StructureMap, part 1 – Janus on .NET

    [...] fandt 2 websider som har gennemgået de forskellige frameworks rimeligt grundigt, http://blog.ashmind.com/index.php/2008/08/19/comparing-net-di-ioc-frameworks-part-1/ og [...]

  • http://openid.claimid.com/ctacke ctacke

    You state in the article that “If you feel that your framework should be here, just send me the values for all feature columns, and I’ll add it.” but I'm having trouble finding exactly where I should send these values.

  • http://blog.ashmind.com Andrey Shchekin

    You can send it to ashmind [!] gmail.com.
    However, this post is extremely obsolete (year ago), so I think it is a bit unfair to add anything to it unless I update situation on all other frameworks.

    On the other hand, I can add it to the test suite here http://code.google.com/p/net-ioc-frameworks (and update the wiki which does not include all tables at the moment).

  • http://blog.ashmind.com/index.php/2009/10/12/net-di-ioc-frameworks-revisited/ .NET DI (IoC) Frameworks, Revisited » Blog Archive » I Think It’s Interesting

    [...] a year ago I wrote a comparison of .NET IoC frameworks (part 1, part 2), which turned out to be somewhat popular. I was pretty sure it is quite obsolete by [...]

  • http://bloggingabout.net/blogs/vagif/archive/2009/10/27/selecting-ioc-framework.aspx Selecting IoC framework – Vagif Abilov's blog on .NET

    [...] .NET DI (IoC) Frameworks (part1 and part2) by Andrey Shchekin. Very good comparison with source code uploaded to Google [...]

  • maciejgren

    Thank you very much for this post! I was thinking about this comparison. You made my day!

  • maciejgren

    Thank you very much for this post! I was thinking about this comparison. You made my day!

  • http://sachinchavan.in/2010/02/ioc-starter/ IOC Starter « Sachin Chavan

    [...] .NET DI (IoC) Frameworks Part 1 and Part 2 [...]

  • http://www.semanticarchitecture.net Patrick Kalkman

    Thank you for your post. Was just looking for a comparison of DI frameworks.

  • http://site.zeytin.net/technology/net-dependency-injectionioc-container-recomendations.html .NET Dependency Injection/IOC Container Recomendations? – Technology | Zeytin.Net

    [...] the new Manning book Dependency Inject With .NET and reading a lot of stuff on StackOverflow comparing containers. At the end of this, I’ve come down to NInject as my main choice with Castle [...]

  • http://programmersgoodies.com/hiro-vs-other-ioc-containers Hiro vs other IoC containers – Programmers Goodies

    [...] a look for instance at this article (and the follow up) which gives a good (feature) comparison of IOC frameworks. The writer of the [...]

  • http://hivensil.vkgu.kz/?p=26 Anonymous

    [...] рассмотрены тут,  вот тут, затем можно посмотреть здесь2. Шаблоны проектирования MVVM, MVP.  Для начала можно [...]

  • http://programmersgoodies.com/simple-injector-vs-hiro-vs-autofac Simple Injector vs Hiro vs Autofac – Programmers Goodies

    [...] I’m leaning towards Autofac since it is fairly mature and feature complete (good article here and here) as well as being one of the quickest IoC containers available. Has anyone had experience [...]

  • http://DeveloperForums.net/http:/DeveloperForums.net/net-dependency-injectionioc-container-recomendations/ Developer Forums » .NET Dependency Injection/IOC Container Recomendations? » Developer Forums

    [...] the new Manning book Dependency Inject With .NET and reading a lot of stuff on StackOverflow comparing containers. At the end of this, I’ve come down to NInject as my main choice with Castle [...]

  • http://www.semanticarchitecture.net/the-business-case-for-a-di-framework/ The business case for a DI framework | Semantic Architecture

    [...] [...]

  • http://www.gamedevportal.com/?p=1073 Clean Code Development Tools « GAMEDEVPORTAL

    [...] [...]