views:

9

answers:

1

Hi folks,

I have a strategic question for my C#-project. I want to implement a plugin-concept and now struggling with the best way for the plugins to manipulate the data of the main project.

At first I created a "Master" PlugIn Project that defines the Interface for the plugins and an attribute to identify a class as a pluginclass when I load it in my main project.

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public sealed class GraphViewerPlugInAttribute : Attribute
{
}

public interface IGraphViewerPlugIn
{
  Panel GetRightSidePanel();
  string Name();
}

This project is then referenced in the main project and the pluginclasses. The pluginclass implements the interface ...

[GraphViewerPlugIn]
public class myTestPlugIn : IGraphViewerPlugIn
{
  public Panel GetRightSidePanel()
  {
    Panel myPanel = new Panel();
    myPanel.BackColor = System.Drawing.Color.Red;

    return myPanel;
  }

  public string Name()
  {
    return "TestPlugIn";
  }
}

... and the main project loads all plugins that are stored in a certain directory.

This works quite well so far, I can get the special Panel from the PlugIn, but now I want to manipulate data of the main project from within the PlugIn. What would be the best way to do that? I have some kind of data-container-class that is defined in the "Master Plugin Project" in mind. The plugin puts the data into that container and the main project will be recognized by an event that the data has changed and can now look what data has changed and then apply the changes. Is that the right direction, what do I have to consider and which techniques (i.e. events, static classes ...) shall I use to implement that?

Thanks in advance, Frank

A: 

One approach is to add a configuration button which activates a method on the interface designed for configuration.

This method can make use of any code it would like to configure the plug in, including making it's own calls to windows forms.

If you are using windows forms or winfx, and not a custom UI, this is the ideal method because it removes all logic related to the plugin from the application itself, except for notifcation that a configure window was requested.

public interface IGraphViewerPlugIn 
{ 
  Panel GetRightSidePanel();
  string Name(); 
  void ShowConfigurationDialog();
} 

Of course, you then implement ShowConfigurationDialog as such:

public void ShowConfigurationDialog()
{
    Form form = new MyConfigurationDialog();
    form.ShowDialog();
}

where MyConfigurationDialog is your designer created form for configuring the plugin.

Eugarps