views:

52

answers:

1

I am writing an application that supports plugins. Plugin creator is responsible for providing some instances to the plugins it creates. There are two alternatives for providing these instances.

Option 1: Plugin writer writes her plugins like this:

public interface IGiveMe1 { INeed1 Need1 { set; } }

public interface IGiveMe2 { INeed2 Need2 { set; } }

public class MyPlugin : IGiveMe1, IGiveMe2
{
    // This has to be parameterless
    MyPlugin() { ... }

    #region IGiveMe1 Members

    public INeed1 Need1 { set { ... } }

    #endregion

    #region IGiveMe2 Members

    public INeed2 Need2 { set { ... } }

    #endregion
}

After the plugin creator creates the instance by using the mandatory parameterless constructor, it looks for interfaces that the plugin class implements. If plugin implements IGiveMe1, plugin creator calls setter of INeed1. Same for IGiveMe2.INeed2.

Option 2: Plugin writer writes her plugins like this:

public class MyPlugin
{
    MyPlugin(INeed1 need1, INeed2 need2)
    {
        ...
    }
}

In this method, plugin creator searches for constructors that take parameters of type INeed1 and INeed2. It finds the best match (the one with the most matching parameters) and calls it by passing the instances.

If you were to write plugins to this application, which approach would you prefer?

A: 

I definitely prefer the second option, because it is much simpler and more clear. The unit tests for the plugins will be more clear as well.

Alternatively, if you don't want to pass all the INeedX to all the plugins, match the type of the constructor parameters (using reflection obviously) and pass only the ones that are accepted in the constructor.

Grzenio