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:
  1. Added a reference to System.ComponentModel.Composition to my project
  2. Created a new Class Library ("Modules") with a single interface: "IModule"
  3. Referenced my Class Library project from my main project
  4. 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);
  5. Added an Import statement to my ApplicationWindow:
      [Import]
      private IEnmerable modules { get; set; }
  6. Added an interface implementation to my ApplicationWindow:
      public partial class ApplicationWindow : Form, INotifyImportSatisfaction
  7. 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;
        }
      }
  8. Exported my sole module:
      [Export(typeof(IModule))]
      public partial class wizLäggTillFordonsfabrikat : Form, IModul
Done! Check out MEF at CodePlex: http://mef.codeplex.com/

Comments

Popular posts from this blog

Auto Mapper and Record Types - will they blend?

Unit testing your Azure functions - part 2: Queues and Blobs

Testing WCF services with user credentials and binary endpoints