Why MEF

By David Giard


Microsoft Managed Extensibility Framework (MEF) is a framework for building extensible applications. Using MEF, you can build extensible applications constructed of loosely-coupled composable parts. By constructing an application of parts, any part can be replaced at runtime, without recompiling or redeploying the entire application. 
Scenarios for MEF
One use of MEF is to create an extensible application with a plug-in architecture, allowing users to extend it or to replace parts of it, without recompiling. As such, you would not need to release the source code along with your application. By creating extension points, users need only learn those extension points and the MEF framework in order to create plug-ins that export the contract expected at those extension points. This avoids the messy problems associated with users modifying source code, such as inadvertently breaking functionality or making it upgrades difficult.
MEF can also prove useful if you want to release modules of a software package to your users at different times and you do not want to recompile the entire application. Using MEF, each module could be dropped into a well-know folder and your application would use MEF to monitor that folder and integrate any module assemblies found there. This scenario is common with accounting systems. A vendor may create dozens of modules and customers may purchase any or all of them. A customer may initially buy the General Ledger, Accounts Payable and Accounts Receivable module; then decide at a later date to add the Inventory and Payroll modules. Using MEF, the software vendor can make it easy for the customer to add these extra modules months after the application was installed.
Finally, MEF can be used to manage all the interdependencies of an application. Doing so, keeps the components of the application loosely-coupled and allows developers to more easily swap out parts of the application. Because MEF dependencies are built using contracts, rather than explicit assembly references, changing a component requires no recompile - the new component must only satisfy a contract expected by the application.
Single Extensibility Model
Microsoft already has several existing technologies to accomplish similar things. For example, Visual Studio 2008 and Microsoft Office 2007 each has a plug-in framework that allows users to extend the application. MEF promises a single extensibility framework that can be used across all Microsoft applications. This frees developers from the need to learn a different framework to extend each application. In fact, the upcoming Visual Studio 2010 (currently in beta) is built on top of MEF, so that developers can use MEF to add plug-ins to the IDE.
Advantages Over Existing Extensibility Technologies
There are existing technologies built into the .Net framework that allow you to extend applications at runtime. 
In the current version of .Net, we can code to interfaces, instead of concrete classes. Doing so gives us the ability to defer to runtime which class to instantiate. Our code is flexible enough to accept any class, as long as that class implements the expected interface. However, we must decide at compile time all possible classes that might be instantiated at runtime. This is because, in most cases, we cannot instantiate a class without setting a reference to the assembly in which that class resides. And setting references is something done prior to compiling. Using MEF, we can instantiate classes even if there is no explicit reference set. MEF takes care of that for us.
Final Thoughts
Managed Extensibility Framework promises to help us build loosely-coupled, extensible applications without forcing developers to learn a new skill set for each application. It does so without the disadvantages of forcing a recompile and loading classes into memory unnecessarily when an application is extended at runtime.
You can learn more about this framework at http://www.codeplex.com/MEF.
 
Note: As of this writing, MEF is in Preview 7 and is planned to be released as part of .Net 4.0
 
David Giard has been developing solutions using Microsoft technologies since 1993. In the past, he has spoken at Day of .Net, CodeStock, Microsoft DevCares, Microsoft ArcReady, Dot Net University, X Conference and numerous user groups around the Midwest. He is a recovering certification addict and holds an MCTS, MCSD, MCSE, and MCDBA, as well as a BS and an MBA. He is the host and producer of the popular online TV show Technology and Friends. He is an officer of the Great Lakes Area .Net User Group. He is an avid photographer and has visually documented many of the Heartland community events. You can read his latest thoughts at www.DavidGiard.com. David lives in Michigan with his two teenage sons.