MEF is not An IoC container; but MEF uses IoC

Somehow I got on the conversion of MEF while chatting with Glenn Block.  IoC came up in that conversion.  I believe, at some point, I said something along the lines of MEF is not an IoC container; but MEF uses IoC.  Someone else asked me to clarify that after the conversation.  It’s a common misconception that MEF *is an* IoC container.  I thought it might be useful to summarize those conversations for others.

Part of what gives MEF the ability to do what it does is most certainly IoC.  Traditional dependencies (control) are inverted so that something (the host) doesn’t depend on a concretion (the extension, in the case of MEF) but an abstraction.  The abstraction with MEF and IoC is an interface.

MEF manages extensions—dependencies—that may or may not exist at run-time but are rarely known/exist at compile-time.  This needs to occur because you want 3rd parties to extend your application (of course, conceivably you must produce and publish your application before a 3rd party can even conceive of extending it).  For any one dependency, MEF may be managing multiple extensions.

The difference with an IoC container is that it’s managing static dependencies: dependencies that must exist at compile-time in order for the application to correctly run at run-time.  The impetus of IoC is different than MEF in that you don’t want to offer the ability to “extend” your application, but ensure that a particular class doesn’t have a direct coupling (or dependency) on another class.  IoC doesn’t remove the dependency entirely, it just means the code can evolve independently.  For the application to run correctly when deployed, it depends on ClassA being injected into ClassB at some point for that to happen.  But, ClassA can compile without ClassB.  This is always a one-to-one dependency.

If MEF were truly an IoC container then you’d expect be able to use an IoC container to extend an application at runtime—which is not the case.

Published Wed, Feb 24 2010 12:32 by PeterRitchie

Comments

# re: MEF is not An IoC container; but MEF uses IoC

Thanks for your explanation, but I still do not understand their relationship. If MEF uses IoC, which IoC container (e.g. unity, ninject) it uses, or does the developer have the right to choose?

Thursday, February 25, 2010 8:59 AM by John

# re: MEF is not An IoC container; but MEF uses IoC

To be clear, MEF uses the concept of Inversion of Control; but doesn't use an external Inversion of Control container (that I'm aware of--I don't know of any that support what MEF is trying to do).

One of the issues that MEF attempts to solve is the lack-of design/compile-time availability of the components that it's loading.  This isn't generally a scenario that most IoC containers try to deal with.  Most IoC containers assume that the loaded types are required by the application in order to continue and simply fail the application if they're not.  MEF supports extensions; extensions to existing functionality.  So, MEF often deals with optional components, components that if are not available at run time the application is free to continue running.

Friday, February 26, 2010 6:39 PM by PeterRitchie

# re: MEF is not An IoC container; but MEF uses IoC

The way you describe it, it is not that MEF is not an IoC container, rather it is more than an IoC container. Since you believe that MEF is not an IoC container, can you explain why you would not choose to use it as an IoC container?

Monday, March 22, 2010 1:27 PM by David Nelson

# re: MEF is not An IoC container; but MEF uses IoC

@David.  It's not that MEF can't be used as an IOC or a DI container, clearly it could be.  And, in fact, some of the code examples for MEF show using it simply as a DI container.

MEF is about extensibility.  Extending an application involves increasing its scope and to accept new functionality.  A DI container is about decoupling and managing dependencies--dependencies that the software system needs in order to run.  Dependencies are requirements known at design/compile time.

A system shouldn't be dependent on extensions in order to run--clearly that's not "extending" functionality.  Yes, you must design DI into your system in order to support extensibility, because you can't couple to components you don't know exist.  But, MEF doesn't simply become a DI container because it manages injection of extensions.

Monday, March 22, 2010 3:08 PM by PeterRitchie

# re: MEF is not An IoC container; but MEF uses IoC

you failed to make your point, you can't really explain the difference

Tuesday, November 02, 2010 2:38 PM by greg

# re: MEF is not An IoC container; but MEF uses IoC

@greg

Actually, I think he explained it quite well.

Perhaps you should re-read and consider the possibility that you just didn't "get" it /

Wednesday, November 24, 2010 11:59 AM by nchaud

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: