If you’ve heard anything about the new feature set of Visual Studio 2010 one of the game changing features for any developer is the Built in Extension Manager, this functionally is using MEF. What exactly is MEF? The Managed Extensibility Framework is as Framework for creating loosely-coupled, extensible applications. I am blogging this from the TechEd North America Session, When and Where to use MEF: Too much is never enough.
Where can I find MEF?
No MEF isn’t found in your local dark alley, and you don’t need to know a guy that knows a guy in order to use MEF. MEF is baked into the .NET framework comes out of the System.ComponentModel.Composition (2.0.5.0 for Silverlight, 4.0 for .NET 4.0) assembly.
Parts of MEF
Catalogs
A Catalog in terms of MEF is a class that contains metadata about your particular application so that it is able to pull in the particular Imports and Exports.
Exports
Exports in MEF are a way of exposing contracts to the framework when a particular class type is needed to be implemented into your application. This is useful when you need to use multiple implementations of an Interface to cover a particular task, like logging.
[Export[typeof(ILogger)]
public class RedGreenLogger
{
// Implement Class here
}
Imports
Imports in MEF is a way of stating that a concrete implementation is needed within the application. You will attribute a method with a particular importer when you would like MEF to fill in the blanks with an export or many exports for that particular task.
[ImportMany(typeof(ILogger)]
public List<ILogger>() Executors
{
// Add Implementation here
}
Containers
A container is a wrapper used to collect the Catalogs at a hi
public void Init(Executor e)
{
var container = new CompositionContainer(new DirectoryCatalog(Environment.CurrentDirectory));
container.SatifsyImportsOnce(e);
}
Composition
Composition is the piece of MEF that pulls all the parts of together. This is a call that you need to do explicitly, however depending on the implementation composition can be extremely easy or increasingly more difficult. When using Silverlight you can do this very simply by calling a method and passing the main page into the application and MEF runs automagically.
CompositionContainer.ComposeParts(this);
I can make a Living Application?
Due to the fact that you’re creating an abstracted [loosely-coupled] application, and how MEF can be used to create dynamically loaded components, you have the ability to use MEF to add new features to your application, or add additional functionality to your application without recompiling your application.