Code Library.... C# | Microsoft.Net

MEF... (Managed Extensibility Framework)

 MEF is now a part of .Net 4.0,

MEF was around the corner for nearly some time now and it was available as one of the open source framework in Microsoft’s open source community. ( Now with lot of modifications and a finishing touch, it has been added as a new feature in Microsoft .Net 4.0. (Packed with Visual studio 2010)

So what is MEF or Managed Extensibility Framework? What is it actual use ?.

To answer, Managed Extensibility Framework (MEF) is a new library in .NET Framework 4 and Silverlight 4 that addresses the design problem of extensibility to some extend by allowing application to discover and use MEF extensions without using any configuration files. Here the application receiving extensions also should be MEF compatible and it will search for the MEF extensions on a particular path which is again declared inside the main application by any of the following ways


//An aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();
//Adds all the parts found in the same assembly as the Program class
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
catalog.Catalogs.Add(new DirectoryCatalog("C:\\myExtensions"));
//Create the CompositionContainer with the parts in the catalog
private CompositionContainer _container;
_container = new CompositionContainer(catalog);


Instead of this explicit registration of available components, MEF provides a way to discover them implicitly, via composition. A MEF component, called a part, declaratively specifies both its dependencies (known as imports) and what capabilities (known as exports) it makes available. When a part is created, the MEF composition engine satisfies its imports with what is available from other parts.

All these so called “Exports” and “Imports” are marked at method level or class level with the help of .Net attributes like “Export” “ExportMetadata”, “ImportMany”, “Import” which is available in System.ComponentModel.Composition namespace in .Net 4.0

In a way this kind of logic is widely used in many of today’s applications. So there is no wonder on the inner side workings. But MEF stands as guidance or as a re-usable framework which can be used readily with all these things built in

You can

Also download the  sample application here

blog comments powered by Disqus