Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / WPF

Basics of Managed Extensibility Framework (MEF)

Rate me:
Please Sign up or sign in to vote.
3.24/5 (6 votes)
3 Jul 2014CPOL3 min read 15.2K   257   11   6
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:

Image 1

  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 

Image 2

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.

C#
    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.

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

IMyMessage

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

C#
    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.

C#
    //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

C#
    [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 

Image 3

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

Image 4

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
Artha is a Technical Lead in Windows Phone, WPF [MVVM, PRISM], ASP.NET [MVC 3.0 & 4.0], C#.NET, VB.NET and ASP.

Windows Phone

Microsoft Developer Network


Published Tools in Visual Studio Galleries


Published Source Codes in CodePlex


Microsoft Virtual Academy Profile


Published Articles in Code Project


Microsoft Certified Professional Developer (MCPD) Microsoft ASP.NET Developer 3.5 (070-564, 070-536, 070-562, 070 315)

Comments and Discussions

 
GeneralMy vote of 3 Pin
ady cody3-Jul-14 22:07
ady cody3-Jul-14 22:07 
GeneralRe: My vote of 3 Pin
Arthanarieaswaran Shanmugaraj3-Jul-14 23:23
Arthanarieaswaran Shanmugaraj3-Jul-14 23:23 
GeneralMy vote of 3 Pin
Klaus Luedenscheidt20-Jun-14 19:49
Klaus Luedenscheidt20-Jun-14 19:49 
GeneralRe: My vote of 3 Pin
Arthanarieaswaran Shanmugaraj24-Jun-14 20:02
Arthanarieaswaran Shanmugaraj24-Jun-14 20:02 
GeneralMy vote of 1 Pin
Tim Kemp20-Jun-14 3:16
Tim Kemp20-Jun-14 3:16 
GeneralRe: My vote of 1 Pin
Arthanarieaswaran Shanmugaraj24-Jun-14 20:02
Arthanarieaswaran Shanmugaraj24-Jun-14 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.