Which Dependency Injection Tool Should I Use? [closed]
Asked Answered
O

9

39

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?

Outstretch answered 29/9, 2008 at 14:28 Comment(2)
Related question #2217184Bernini
ObjectBuilder is not a DI framework. It's a framework for building DI frameworks.Weismann
B
12

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

Bethea answered 30/9, 2008 at 6:0 Comment(3)
Can you please elaborate on the Unity remark? Why is it a bad choice?Sconce
after using ent lib before windsor - i have a very negative and allergic reaction to any attempt by MS to create any thing like a "policy injector"!! asp.net mvc aside :)Trefler
That is not a good way to convince by just telling not use something without any comments or infos about that (10 years later comment :)).Cabernet
M
61

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.

Minstrel answered 6/12, 2008 at 15:37 Comment(6)
Nice and intelligent reasoning.Orthogenic
Interesting to see the accepted answer says to stay away from Unity while the most voted answer chooses Unity!Renown
Yes, I thought that was interesting too. We gave equal measure to all containers and ignored anti-MS bias. Every situation is different however and one should chose the solution that is right for them at that time. As it happens we ended re-inventing the wheel and wrote our own we are going to open source soonMinstrel
blatent plug, but off the back off my research I have created a lightweight container for .Net. I believe it is currently the fastest in the industry and encapsulates what I was looking for most in a IoC. Caveat emptor: Work in progress code.google.com/p/yadicMinstrel
What happened with this answer? The last part of it seems to be missing...Mulligatawny
edited the question, so that it is displayed completely. (was because of this bug: meta.stackexchange.com/questions/14097/…)Mulligatawny
B
12

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

Bethea answered 30/9, 2008 at 6:0 Comment(3)
Can you please elaborate on the Unity remark? Why is it a bad choice?Sconce
after using ent lib before windsor - i have a very negative and allergic reaction to any attempt by MS to create any thing like a "policy injector"!! asp.net mvc aside :)Trefler
That is not a good way to convince by just telling not use something without any comments or infos about that (10 years later comment :)).Cabernet
M
12

Here is a good article which compares .NET IoC containers. http://blog.ashmind.com/index.php/2008/08/19/comparing-net-di-ioc-frameworks-part-1/

Mccrary answered 3/10, 2008 at 20:36 Comment(1)
Wow, that article has an amazing amount of detail. Totally worth the read. It also has a part 2: blog.ashmind.com/index.php/2008/09/08/…Carbineer
W
5

I started using Autofac a year ago and haven't looked back since..

Wrestle answered 15/7, 2009 at 22:3 Comment(0)
M
4

I'm an Autofac fan, but both Windsor and Unity will do a good job (though Windsor is more capable than unity and doesn't require attributing your code). There's plenty of good non technical reasons for sticking to a single container in a system though.

Modify answered 29/9, 2008 at 14:33 Comment(1)
What makes you say (implied) that Unity requires you to attribute your code? I'm using it and don't have to attribute my code at all.Augustaaugustan
S
2

Use what works. Most have features that are unique to them, and almost all are more feature-rich than Unity. If unity is all that you need, you can certainly use it.

Using Microsoft's unity just because it's from Microsoft is a poor way to make a decision. Think about what you need and why, and choose the one that fits your needs.

However, I second the notion of sticking to a single container if possible.

Stripy answered 29/9, 2008 at 14:36 Comment(0)
M
2

I've been using the Managed Extensibility Framework and found it quite easy to work with. It's been integrated into .NET 4.

Marylnmarylou answered 2/2, 2010 at 20:58 Comment(2)
MEF is not a IoC container: #42751Archbishopric
@Mauricio Scheffer: MEF uses a dependency injection pattern in solving many architectural problems people are facing when they start to consider bringing an IOC/DI framework into the project. The OP does not provide any indication that his needs stray outside the bounds of what MEF excels at, in which case it could be an easy, viable, and extremely lightweight option.Marylnmarylou
B
0

Unless you already have experience and a personal preferance for a particular sub-technology utilized by one of the possible IoC container solutions, they all function well and I don't see any one in particular with a "killer function" that makes it stand out from the others. Unity is probably the best fit for solutions already utilizing the P&P Enterprise Library 4.x...

Bautram answered 15/11, 2008 at 22:11 Comment(0)
J
0

IoC Container Benchmark - Performance comparison has performance and features comparison tables for 20+ products and keep them up-to-date.

The conclusion from the article:

SimpleInjector, Hiro, Funq, Munq and Dynamo offer the best performance, they are extremely fast. Give them a try!

Especially Simple Injector seems to be a good choice. It's very fast, has a good documentation and also supports advanced scenarios like interception and generic decorators.

Jackelinejackelyn answered 22/2, 2013 at 6:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.