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:

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:

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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s