views:

108

answers:

2

I have three components. A windows forms control (custom made), a main class and a plugin class.

The plugin generates an update event, where something should be added to the control. This could be a string, a string with an url, or another control, but maybe in the future, something else.

What is the best way to design something like this?

Edit:

I should have been a bit more clear. The problem I want to solve is how to respond differently on a specific item.

for example: A plugin may choose just to add some text. It dispatches an event, and the main class should call the corresponding methods. But another plugin may choose to add a custom control, so the main class needs to call different methods.

How can I desgin the part where the main class acts differently on a specific event.

(btw, don't take the word plugin too literaly. Right now, they're just classes in the project).

A: 

Sounds pretty straightforward. I would suggest using an interface for your plugin class.

interface IPlugin
{
    event EventHandler PluginUpdateEvent;
}

Either your form class or your main class would contain a collection or an array of plugins, whatever the requirements were, and discover them however you need to - via reflection, constructor injection, config file, whatever is best.

Upon loading the plugin, you would ensure that the form subscribed to the PluginUpdateEvent for that plugin, and add an event handler to tackle the event and add whatever is necessary to the form.

womp