MEF (Managed Extensibility Framework) vs IoC/DI

Dependency InjectionInversion of-ControlMef

Dependency Injection Problem Overview


What problems does MEF (Managed Extensibility Framework) solves that cannot be solved by existing IoC/DI containers?

Dependency Injection Solutions


Solution 1 - Dependency Injection

The principle purpose of MEF is extensibility; to serve as a 'plug-in' framework for when the author of the application and the author of the plug-in (extension) are different and have no particular knowledge of each other beyond a published interface (contract) library.

Another problem space MEF addresses that's different from the usual IoC suspects, and one of MEFs strengths, is [extension] discovery. It has a lot of, well, extensible discovery mechanisms that operate on metadata you can associate with extensions. From the MEF CodePlex site:

"MEF allows tagging extensions with additonal metadata which facilitates rich querying and filtering"

Combined with an ability to delay-load tagged extensions, being able to interrogate extension metadata prior to loading opens the door to a slew of interesting scenarios and substantially enables capabilities such as [plug-in] versioning.

MEF also has 'Contract Adapters' which allow extensions to be 'adapted' or 'transformed' (from type > to type) with complete control over the details of those transforms. Contract Adapters open up another creative front relative to just what 'discovery' means and entails.

Again, MEFs 'intent' is tightly focused on anonymous plug-in extensibility, something that very much differentiates it from other IoC containers. So while MEF can be used for composition, that's merely a small intersection of its capabilities relative to other IoCs, with which I suspect we'll be seeing a lot of incestuous interplay going forward.

Solution 2 - Dependency Injection

IoC containers focus on those things you know i.e. I know I will use one logger in a Unit Test, and a different Logger in my app. MEF focuses on those things you don't, there are 1 to n loggers that may appear in my system.

Solution 3 - Dependency Injection

Scott Hanselman and I covered this topic in more detail in the recent hanselminutes.

http://www.hanselminutes.com/default.aspx?showID=166

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalexView Question on Stackoverflow
Solution 1 - Dependency InjectionJ HealyView Answer on Stackoverflow
Solution 2 - Dependency InjectionGlenn BlockView Answer on Stackoverflow
Solution 3 - Dependency InjectionGlenn BlockView Answer on Stackoverflow