MEF for Beginner (concept) – part 1




MEF for Beginner – part 1

this post is the first in a series of posts of How To start using the new MEF (Manage Extensibility Framework).

the series will include simple steps for using MEF on the .NET, Silverlight and Compact Framework platform.


the series TOC is available here.


MEF Concept

part 1 will focus on the MEF concept, while the following posts will focus on the implementation details.

What is MEF?

the MEF framework is the Extensibility (plug-ins) standard for the .NET

The MEF team declare MEF as

The Managed Extensibility Framework (MEF) is a new library in .NET

that enables greater reuse of applications and components. Using MEF,

.NET applications can make the shift from being statically compiled to dynamically composed.


The core concept of MEF

MEF Extensibility  CLR 4   The essence of MEF paradigm is built upon the idea of needs and part

  that can be discovered (in order of satisfying the needs).

  MEF assume that application is build from parts, each part may have needs to

  consume other parts, or may be discover and consumed by other.

  in general parts consumers care about the contract and doesn’t care about the parts implementation.


MEF is using the following terms to address the above paradigm:

    Import: Define needs in term of Contract (the contract present the capabilities that is needed).

    Export: Exposing discoverable parts (that latter can be instantiate and consumed by the Import),

                      the Export is also Contract based.

    Compose: is handle by the MEF engine, it responsible of putting the pieces together
                        (discover and instantiate the matching Exports, and hand it to the Imports).

                        as analogy you can think of it as having Guilds that publish its policy (shoemaker, tailor, ext…),

                        and Peoples that declaring their professions, the MEF infrastructure apply the role of

                        matching the Peoples to their suitable Guilds.


MEF Extensibility  CLR 4 


MEF Principles

MEF is following Bertrand Meyer‘s proposed the “Open Closed Principle” which say:

MEF Extensibility  CLR 4

that way you can architecture the your solution to consume unknown implementation of parts.


What does MEF bring to the table?

actually MEF is all about breaking the tightly coupled dependencies within your solution

without loosing the type check safeness.


Addressing the Unknown

MEF addressing potentially unknown parts using its Compose mechanism which will be responsible

to discover and instantiate the parts that satisfy the needs of the Imports.

Show me the Code

the next parts of this series will focus on how to use the MEF framework in more details

and give the practice needed for building your first MEF application.

but i wont live you without having the taste of how does it implement.

for the first taste i will borrow sample from Glenn Block presentation:

the scenario Glenn present is a Rule engine.

the first part is a single Rule that has need for logger.

Code Snippet
  1. [Export(typeof(IRule))]
  2. public class DiscountRule : IRule
  3. {
  4.     [Import]
  5.     public ILogger Logger { get; set; }
  6.     public void Execute(ref double value)
  7.     {
  8.          Logger.Log("Executing rule");
  10.          …
  11.     }
  12. }

the code snippet expose the Rule by decorating the class with the Export attribute.

the class declare its needs for logger by decorating the Logger property with the Import attribute.

Note: MEF does not dictate the use of attribute but this discussion is out of the scope of this post.


the Rule Engine class has needs to have the rules, so it decorate the Rules properties as follow:

Code Snippet
  1. public class RuleEngine
  2. {
  3.     [ImportMany]
  4.     public IRule[] Rules { get; set; }
  5. }


the final step is to compose all parts together:

Code Snippet
  1. static void Main()
  2. {
  3.     var ruleEng = new RuleEngine();
  4.     var container = new CompositionContainer();
  5.     container.ComposeParts(ruleEng);
  6. }



MEF is very flexible and there is many other ways of implementing that task.

in the following post we will learn how to use MEF in both VS 2008 and 2010, and on different platforms.


once again i want to thanks Glenn Block for giving me his MEF presentation which help me building this post.

more resources can be found at Glenn Block post

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



  1. Rolle2010/03/29 ב 09:23

    Interesting, thanks for this! Looking forward for the other parts.

  2. converse sale2013/11/28 ב 17:20

    I got thіs web page from my
    pal who &X74;o&X6c;d me about this web site and
    now thiѕ time I am brоwsin&X67;
    &X74;his si&X74;е and read&X69;ng very infoгmative
    pоs&X74;s here.