IoC Containers: Some Comments.

ProfileQuick on-the-way-post:

Lately i worked a lot with Unity and MEF, Microsofts IoC and Code-Composition solutions. I want to answer 2 questions flying around there for a while:

Q: Is MEF an IoC Container ?

A: Yes, it is. It has his limitations (like IoC containers are limited too, when it comes down to object composition, where MEF, on the other side, is really strong), but yes, it is an IoC container.

Q: Should i use Unity or MEF for dependency injection ?

A: It depends on your needs. Read this excellent article by Peter Vogel, released in the Visual Studio Magazine:

http://visualstudiomagazine.com/articles/2013/04/01/unity-vs-mef.aspx

tl;dr: Use MEF if you need deep and complex object composition, fine-granulary resolve filtering and especially composition from dlls, external stuff and so on, for plugin-like solutions and scenarios. Use Unity if you need interceptions for crosscutting concerns or if you want simple IoC, without the complexier usage of MEF (in MEF you bind to properties via attributes and therefore need some (helper)class, whereas in Unity you directly get the resolved object).

As a side note to Microsoft: It is REALLY time for bundling Unity and MEF functionalities in a new straight redesigned library and deliever it by default with the .NET Framework.

Second thing i want to publish on-the-way is, to differentiate and critisize IoC containers in general. Personally i like em and use em. But i really don’t know, if i should like them. They clearly lead to anti-patterns and therefore sometimes can cause more problems then they solve. Don’t get me wrong: We need dependency injection, or better told, we need max decoupling between parts of software. I just think IoC is not ALWAYS the best approach. But some IoCs want to be the masters of all decoupling. And “one size fits it all” has never really worked well. I have not the time, to go into deeper details here, but as some starting point, read that short but great article by David Arno here:

http://www.davidarno.org/2013/11/13/are-ioc-containers-a-case-of-the-emperors-new-clothes/

Also have a look at the comments, what Mark Seeman (we can call him a DI-Specialist without worries) wrote. Very interesting.

Conclusion: Think about WHAT decoupling mechanism you need WHEN, and if you need an IoC, choose wisely.