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

Introduction of Managed Extensibility Framework (MEF)

By , 3 Apr 2010
 

Background

Software requirements evolve because of ever evolving businesses they are developed for. This evolution is making the life of software architects and designer ever more difficult. Modern software should be extensible. This is kind of Addins support in our applications. But available software tools lacked good provision of creating extensible applications. As good software architects and developers, we are always in the quest of getting this out of the box so that we can focus more on the business requirements.

Introduction

With Visual Studio 2010, Microsoft has introduced a great framework which must be looked at. This is called MEF (Managed Extensions Framework). This is based on the same software principles that were the basis of Inversion of Control pattern. We design software components in a way that they follow “Low Coupling” GRASP (General Responsibility Assignment Software Pattern) pattern.

Jump to Writing Code

Since I don’t want to waste anymore of your time, I am starting to create an example “Hello World” program using MEF. Let us create a Console C# Application. We name our application as MEFIntro.

CreateProject.JPG

Now we add a reference of System.ComponentModel.Composition.dll.

AddReference1.JPG

It is shown in the Solution explorer as follows:

AddRefernce2.JPG

Now we add the namespaces we need to create our first application in Program.cs.

using.JPG

We implement two classes in our namespace MEFIntro. They are as follows:

  1. Program (Listing 1)
  2. SimpeHello (Listing 2)

Dependencies in MEF are provided using attributes. There are two basic attributes. They are Import and Export. If you look at Program class in Listing 1, you can figure out that, its string Property message should be importing some string. It doesn’t specify what would be the source of this import. Now in Listing 2, SimpleHello exports some string property. Some other component might need this property. In our example, this is Program class. So it is apparent that both these classes depend on each other.

Now if we just follow cowboy coding, we would directly instantiate MyHello in Program class and use MyHello.Message property to populate its own Message property. But, with MEF, we don’t need to do that. This is because MEF takes care of injecting the dependencies.

To determine the dependencies between software components, catalogs are defined (Run method in Program class). Here we have used AssemblyCatalog. There may be more than one catalog of same or different types. There might be other catalogs too, like DirectoryCatalog, etc. After adding these catalogs in CompositionContainer, when we call ComposeParts method of the container, all dependencies are resolved. As you can see that when we call ComposeParts, string property Message in MyHello class (specified with Export attribute) is copied to the string property Message in Program class (specified with Import attribute).

Listing 1

class Program
{
    [Import]
    public string Message { get; set; }

    static void Main(string[] args)
    {
        Program p = new Program();
        p.Run();
    }

    public void Run()
    {
        var catalog = new hosting.AssemblyCatalog(Assembly.GetExecutingAssembly());
        var container = new hosting.CompositionContainer(catalog);
        container.ComposeParts(this);
        Console.WriteLine(Message);
        Console.ReadKey();
     }
} 

Listing 2

public class MyHello
{
   [Export]
   public string Message
   {
       get { return "Hello World"; }

   }
}

History

  • 03/03/2010: Article posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Muhammad Shujaat Siddiqi
Software Developer (Senior)
United States United States
Member
Muhammad Shujaat Siddiqi
New Jersey, USA

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy Vote of 5memberMrinal Nath TCS28 Feb '12 - 23:24 
Thanks.. u saved my day
GeneralMy vote of 5memberjugalpanchal4 Jul '11 - 2:47 
It helps a lot for beginner.
GeneralThis article helped a lotmemberMember 224525415 Apr '10 - 17:16 
I was using .net 4 and the examples I was following where .net 3.5, and the names of the methods all changed, so this was real helpful. Thanks Laugh | :laugh:
GeneralMy vote of 1memberdisore7 Apr '10 - 20:12 
The article doesn't contain enough information for me to give it anything else then a 1. Watching a screencast with Glenn Block on Channel 9 is far more educating.
GeneralMy vote of 1memberArchAngel1236 Apr '10 - 5:56 
Very superficial - not worth the time to read it
GeneralYou haven't actually explained much about MEF here.mvpPete O'Hanlon3 Apr '10 - 9:21 
What you have here is a start; nothing more. You haven't covered the use of Catalogs or items such as why to use MEF over IoC/DI. There is so much more to MEF than this article would suggest - yes you say it's an introduction, but you have missed out a lot of the basics. Please add them in.

"WPF has many lovers. It's a veritable porn star!" - Josh Smith

As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

My blog | My articles | MoXAML PowerToys | Onyx



GeneralRe: You haven't actually explained much about MEF here.memberDavide Vitelaru3 Apr '10 - 21:28 
He might have forgot to check it as a "Work in progress" article.
Generalnteresting and InformativememberMakooali3 Apr '10 - 8:06 
I really like it.
 
Ali
GeneralRe: nteresting and InformativememberRichard Osafo7 Apr '10 - 11:56 
Same here. I found the example to be very simple. Just about enough to introduce the framework.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 3 Apr 2010
Article Copyright 2010 by Muhammad Shujaat Siddiqi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid