Using MEF to Set Up Dependency Injection

The Managed Extensibility Framework (MEF) is a built-in set of elements that allows you to “export” and “import” objects across projects that allows you to not rely on hard dependencies.

From Microsoft:

    The Managed Extensibility Framework or MEF is a library for creating lightweight, extensible applications. It allows application developers to discover and use extensions with no configuration required. It also lets extension developers easily encapsulate code and avoid fragile hard dependencies. MEF not only allows extensions to be reused within applications, but across applications as well.

To use MEF you need to include the following reference in your project:


We need to define a container. This is where all the exported values will be stored. One simple way to do that is to create a class such as this one:

  1. public static class Mef    
  2. {    
  3.     private static CompositionContainer container;    
  5.     public static CompositionContainer Container    
  6.     {    
  7.         get    
  8.         {    
  9.             if (container == null)    
  10.             {    
  11.                 var catalog =    
  12.                     new DirectoryCatalog(".""MyProjectNamespace.*");    
  14.                 container = new CompositionContainer(catalog);    
  15.             }    
  17.             return container;    
  18.         }    
  19.     }    
  20. }   
This will grab all the exported values from all the assemblies in the same directory starting with “MyProjectNamespace”.

And then I can annotate the classes I want to export as in the following:
  1. [Export]    
  2. public class Logger    
  3. {    
  4. }    
  6. [Export]    
  7. public class GameService    
  8. {    
  9.     [Import]    
  10.     private Logger log;    
  11. }   
Whenever I need a GameService I can request it from the container as in the following:
  1. GameService gameService = Mef.Container.GetExportedValue<GameService>();   
Notice that on GameService class I have a field with an [Import] attribute. This means MEF will resolve the value for me while it is retrieving the exported value for GameService.

I can also export a class identified by an interface:
  1. [Export(typeof(IGameService))]    
  2. public class GameService    
  3. {    
  4.     [Import]    
  5.     private Logger log;    
  6. }   
And I could use it as:
  1. GameService gameService = Mef.Container.GetExportedValue<IGameService>();  
MEF will get whatever it has in its container for IGameService.

If you have more than one export for IGameService and you attempt to resolve it with GetExportedValue you will get an exception.

You can have multiple exports, but the way you need to handle it is different.

For example, I can have a screen with several tabs that are exported as in the following:
  1. public interface ITab { }    
  3. [Export(typeof(ITab))]    
  4. public class HomeTab : ITab { }    
  6. [Export(typeof(ITab))]    
  7. public class GamesTab : ITab { }    
  9. [Export(typeof(ITab))]    
  10. public class WishlistTab : ITab { }    
And I can import them using the ImportMany attribute as in the following: 
  1. [Export]    
  2. public class Home    
  3. {    
  4.     [ImportMany]    
  5.     private List<ITab> tabs;    
  6. }    

And there is also the ImportingConstructor attribute that allows me to import objects while I am creating the instance.
  1. [Export]    
  2. public class GameService    
  3. {    
  4.     private Logger log;    
  6.     [ImportingConstructor]    
  7.     public GameService(Logger log)    
  8.     {    
  9.         this.log = log;    
  10.     }    
  11. }    
  13. [Export]    
  14. public class Logger { }   

Up Next
    Ebook Download
    View all
    View all