No, as a few designers have asked we are not talking METH, a drug that has reached pandemic proportions in this country.
We are talking about MEF.. Microsoft’s Managed Extensibility Framework
MEF has shipped with .NET 4 and is also available for Silverlight 3 and 4 which seems to be a unfortunately named but incredible and new feature that you will want to know about and use in your applications.
check out http://MEF.codeplex.com if you need a Silverlight 3 version.
MEF basics or what designers will want to know..
- You can add and replace functionality to the system without having modify existing code. This means you don’t have to worry about introducing bugs into the existing code.
- MEF allows you to design applications and libraries that can be extended by third-parties after they are released.
- In Silverlight, MEF is ready whenever it’s needed.
MEF’s Three Basic Concepts
- Export- Let MEF know you are providing a service.
- Import- Let MEF know that you need something.
- Compose- Let MEF know it has permission to go do something.
HELLO WORLD as a MEF Extension
Export
First we will create a User Control.
To Tell MEF your extension is available you will need to add an “EXPORT” attribute on it and specify a “Contract”..
[Export(typeof(UserControl))]
public class MyExtension1: UserControl
{
public string Message {
get{return(string) Button.Content;}
set {Button.Content=value;}
}
}
Import
Notice above that our extension has a Message property. We can let MEF provide this so it’s customizable rather than hardcoded. To do this we will add an “IMPORT” attribute. If we don’t specify a contract for the import, it will just derive the contract as being a string. We don’t want just any string, we want the Message, we can specify a specific contract such as “HelloMEF.Message”.
[Export(typeof(UserControl))]
public class MyExtension1: UserControl
{
[Import(“HelloMEF.Message”)]
public string Message {
get{return(string) Button.Content;}
set {Button.Content=value;}
}
}
Next let’s create our message which our extension will import. To do this we’ll create an export, but it will use a feature of MEF called a Field Export. This is because we can’t derive from System.String, so we want to create an instance of a string and export it as our message. If your needs are beyond setting a value which we can do in a field, MEF also supports property exports.
public class MessagePart
{
[Expor(“HelloMEF.Message”)]
public string Message = “Hello MEF”;
}
Getting the hosting App to discover your extension using Import Many
- Create a public collection of type UserControl
- Add the ImportMany attribute so that MEF can tell our app’s MainPage that it needs all exports of UserControl to be injected inside of it.
public class MainPage:UserControl
{
[ImportMany]
public IEnumberable <UserControl> MyExtension1 {
get;set;}
}
Compose – Satisfy imports
Next we need to tell MEF to go do the work and give it back to us. Just drop into the constructor of the MainPage and call the SatisfyImports method. This will load up the collection with all available extensions.
In the MainPage below the standard InitializeComponent(); code add the line:
PartInitializer.SatisfyImports(this);
Next we simply loop through the collection and show the extensions. In this example we will add them to an items control.
public MainPage()
{
InitializeComponent();
PartIntializer.SatisfyImports(this);
foreach (var MyExtension1 in MyExtension1)
TopItemsControlWidget.Items.Add(MyExtension1);
}
}
Follow-Ups
Here’s a great video from Glen Block from Microsoft’s PDC09 event talking about MEF and Rich Internet Applications in Silverlight..
More information can also be found on MSDN at:
http://msdn.microsoft.com/en-us/magazine/ee291628.aspx