Home
Do you know the best dependency injection container? (aka Do not waste days evaluating IOC containers)
  v12.0 Posted at 30/07/2019 11:06 AM by Tiago Araujo

​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.png
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.png alt=
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:
http://nblumhardt.com/2011/01/an-autofac-lifetime-primer

StructureMap does also support a kind of child container:
http://codebetter.com/jeremymiller/2010/02/10/nested-containers-in-structuremap-2-6-1/

Autofac_web.png

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:​​

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments: