Managed Extensibility Framework
I'd say it took me about an hour to implement MEF, and then I spent most of it making my loadable modules look good in my form (I'm not an Art Designer...).
Verdict: Love.
A colleague and I are writing a Business Administration Application that will be shipped with one piece of functionality, but that we expect will grow over time. Thusly, I have converted it from a Main-Form-with-a-Child-Form piece of work (very tightly coupled) to a ... Main-form-that-can-load-"any"-module-into-its-UI solution.
Changes:
-
Added a reference to System.ComponentModel.Composition to my project
Created a new Class Library ("Modules") with a single interface: "IModule"
Referenced my Class Library project from my main project
Revamped the Main(string[]) code:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var mainForm = new ApplicationWindow();
var container = new CompositionContainer(new DirectoryCatalog("."));
var batch = new CompositionBatch();
batch.AddPart(mainForm);
container.Compose(batch);
Application.Run(mainForm);
Added an Import statement to my ApplicationWindow:
[Import]
private IEnmerable
public partial class ApplicationWindow : Form, INotifyImportSatisfaction
Fullfilled the interface:
public void ImportCompleted()
{
foreach (var module in modules)
{
var item = menu.Items.Add(module.Namn);
item.ToolTipText = module.Description;
item.Tag = module;
item.Click += OpenModule;
}
}
Exported my sole module:
[Export(typeof(IModule))]
public partial class wizLäggTillFordonsfabrikat : Form, IModul
Comments