Introduction To Managed Extensibility Framework

Version: 1.1.0

Description

The Managed Extensibility Framework (MEF) allows developers to provide hooks into their .NET applications for extensions by first and third parties. MEF can be thought of as a general application extension facility. In this Hands-On Lab, you will learn how to define extensibility points for components, perform conditional binding and component creation and import extended assemblies while an application is running.

Overview

The Managed Extensibility Framework (MEF) allows developers to provide hooks into their .NET applications for extensions by first and third parties. MEF can be thought of as a general application extension facility.

MEF enables developers to create extensions dynamically, without the extending application or the extension requiring any specific knowledge of the other. This ensures no coupling exists between the two at compile time, enabling applications to be extended at runtime, without the need to recompile. MEF also provides the ability to examine the metadata of an extension assembly before loading the extension into the application -- a much faster approach.

Several key concepts relating to extensibility are covered in this lab:

  • Composition is the practice of combining several objects with discrete functionality into one or more complex objects. Composition is not inheritance of functionality from a parent class, but is instead the assembling of several different objects into one. For example, Wing, Propeller, Fuselage, and VerticalStabilizer objects could be composed as part of an Aircraft object.
  • ComposableParts are the key building blocks of MEF. ComposableParts enable applications to expose and consume component extensions via Exports and Imports.
  • Contracts are the avenue for communication between Export and Import components. A Contract is normally implemented via an Interface class. Contracts enable MEF ComposableParts to avoid dependencies or tight coupling with other components.
  • Conditional Binding allows loading of components, which meet specific metadata criteria. Following the example above, you might choose to load VerticalStabilizer components, which were made only of composite graphite.

Extensibility is achieved, primarily, by adding Import attributes at appropriate points in the application and adding corresponding Export attributes to the extensions. Import and Export attributes can be thought of in the same aspect as a supplier and consumer respectively. Export components supply some value; Import components consume that value. Other options for extensibility are open to developers, including completely custom extensibility approaches; however, this lab focuses solely on the primary approach discussed above. Moreover, the components can be imported using the ImportMany attribute, which loads all the components that match the given contract as a collection.

With the release of Silverlight 4, MEF has been included in it, allowing you to extend rich internet applications. This does not imply that MEF can only be used with Silverlight 4; using the version available in codeplex (http://mef.codeplex.com), MEF can be used to compose applications created in Silverlight 3.

Objectives

In this Hands-On Lab, you will learn how to:

  • Create extensibility modules and components
  • Import Extended assemblies while an application is running
  • Extend a Silverlight 4 application (optional)

System Requirements

You must have the following items to complete this lab:

  • Microsoft Visual Studio 2010
  • .NET Framework 4
  • Silverlight 4 (optional)
  • Silverlight 4 SDK (optional)
  • Silverlight 4 Tools for Visual Studio 2010 (optional)

Setup

All the requisites for this lab are verified using the Configuration Wizard. To make sure that everything is correctly configured, follow these steps:

Note:
To perform the setup steps you need to run the scripts in a command window with administrator privileges.

  1. Run the Configuration Wizard for the Training Kit if you have not done it previously. To do this, browse to Source\Setup folder of this lab, and double-click the Dependencies.dep file. Install any pre-requisites that are missing (rescanning if necessary) and complete the wizard.

    Note:
    The Configuration Wizard is used for checking dependencies and setting up the environment. If the Configuration Wizard is not installed on your machine, you must install it running the DependencyChecker.msi file located on the %VS2010TKInstallationFolder%\Assets\DependencyChecker folder (e.g. by default the Training Kit is installed under C:\VS2010TrainingKit).

    For convenience, much of the code you will be managing along this lab is available as Visual Studio code snippets. The Dependencies.dep file launches the Visual Studio installer file that installs the code snippets.

Exercises

This Hands-On Lab comprises the following exercises:

  1. Exercise 1: Using MEF to Extend an Application
  2. Exercise 2: Using Metadata and Lazy-Loading Modules
  3. Exercise 3: Using MEF and Silverlight 4

Starting Materials

This Hands-On Lab includes the following starting materials.

  • Visual Studio solutions. Depending on the exercise you will find Visual Studio solutions that you can use as starting point for the exercises.
Note:
Each exercise is accompanied by an End folder containing the resulting solution you should obtain after completing the exercises. You can use this solution as a guide if you need additional help working through the exercises.

Note:
Each exercise contains a Visual Basic and a C# version; Inside the End/Begin solution folder, you will find two folders: VB, containing the Visual Basic version of the exercise, and C#, containing the C# version of it.

Estimated time to complete this lab: 30 minutes.

Next Step

Exercise 1: Using MEF to Extend an Application