Secret ingredients to quality software

Do you know the best dependency injection container? (aka Do not waste days evaluating IOC containers)

Last updated by Christian Morford-Waite [SSW] on 25 Feb 2021 04:35 am (9 months ago) See History

You can waste days evaluating IOC containers. The top ones are quite similar. There is not much in this, but the best ones are StructureMap and AutoFac. At SSW we use Autofac on most projects.

Other excellent DI containers are Ninject and Castle Winsdor. They have weaknesses, some are listed below.

Dependency Injection is an essential ingredient to having maintainable solutions. IOC containers make doing dependency injection easier.

When selecting a Dependency Injection container it is worth considering a number of factors such as:

  • Ease of use
  • Configurability: Fluent API and/or XML Configuration
  • Performance (Unless you have a very high traffic application the difference should be minimal)
  • NuGet Support (only Ninject is doing a poor job of this) - see image

The top tools all contain comparable functionality. In practice which one you use makes little difference, especially when you consider that your container choice should not leak into your domain model.

Important: Unless a specific shortfall is discovered with the container your team uses, you should continue to use the same container across all of your projects, become an expert with it and invest time on building features rather than learning new container implementations.

dic bad
Figure: Bad Example - Ninject was a top container but is no longer developed as actively as Autofac and Structuremap. Both Autofac and Structuremap have active communities and contributors that ensure they stay up to date with the latest changes in .Net

dic good
Figure: Good Example - Autofac has a great combination of performance and features and is actively developed

Note: Autofac's support for child lifetime containers may be significant for some:

StructureMap does also support a kind of child container:

Autofac web

Figure: Good Example - the web / mvc integration package layer for Autofac is developed by the same core Autofac team. Some containers (such as Structure Map) require third-party integration layers

Further Reading:

Adam StephensenAdam Stephensen
Adam CoganAdam Cogan
Brendan RichardsBrendan Richards

We open source. Powered by GitHub