Reputation: 793
I have an application that is built up with Prism and MEF. I have some modules (defined inside same solution) that I load in and everything works fine.
However now I would like to allow the user to add their own plugins (basically these plugins should alter a collection of data points) - so in other words the user creates an algorithm(plugin) that changes these datapoints that are living inside a ViewModel.
I need to define some kind of "Contract" that the user needs to ophold when creating a new plugin. How would I do this?
The plugin should be a single .dll inside a /Plugin folder and should be loaded at runtime, a ListView should be populated in a view that contains a UserControl for each Plugin.
Upvotes: 0
Views: 72
Reputation: 266
MEF will make this work really easy and smooth.
For the plugins:
Create a interface for your plugins. It doesn't really need to be very complex, although you may use it to force the developers to add some description or version information, and of course a method that receives your collection and transforms it.
The pluguins should use the ExportAttribute to let MEF know about them.
[Export(typeof(IPlugin))]
public class Plugin : IPlugin
That should be enougth here.
For the application:
Declare a collection that will receive the list of plugins that MEF could find, dont forget the ImportManyAttribute.
[ImportMany(typeof(IPlugin))]
public IEnumerable<IPlugin> Plugins{ get; set; }
Create your catalog and container. On the code I show I add two catalogs, maybe you dont need it, just to you know you can have more tha one source.
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetEntryAssembly()));
catalog.Catalogs.Add(new DirectoryCatalog("plugins"));
var container = new CompositionContainer(catalog);
Do the magic.
Of course this
should be the object with that ImportManyAttribute
.
container.ComposeParts(this);
Upvotes: 2