Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decide between MEF and any IoC container?

In which scenario should we use which? How to decide on which one to choose? And under which circumstances would we choose to use both together?

I have previously worked with Unity Container (unity-container).

like image 743
Jeevan Bhatt Avatar asked Dec 10 '22 12:12

Jeevan Bhatt


1 Answers

Tricky question - since the two do indeed overlap to a certain degree.

I would say this:

  • use any useful IoC if you're primarily concerned about dependency injection for the sake of decoupling your components, e.g. for the ability to be able to inject a mock instead (for testing)

  • use MEF especially if you're more into being extensible, e.g. be able to "load all assemblies from that directory that export a certain interface", and if you need to be extensible and open for third parties (like Visual Studio: offer a public API so others can write an extension for your app). This is where MEF really shines

For MEF and Unity, there's also the MEF and Unity Integration Layer to combine the strengths of both tools together.

I would also recommend you check out Ayende's excellent blog post on what differentiates MEF from an IoC.

like image 189
marc_s Avatar answered Dec 26 '22 00:12

marc_s