Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Dependency Injection Tool Should I Use? [closed]

I am thinking about using Microsoft Unity for my Dependency Injection tool in our User Interface.

Our Middle Tier already uses Castle Windsor, but I am thinking I should stick with Microsoft.

Does anyone have any thoughts about what the best Dependency Injection tool is?

  • Autofac
  • Castle MicroKernel/Windsor
  • PicoContainer.NET
  • Puzzle.NFactory
  • Spring.NET
  • StructureMap
  • Ninject
  • Unity
  • Simple Injector
  • NauckIT.MicroKernel
  • WINTER4NET
  • ObjectBuilder
like image 563
Elijah Manor Avatar asked Sep 29 '08 14:09

Elijah Manor


People also ask

Which tool is used for dependency injection?

Spring.NET is one of the popular open source frameworks for Dependency Injection. Spring.NET supports . NET 4.0, . NET Client Profile 3.5 and 4.0, Silverlight 4.0 and 5.0, and Windows Phone 7.0 and 7.1.

Should I use IoC container?

The most valuable benefit of using an IoC container is that you can have a configuration switch in one place which lets you change between, say, test mode and production mode. For example, suppose you have two versions of your database access classes...

Which IoC container is best?

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.

Which is the right way to inject dependency in Java?

The injector class injects dependencies broadly in three ways: through a constructor, through a property, or through a method. Constructor Injection: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.


2 Answers

Having recently spiked the use of 6 of these (Windsor, Unity, Spring.Net, Autofac, Ninject, StructureMap) I can offer a quick summary of each, our selection criteria and our final choice.

Note: we did not look at PicoContainer.Net as one of our team considered the .Net port to be quite poor from the Java version. We also did not look at ObjectBuilder, as Unity is built on top of ObjectBuilder2 and was considered to be a superior choice by default.

Firstly, I can say that more or less all of them are all pretty similar and it really comes down to what really works best for you, and your specific requirements. Our requirements included:

Requirements

  • Constructor based injection (we intend not to use property, field or method injection)
  • Programmable configuration (not XML)
  • container hierarchies (one per application, per request and per session to more implicitly bind component lifetimes scope to container)
  • component lifetime management (for more granular scoping eg transient / singleton)
  • injection from interface to type or concrete instance (eg ILogger -> typeof(FileLogger) or ILogger -> new FileLogger() )
  • advanced component creation / "creaton event mechanism" for pre/post initialisation
  • correct disposal of IDisposable components on container tear down
  • well documented and/or online information readily available

    Note: whilst performance was a requirement it was not factored in the selection as it seemed that all containers reviewed were similar according to this benchmark

Test

Each container was used in a typical Asp.Net webforms project (as this was our target application type). We used a single simple page with with a single simple user control, each inheriting from a base page / base control respectively. We used 1 container on the BasePage for a "per request" scope container and 1 on the global.asax for an "application" scope and attempted to chain them together so dependencies could be resolved from both containers.

Each web application shared a contrived set of domain objects simulating multi-levels of dependency, scope type (singleton/transient) and also of managed and unmanaged classes (IDisposable required). The "top level" dependency components were manually injected from the methods on the BasePage.

Results

Windsor - Satisfied all the criteria and has a good case history, blogger community and online documentation. Easy to use and probably the defacto choice. Advanced component creation through Factory facility. Also allowed chaining of individually created containers.

Spring.Net - Verbose and unhelpful documentation and no-obvious / easy for programmable configuration. Did not support generics. Not chosen

Ninject - Easy to use with good clear documentation. Powerful feature set fulfilling all our requirements except container hierarchies so unfortunately was not chosen.

StructureMap - Poorly documented, although had quite an advanced feature set that met all of our requirements, however there was no built-in mechanism for container hierarchies although could be hacked together using for loops see here The lambda expression fluent interface did seem a little over complicated at first, although could be encapsulated away.

Unity - Well documented, easy to use and met all our selection criteria and has an easy extension mechanism to add the pre/post creation eventing mechanism we required. Child containers had to be created from a parent container.

Autofac - Well documented and relatively easy to use, although lambda expression configuration does seem a little over complicated however, again, can be easily encapsulated away. Component scoping is achieved through a "tagging" mechanism and all components are configured up front using a builder which was a little inconvenient. Child containers were created from a parent and assigned components from a "tag". Allowed generic injection.

Conclusion

Our final choice was between Windsor and Unity, and this time around we chose Unity due to its ease of use, documentation, extension system and with it being in "production" status.

like image 111
Xian Avatar answered Sep 28 '22 05:09

Xian


Sticking to one container is not really important, if your system has been designed with the IoC/DI in mind. With the proper approach you can easily change the IoC library down the road.

And, of course, the container has to provide enough flexibility to support common widely used scenarios (lifecycle management, proper container nesting, XML and code configuration, interception, fast resolution).

I'd recommend to pick between Castle (widely used and have a lot of integration libraries) and Autofac (lightweight, fast and has proper container nesting, but is not that widely used)

There is a comprehensive list of IoC containers by Hanselman

PS: You do not want to use Unity

like image 21
Rinat Abdullin Avatar answered Sep 28 '22 04:09

Rinat Abdullin