MEF Silverlight Implementation


What is MEF?

Managed Extensibility Framework (MEF) is a library in Silverlight 4 (Now, it's part of Silverlight 4) for building rich internet applications which improves the flexibility, maintainability and testability of large application.

MEF helps us to build modular based application. We can also load modules dynamically.

Why is MEF?

Really, I was fed up with these frameworks, when MEF has been released. Because, it was not the matter of learning, but the matter of confusion. Which framework should I use for modular programming? Which one should I use for dynamically loading module/assembly? Should I use PRISM or MEF?

The Composite Application Library (PRISM) is basically a huge toolbox. Using PRISM, a lot can be done. It supports for modularity, dynamic loading, region management etc. But, According to me, PRISM is bit complicated. So, if I want to do a modular program, then it is simply waste of time to go PRISM.
MEF can be used for modular, dynamically downloading programs without learning tons of design concepts. Even PRISM v4 supports for MEF.

Basic principles of MEF

There are three major parts.

  1. Export : Export as its name indicate itself, it will export the module. It may have anything like view model, any kind of data source or user control. This is what we want to add it to our application on runtime. It tells to MEF that, this part is ready to compose.
     
  2. Import : So, I have exported something. So, I need to import it. I meant, I have to accept the parts we want to plug in. So, this could be done using Import attribute.
     
  3. Compose.

So, now our parts are ready to be loaded. Now, the actual work is to build the composition. This can be done using the CompositionInitializer Class.

At runtime, the compositioninitializer builds a package and maps with the particular import and export tag names given.

Consider a Silverlight example. Here, I would like to display my name using MEF.

Step 1 : Create a Silverlight 4 application.

Step 2 : Create a class. Write a property and use export key over that. So, here I want to display my name so, I am using EXPORT over firstname property. (Here, property name may be anything. But, export key name should be same for both Export and Import.

public class AravindTest
    {      
        public string Midlename
        {
           
get
            {
                return "Hi";
            }
           
set
            {
            }
        }
      
        [Export("MyName")]
 
        public string Firstname
        {
           
get
            {
                return "Aravind";
            }
           
set
            {
            }
        }
    }


Step 3 : In the main page, I am using AravindTest module to import. I have created one property called Name with keyword Import("MyName").

Also, I have used CompositionInitializer to build a package.

    public partial class MainPage : UserControl
    {
        [Import("MyName")]
        public string Name{get;set;}
 
        public MainPage()
        {
            InitializeComponent();
            CompositionInitializer.SatisfyImports(this); 
            MessageBox.Show(Name);
        }
    }


Step  4 :

So, when I run the application, the result will be:

MEF.gif


Similar Articles