DI Containers – comparison

By on (tags: IoC, categories: None)

This blog shortly describes a few chosen DI containers and their out-of-the-box functionality. If a certain container does not have a specific platform listed, it doesn’t necessarily mean that it’s not possible to use it there – it just means that it’s not in the box.

Castle Windsor

License: Apache 2
Target platforms: .Net, Silverlight
Complexity: Castle.Core, Castle.Windsor + facilities (plugins)
Acceptance: 3500 downloads nuget, 8600 downloads site
Age: ver 2.0 in 2009
Last Release: 02.2011
Features:

  • configurable in xml
  • autodiscovery
  • well documented

Sample code:

   1: var container = new WindsorContainer();
   2: container.Install(FromAssembly.This());
   3: var king = container.Resolve<IKing>();
   4: king.RuleTheCastle();
   5: container.Dispose();

StructureMap

License: Apache 2
Target platforms: .Net
Complexity: StructureMap, StructureMap.AutoMocking (for unit tests)
Acceptance: 3500 downloads nuget, 10 000+ downloads site
Age: since 2004
Last Release: 02.2010
Features:

  • configurable in xml
  • autodiscovery (extensive)

Sample code:

   1: ObjectFactory.Initialize(x =>
   2:                              {
   3:                                  x.ForRequestedType<IValidator>()
   4:                                  TheDefaultIsConcreteType<Validator>();
   5:                              });
   6: IValidator validator = ObjectFactory.GetInstance<IValidator>();

Spring.Net

License: Apache 2
Target platforms: .Net, MVC, WCF,
Complexity: Spring.Core.Configuration
Acceptance: 60 downloads nuget
Age: since 2007
Last Release: 12.2010
Features:

  • configurable in xml

Sample code:

   1: [Configuration]
   2: public class ControllerConfiguration
   3: {
   4:     [Definition]
   5:     [Scope(ObjectScope.Prototype)]
   6:     public virtual HomeController HomeController()
   7:     {
   8:         HomeController controller = new HomeController();
   9:         controller.Message = "MVC powered by Spring.NET!";
  10:         return controller;
  11:     }
  12: }

Autofac

License: MIT
Target platforms: .Net, MEF, MVC, WCF, WEB, Community, Silverlight
Complexity: Autofac, Autofac.Configuration + (MEF, MVC, WCF, WEB and community Prism, WCF Ria Services, Log4Net)
Acceptance:  3000 downloads nuget, 800+ downloads site
Age:
Last Release: 03.2011
Features:

  • confugurable in xml
  • autodiscovery

Sample code:

   1: var builder = new ContainerBuilder();
   2: builder.RegisterAssemblyTypes(controllerAssembly);
   3: var container = builder.Build();
   4: var taskController = container.Resolve<TaskController>();
 

Unity

License: MS-PL
Target platforms: .Net, Silverlight
Complexity: Microsoft.Practices.ServiceLocation, Microsoft.Practices.Unity, Microsoft.Practices.Unity.Configuration, Microsoft.Practices.Unity.Interception, Microsoft.Practices.Unity.Interception.Configuration
Acceptance: 4000 downloads nuget, 50 000+ downloads site
Age: since 2005
Last Release: 05.2010 (seems out of data – projects fo vs 2008)
Features:

  • configurable in xml
  • autodiscovery

Sample code:

   1: IUnityContainer container = new UnityContainer()
   2:     .RegisterType<ILogger, TraceLogger>()
   3:     .RegisterType<IStoplightTimer, RealTimeTimer>();
   4: var form = container.Resolve<StoplightForm>();