Page 1 of 1

How to include dynamically Extensions in your Application

#1 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 181
  • Joined: 25-October 10

Posted 06 January 2011 - 04:48 AM

How to include dynamically Extensions in your Application

In this short tutorial I will show up how easy it is with Microsoft .NET Managed Extensibility Framework (short MEF) to include dynamically extensions in your Application.

Setup

Attached Image

The MEFExample project will consume the loaded libraries. To ensure a lose coupling, there is a ExtensionInterface defined. It is a external Interface.
The Extensions1..n will implement the IExtensionBase.

A diagram will show that much better :):

Attached Image

Implementation
IExtensionBase
namespace ExtensionInterface {
    public interface IExtensionBase {
        string SayHello(string message);
    }
}



Extension1
using System.ComponentModel.Composition;
using ExtensionInterface;

namespace ExtensionOne {

    [Export(typeof(IExtensionBase))]
    public class Extension1 : IExtensionBase {
        public string SayHello(string message) {
            return string.Format("Hello, I'm Extension One. You send me: {0}", message);
        }
    }
}



Extension1..n implements the external interface "IExtensionBase. The clue with MEF you define the Composition.Export-Atrribute with the type of IExtensionBase. This is used later to find the correct extensions for your application.

Additional Example: Extension2
using System.ComponentModel.Composition;
using ExtensionInterface;

namespace ExtensionTwo {

    [Export(typeof(IExtensionBase))]
    public class Extension2 : IExtensionBase {
        public string SayHello(string message) {
            return string.Format("Hello, I'm Extension Two. You send me: {0}", message);
        }
    }
}



As you can see, the extensions just have a reference to the external interface and nothing more (lose coupling :)).

In my example the FormMain will load all the extension, and this is the way it's done:

private void LoadExtensions() {
    var catalog = new AggregateCatalog();

    // Catalog to the CurrentDirectory and filter on *.dll
    catalog.Catalogs.Add(new DirectoryCatalog(Environment.CurrentDirectory, "*.dll"));
    var container = new CompositionContainer(catalog);
    var batch = new CompositionBatch();
    batch.AddPart(this);

    // Compose the Parts
    // This will inject the Extensions to the ImportMany-Property
    container.Compose(batch);
}

#region Properties
[ImportMany(typeof(IExtensionBase))]
public IEnumerable<IExtensionBase> Extensions { get; set; }
#endregion




The AggregateCatalog searches the defined directory, in this case the current directory, for any files which have an extension of .dll.

After collect all libraries a container will compose all them to the right property defined with the ImportMany Attribute. The property is defined with the wanted type, so here we define IExtensionBase.

And that's it.

Demostration
I build a simple UI to show that it works :).

Attached Image

In runtime you will see all loaded extensions in the listBoxExtensions.
With the textBoxMessage and the buttonSend you can test the extensions.

Attached Image

So, that's all.

You could also add a FileSystemWatcher which check the directory for new libraries and load them dynamically. Just play a bit around with it.

Have Fun!

SourceCode and executable: Attached File  MEFExample.zip (86.99K)
Number of downloads: 182

Is This A Good Question/Topic? 2
  • +

Replies To: How to include dynamically Extensions in your Application

#2 killie01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 05-November 10

Posted 05 February 2011 - 05:11 AM

nice, i was looking for this
i even posted a request :)
but do you know anything about System.Addin
Was This Post Helpful? 0
  • +
  • -

#3 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 181
  • Joined: 25-October 10

Posted 05 February 2011 - 09:49 AM

It's hard to different these 2 frameworks. MAF (System.Addin) and MEF (System.ComponentModel.Composition) both are frameworks to extend the host app.
MAF is very powerfull, you can update the addin on runtime or even if a addin crashes the host app will work fine. But cause of all these benefits it's very slow and it's not very flexible. There aren't so much types you can use.

MEF is very simple to use, works like a charm and it's very fast.
Was This Post Helpful? 0
  • +
  • -

#4 killie01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 05-November 10

Posted 25 February 2011 - 07:01 AM

the iextensionbase is the Api right?
Was This Post Helpful? 0
  • +
  • -

#5 killie01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 05-November 10

Posted 25 February 2011 - 10:15 AM

how do i add tabs and menu items with extensions?
create a menuitem in the iextensionbase
and on the forum load load it into the tabcontrol/menu
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1