Malisa Ncube - .NET Delights

.NET Development ideas and things
posts - 40 , comments - 82 , trackbacks - 0

My Links

News



I LOVE DataObject.NET
http://xceed.com
http://www.sharpcrafters.com/
http://www.telerik.com

Get this blog as a slideshow!
Powered by feedmap.net

Twitter












Tag Cloud

Archives

Post Categories

Managed Extensibility Framework 101 - (a)

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

 

Print | posted on Tuesday, May 26, 2009 5:13 PM |

Feedback

Gravatar

# re: Managed Extensibility Framework 101 - (a)

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?
8/14/2011 11:52 PM | Andrey
Gravatar

# re: Managed Extensibility Framework 101 - (a)

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.
8/15/2011 12:00 AM | Malisa Ncube
Gravatar

# re: Managed Extensibility Framework 101 - (a)

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...
8/16/2011 12:41 AM | Andrey
Gravatar

# re: Managed Extensibility Framework 101 - (a)

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
9/20/2011 8:22 AM | saroop trivedi
Gravatar

# re: Managed Extensibility Framework 101 - (a)

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...
10/23/2013 2:16 AM | Claus
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: