Geeks With Blogs
Malisa Ncube - .NET Delights .NET Development ideas and things

This is the first posting on MEF, in which I will be explaining some things that I discovered while playing around with MEF. I should say that all the postings on my blog, including this subject do not represent my employer or any other organization, they are merely my mumblings based on my work that I do and whatever in find interesting and would like to share. Use it at your own risk, but it works on my machine.

clip_image002

To start with I would like you to read about the architecture of the MEF framework from the MEF Codeplex site.

In my MEF101, I present an example which shows how you can import composable parts from your assemble and let MEF compose and satisfy imports and enable you to come up with a simple interface made from different parts.

I created the IPlugin interface which I use in my exports.

/// <summary>

/// IPlugin Interface

/// Used by all the composable parts which will be used as plugins

/// </summary>

public interface IPlugin

{

}


 

All rectangle items visible are simple UserControls which implement the IPlugin contract. Notice that I have placed an [Export] attribute. This informs MEF that during composition that if there is something which needs [Imports] IPlugin, then the MEF will create it and satisfy that import.

/// <summary>

/// Plugin1

/// Example Plugin

/// </summary>

[Export(typeof(IPlugin))]

public partial class Plugin1 : UserControl, IPlugin

{

public Plugin1()
{
InitializeComponent();
}
}

 

All the plugins in this example have this same structure. I just changed the background color in the designer to so that we can tell that they are different.

The Main form has the following code.

 

/// <summary>
/// Form1
/// The main form. We need to have the [Export] attribute on it because on the Program.cs
/// we compose it using MEF and get its instance as an ExportedObject.
/// </summary>
[Export]
public partial class Form1 : Form
{
/// <summary>
/// Plugins
/// These imports will be satisfied by instances of the IPlugin 's
/// which in this case is the user controls with different colors.
/// </summary>
[Import]
public IEnumerable<IPlugin> Plugins;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// We loop through all the instances of the plugins an add them to the tableLayoutPanel
// and because we know that all of them are UserControls we can cast the as Control.
foreach (var plugin in Plugins)
{
(plugin as Control).Dock = DockStyle.Fill;
tableLayoutPanel1.Controls.Add(plugin as Control);
}
}
}

The Program.cs has the following code.

 

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

var form1 = Compose();
Application.Run(form1);
}

public static Form1 Compose()
{
//Create a catalog - in this case we want to import the composable parts which exist in this
//assembly, so we will use the AssemblyCatalog against the current assembly.
//
//It is also possible to import assemblies from external assemblies using the DirectoryCatalog.
//Furthermore you can combine a number of catalogs into an AggregateCatalog.
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

//Create the Container - then pass the catalog into the container
var container = new CompositionContainer(catalog);

//Return the instance of Form1.
return container.GetExportedObject<Form1>();
}
}

And that’s all. Look at how we create the instance of Form1 class and use the Application class to run it.

You can download the source here

 

Posted on Tuesday, May 26, 2009 5:13 PM | Back to top


Comments on this post: Managed Extensibility Framework 101 - (a)

# re: Managed Extensibility Framework 101 - (a)
Requesting Gravatar...
Thanks, Malisancube for your article. I just started searching about MEF/plugins and so far this article is the most informative of the simple ones. However, one thing i don't understand is how to actually extend your application without recompiling it. Am i missing something?
Left by Andrey on Aug 14, 2011 11:52 PM

# re: Managed Extensibility Framework 101 - (a)
Requesting Gravatar...
As long as you have an external assembly which with classes which implement IPlugin you'll be set. You need to change use an AggregateCatalog to combine the catalog which loads external assemblies and the executing assembly.

If its not clear, holla and I can send a sample.
Left by Malisa Ncube on Aug 15, 2011 12:00 AM

# re: Managed Extensibility Framework 101 - (a)
Requesting Gravatar...
Thanks, Malisa. The problem was that i had not found the part b. I turns out that your examples suit me most, just have to swap to WPF...
Left by Andrey on Aug 16, 2011 12:41 AM

# re: Managed Extensibility Framework 101 - (a)
Requesting Gravatar...
hey there is problem when we run the code on framework 4.0

return container.GetExportedObject<Form1>();

not existed in framework 4.0 please give some demo for framework 4.0
Left by saroop trivedi on Sep 20, 2011 8:22 AM

# re: Managed Extensibility Framework 101 - (a)
Requesting Gravatar...
Thanks Malisa, great article. One of the best about MEF (when you start these things), because in is so simple written.
I searched something to implement MEF on Forms and this article gave me the right direction...
Left by Claus on Oct 23, 2013 2:16 AM

Your comment:
 (will show your gravatar)


Copyright © Malisa L. Ncube | Powered by: GeeksWithBlogs.net | Join free