How to Use MEF in Silverlight

In this article I will explain about the Managed Extensibility Framework (MEF) and demonstrate the its use in Silverlight. I will also provide the source code of a sample Silverlight application implementing the MEF concept.

Introduction

In this article I will explain about the Managed Extensibility Framework (MEF) and demonstrate the its use in Silverlight. I will also provide the source code of a sample Silverlight application implementing the MEF concept.

For creating the sample application I used Visual Studio 2010 IDE and Silverlight 4.0.

What is MEF?

MEF was introduced as a part of .NET framework 4.0. As the name indicates, MEF is helpful in improving the extensibility of an application. The concept is like modules, which can be developed independently with zero dependency. During run time these modules can be composed by the MEF framework and loaded into the application memory for further usage. The independent module will be exported and while composing uses the Managed Extensibility Framework. These exported modules will be imported for usage in the application. The modules which are to be imported or exported are termed as Parts.

Below are the advantages of using MEF:

Parts are loosely coupled

Extensibility is easy and straightforward

Dependency between modules is eliminated

Parts are composed at run time

MEF in Silverlight 4.0

The Silverlight MEF related classes are packaged under the namespaces System.ComponentModel.Composition and System.ComponentModel.Composition.Initialization. Below are some of the main attributes and methods:

Export – Attribute used to export a MEF part

Import – Attribute used to import a MEF part

ImportMany – Attribute used to import many MEF parts

SatisfyImports – Populates the respective Imports part

Demo Silverlight MEF application

As I have provided an insight about the MEF concept in the above sections, we will create a demo Silverlight application implementing MEF now.

Create a Silverlight application named SilverlightMEFSample. Add a reference to the two MEF libraries below:

System.ComponentModel.Composition.dll

System.ComponentModel.Composition.Initialization.dll

In the MainPage.XAML add a stack panel. The idea is to create multiple user controls as exportable parts then using MEF we will import and compose them onto the MainPage.XAML at runtime.

Now add a StackPanel named to the MainPage.XAML page as shown below. The composed parts will be added to this stack panel.

In the above code the CompositionInitializer.SatisfyImports call performs the importing of the exported parts. That’s all--we are good to go. You can now run the application. Fig 1.0 below shows the output screen. Notice that all the three parts have been composed and added to the stackpanel.

Fig 1.0

Conclusion

I hope this article has provided a valuable insight about the Managed Extensibility Framework and implementing MEF in a Silverlight application. Please make use of the comments section to provide feedback or to ask for any clarification.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.