Managed Extensibility Framework (MEF)

MEF is a component of .NET framework 4.0, to create lightweight, extensible applications. It avoids hard dependencies and lets the application developer discover and use extensions without any configuration required.
 

Why MEF?

 
Imagine a situation where an application is using several smaller components. And the application is responsible for creating and running those components.
 
One possible solution could be including all the components as source code in the application. But you cannot add new components without modifying the source code.
 
Another solution could be to provide an interface for decoupling between application and components. So the component can implement the interface and interact with the application. But this approach has a drawback. As the application cannot discover components by itself, it must be explicitly told which components are required and loaded.
 
Here MEF comes into the picture. MEF provides a way to discover components via composition. A MEF component specifies both its dependencies (known as imports) and what capabilities (known as exports) it makes available.
 
Let's understand it with the help of an example. Suppose you are making a simple calculator application that currently supports addition and subtraction.
 
Creating a composition container and catalog
 
Composition container keeps track of which components are available for composition and what are their dependencies. It provides a way by which an application can get the instance of components to be composed. We need to include System.ComponentModel.Composition in reference.
  1. //An aggregate catalog that combines multiple catalogs  
  2. var catalog = new AggregateCatalog();  
  3. //Adds all the parts found in the same assembly as the current class  
  4. catalog.Catalogs.Add(new AssemblyCatalog(typeof(this).Assembly));  
  5.   
  6. //If parts are placed at some other location then adds that directory path  
  7. //catalog.Catalogs.Add(new DirectoryCatalog(componentsDirectoryPath));  
  8.   
  9.   
  10. //Create the CompositionContainer with the parts in the catalog  
  11. CompositionContainer _container = new CompositionContainer(catalog);  
  12.   
  13. //Fill the imports of this object  
  14. try {  
  15.     this._container.ComposeParts(this);  
  16. catch (CompositionException compositionException) { Console.WriteLine(compositionException.ToString()); }  
  17. catalog.Catalogs.Add(new AssemblyCatalog(typeof(this).Assembly)); 
This adds the components from the current assembly. To add the components from some specified folder location, add a directory catalog like:
  1. catalog.Catalogs.Add(new DirectoryCatalog(componentssDirectoryPath));  
Where componentsDirectoryPath is the path of the directory where components can be found.
 
Import and Exports
 
Define an interface like it.
  1. [Import(typeof(IOperation))]  
  2. public interface IOperation  
  3. {  
  4.     string Operate(int leftOperand, int rightOperand);  
  5. }  
This interface has an attribute Import. This ImportAttribute defines that the type IOperation needs to be imported. So we implement the IOperation class, and above the class, we use ExportAttribute indicating that it has the capabilities of IOperation. Also, it contains ExportMetadata attribute indicating that depending on the metadata Symbol the operation is performed.
  1. [Export(typeof(IOperation))]  
  2. [ExportMetadata("Symbol"'+')]  
  3. class Add: IOperation  
  4. {  
  5.     //Implementation of IOperation  

Now we will do lazy initialization for getting objects like this:[ImportMany] IEnumerable<Lazy<IOperation, IMetadata>> operations;Lazy initialization is used so that only operations that are needed are initialized. Based on the metadata it initializes what operation is to be performed. It contains ImportMany attribute because IOperation can be filled by many exports like add, subtract, etc. Lazy InitializationAs we see we declare an IEnumerable for lazy initialization. Now how does it work? When we run the application, it initializes the catalog and creates a container. Based on the Import attribute it finds the components which can be filled for it. Here we have only one class Add to fill it. So operations will contain only a single Lazy<IOperation, IOperationData> object, and that object will be initialized when it will be accessed the first time. So based on the operation we will call the Operate function.
  1. foreach(Lazy < IOperation, IOperationData > i in operations) {  
  2.     if (i.Metadata.Symbol.Equals(operation))  
  3.         result = i.Value.Operate(left, right).ToString();  

I have a sample project to illustrate it. It contains 2 operations Add and Subtract in the assembly and Multiplication in a separate dll, which can be found in "<current executing assembly path>\Extensions" folder.