SIGN UP MEMBER LOGIN:    
ARTICLE

MEF Silverlight Implementation

Posted by Aravind BS Articles | Silverlight with C# November 30, 2011
Leta learn about the Managed Extensibility Framework (MEF) library of Silverlight4.
Reader Level:

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

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor