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.
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.
Average time to resolve dependencies.
|Container||Autowiring||Custom lifetimes||Interception||Auto diagnostics|
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.
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.