DOliva Dev

Inversion of Control (IoC) Containers Basic Comparison between some IoC Containers

Inversion of Control (IoC) Containers Basic Comparison between some IoC Containers

Introduction

Inversion of control containers allows developer to apply dependency injection principle to their projects. The market is saturated of open source and/or free IOC containers. This document intent to do a basic comparison of the most recognized IoC Containers based on configuration, features, performance and supported environment.

There are more than 40 recognized containers. The table below shows some of the most popular.

AutoFac fFastInjector Hiro Maestro Munq Speedioc TinyIoc
Caliburn.Micro Funq IfInjector MEF Ninject Spring.NET Unity
Catel Grace LightCore MEF2 Petite Stiletto Windsor
DryIoc Griffin LightInject MicroSliver QuickInject StructureMap No
Dynamo HaveBox LinFu Mugen SimpleInjector StyleMVVM Unity

 

The containers used for this article are: AutoFact, DryIoc, Dynamo, LinFu, Ninject, SimpleInjector, StructureMap, Unity, and Windsor .
To select IoC containers some of the factors that should be considered are Configuration, Features, Performance and Environment. Configuration can be defined as the way of setting the IoC. There are three basic ways: Code, XML, or Auto. Code configuration use a programming language to defined mapping between interfaces and implementation. XML configuration allows to configure the dependencies in XML files avoiding to compile again projects to change implementation. Auto configuration check among other the naming convention to find the interfaces implementation. Commons features of IoCs are Auto wiring, Interception, and Auto diagnostic. Environment could be defined as .NT, SL, WP7, WP8, and WinRT.

Performance

Average time to resolve dependencies.

performaceIoc

Container Performance
AutoFac Average
DryIoc Fast
Dynamo Fast
LinFu Slow
Ninject Slow
SimpleInjector Fast
StructureMap Average
Unity Average
Windsor Average

 

Configuration

 

Container Code XML Auto
AutoFac Yes Yes Yes
DryIoc Yes    
Dynamo Yes Yes
LinFu Yes    
Ninject Yes Yes Yes
SimpleInjector Yes Yes
StructureMap Yes Yes Yes
Unity Yes Yes Yes
Windsor Yes Yes Yes

 

Features

 

Container Autowiring Custom lifetimes Interception Auto diagnostics
AutoFac Yes Yes  
DryIoc Yes Yes    
Dynamo Yes Yes    
LinFu Yes Yes  
Ninject Yes Yes  
SimpleInjector Yes Yes Yes Yes
StructureMap Yes Yes Yes Yes
Unity Yes Yes Yes
Windsor Yes Yes Yes Yes

 

Autowirinng allows automatically resolve classes for your object by inspecting the contents of the IoC container. This can be done by name (eg. ICaller will resolve Caller), by type, constructor, and auto detection.

Custom Lifetimes allows to create resolved instances in different ways. For instance as Singleton or Transients.

Interception allows to handle cross-cutting concerns such as logging, transaction management, security, and so on at a different level.

Auto diagnostics allows among other to analyze the container’s configuration to search for common configuration mistakes.

Environment

 

Container .NET SL WP7 WP8 WinRT
AutoFac Yes Yes Yes Yes Yes
DryIoc Yes        
Dynamo Yes        
LinFu Yes        
Ninject Yes Yes Yes Yes
SimpleInjector Yes Yes Yes Yes
StructureMap Yes Yes Yes
Unity Yes Yes   Yes Yes
Windsor Yes Yes      

 

 

 Conclusion

 

The popular containers containers such as Ninject, Unity, and Windsor support more features and environments than newer IoC containers. However newer container has in general better performance.   The most popular container based on nugget package download is Ninject. However, is  very slow.  Dryloc and SimpleInjector performance very well and supported complex scenarios as show in the previous tables. However, in my opinion Unity is one of the best selection because is not a new IoC container. It is supported by Microsoft, is not the faster one but is not slower, and it has a considerably community using it.

Links

http://autofac.org/

https://bitbucket.org/dadhi/dryioc

https://github.com/MartinF/Dynamo.IoC

https://code.google.com/p/linfu/

http://www.ninject.org/

https://simpleinjector.codeplex.com/

http://docs.structuremap.net/

https://www.nuget.org/packages/Unity/

https://github.com/castleproject/Windsor/blob/master/docs/README.md

 

More documentation

http://anthonysteele.co.uk/comparing-.net-ioc-containers-part-zero-groundwork

http://cardinalcore.co.uk/2015/01/28/ioc-battle-in-2015-results-using-ninject-think-again/

https://rules.ssw.com.au/do-you-know-the-best-dependency-injection-container-(aka-don%E2%80%99t-waste-days-evaluating-ioc-containers)

http://www.palmmedia.de/blog/2011/8/30/ioc-container-benchmark-performance-comparison