Click here to Skip to main content
Click here to Skip to main content

Basics of Managed Extensibility Framework (MEF)

, 3 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Arthanarieaswaran
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 Pinmemberady cody3-Jul-14 23:07 
GeneralRe: My vote of 3 PinmemberArthanarieaswaran4-Jul-14 0:23 
GeneralMy vote of 3 PinmemberKlaus Luedenscheidt20-Jun-14 20:49 
GeneralRe: My vote of 3 PinmemberArthanarieaswaran24-Jun-14 21:02 
GeneralMy vote of 1 PinmemberTim Kemp20-Jun-14 4:16 
GeneralRe: My vote of 1 PinmemberArthanarieaswaran24-Jun-14 21:02 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 4 Jul 2014
Article Copyright 2014 by Arthanarieaswaran
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid