Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Basics of Managed Extensibility Framework (MEF)

0.00/5 (No votes)
3 Jul 2014 1  
This article explains the basics of MEF for beginner with a simple examble

Introduction

The Managed Extensibility Framework or MEF is a library for creating lightweight, extensible applications. It is a composition layer for .NET that improves the flexibility, maintainability and testability of large applications; it is loosely couple plugin architecture and easily encapsulates code.

MEF is an integral part of the .NET Framework 4, and is available wherever the .NET Framework is used. You can use MEF in your client applications, whether they use Windows Forms, WPF, or any other technology, or in server applications that use ASP.NET.

Key Concepts

  • Export
    • Export attribute decoration will introduce the class to container, any export declared with a matching contract will fulfil this import from the container.
  • Import
    • Import attribute decoration will extract the classes from the container. Every import has a contract, which determines what exports it will be matched.
  • Contract
    • The contract can be an explicitly specified string, or it can be automatically generated by MEF from a given type.
  • Parts
    • All import & exportable components are commonly known as Parts.
  • Composition
    • The core of the MEF composition model is the composition container, which contains all the parts available and performs composition. CompositionContainer discover the parts using Catalog. A catalog is an object that makes available parts discovered from some source. MEF provides catalogs to discover parts from a provided type, an assembly, or a directory.
    • Based on the discovery catalog has been classified as
      • TypeCatalog - Discovers attributed parts from a collection of types.
      • AssesmblyCatalog - Discovers attributed parts in a managed code assembly.
      • DirectoryCatalog - Discovers attributed parts in the assemblies in a specified directory.
    • AggregateCatalog - An aggregate catalog that combines multiple catalogs    
  • System.ComponentModel.Composition
    • This .Net Framework reference is provides above MEF implementation classes.

Description 

This code samples explain very simple MEF implementation, this demo application will display message in console output and window message box output.

MEFDemo solution has four projects:

  1. MEFDemo - This project discover the exportable parts using CompositionContainer and call the ShowMyMessage method
  2. MEFDemo.Interfaces - This project has simple interface IMyMessage with a method declaration ShowMyMessage
  3. MEFDemo.ConsoleMessage - This project has console output implementation of IMyMessage
  4. MEFDemo.WindowsMessage - This project has Windows Message Box output implementation of IMyMessage 

In this example DirectoryCatalog is used for discover the exportable parts from the directory DLLs (folder in the application root path) with the help of below code.

    catalog.Catalogs.Add(new DirectoryCatalog(
                (new DirectoryInfo(
                    Path.Combine(Directory.GetCurrentDirectory(), "../../../DLLs"))
                    ).FullName
                ));

TypeCatalog & AssemblyCatalog can be used for discovering the exportable parts.

Below code extract the IMyMessage contract implemented classes from the container.

    var myMessages = _container.GetExportedValues<IMyMessage>();

IMyMessage

IMyMessage interface has method declaration of ShowMessage, please see below code.

    public interface IMyMessage
    {
        void ShowMyMessage(string message);
    }

This interface is implemented in Console and Windows message display.

Console & Windows Implementation

IMyMessage interface implemented Console & Windows message for displaying difference output format of message.

    //This class can be export to CompositionContainer - If decorated with Export attribut
    // with IMyMessage as contract, then this class can be export to composition container
    // and then this can be import whenever it necessary from the container
    [Export(typeof(IMyMessage))]
    public class MyMessage : IMyMessage
    {
        
        public void ShowMyMessage(string message)
        {
            Console.WriteLine("Hi I am from {0}, {1}", "Console", message);

        }
    }

Same implementation for Windows Messagebox display, please see the code below

    [Export(typeof(IMyMessage))]
    public class MyMessage : IMyMessage
    {
        
        public void ShowMyMessage(string message)
        {
            MessageBox.Show(string.Format("Hi I am from {0}, {1}", "Window", message));

        }
    }

This ConsoleMessage and WindowsMessage are separate projects and there is no direct interact with the MEFDemo application.

Output 

By removing any one DLL from the below folder, corresponding message will not appear in the output.

Conclusion

I hope this article helps to understand some basics in MEF implementation & feel free to write your comments & question on this article.

Reference

1. http://msdn.microsoft.com/en-us/library/dd460648(v=vs.110).aspx

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here