views:

235

answers:

1
public interface IPlugin
{
    public bool execute();
}

All my "parts" implement this IPlugin interface. My parts obviously have Import/Export requirements/offerings.

I'm writing a build+config system, in which the user dynamically selects what he/she wants, which translates to a set of plugins being called.

For example, here's a list of plugins:

(1) Install X ... exports "XTypeInstalled"

(2) Configure X ... imports "XTypeInstalled", exports "XTypeConfigured"

(3) Install Y ... imports "XTypeConfigured"

(4) Install Z

(5) Configure A

Now, a user could select (1), (3), and (4) ... or could select (1), (2), (3)

The problem I'm facing is, do all my plugin writers now need to implement IPartImportsSatisfiedNotification? If not, and the user selects a workflow of (1), (2), and (3) ... how do I get to calling (3)'s execute() method.

Am I making sense?!

+1  A: 

I'm not sure about using MEF to manage the runtime flow of a program like this. MEF is a great choice for locating and loading plugins, but it is designed for very loosely-coupled systems, so you may not have the level of control you need.

For example, though the dependency resolution process will provide tasks with their dependencies, there's no opportunity to inspect what the dependency structure is.

Things may be tricky when, for instance, you want to guarantee that a task executes only once, or when you want to substitute one task with another.

My recommendation is that you use MEF to find the plug-ins, but develop a rich object model independent of MEF for executing them.

E.g.:

[Export(typeof(IPlugin)), ExportMetadata("Name", "ConfigureX")]
public class ConfigureXPlugin : IPlugin { ...

Then use MEF to find all the available plugins:

public class BuildRunner {
  [ImportMany]
  Lazy<IPlugin, IPluginMetadata> plugins[];

  void RunBuild(...) {
    // Figure out which tasks should execute, in which order, and call each of them

Hope this helps. If you're still having trouble, posting some more information about your problem would help.

Nick

Nicholas Blumhardt