C#插件架构问题

Chr*_*ris 5 c# architecture

我正在研究类似于C#中Nagios的系统监控应用程序.我有一个插件接口定义为:

public interface IPlugin
{
    PluginResult Execute();
}
Run Code Online (Sandbox Code Playgroud)

每个插件,根据其功能,将具有可变数量的参数.例如,ping插件可能会占用主机名,数据包数,超时值等.我希望用户能够在我的用户界面中为每个服务定义这些参数,但很明显这些参数在应用程序发现哪些插件可用.我很好奇其他人如何设计一个插件,以便应用程序可以发现这些变量参数.

现在,作为一个例子,我有一个ping插件:

public class PingPlugin : IPlugin
{
    private const string RESULT_MESSAGE = "Average ms: {0}; Packet loss: {1}";

    private string _hostname;
    private int _packets;
    private int _timeout;
    private int _warningTimeThreshold;
    private int _warningLossThreshold;
    private int _errorTimeThreshold;
    private int _errorLossThreshold;

    public PingPlugin(
        string hostname,
        int packets,
        int timeout,
        int warningTimeThreshold,
        int warningLossThreshold,
        int errorTimeThreshold,
        int errorLossThreshold)
    {
        _hostname = hostname;
        _packets = packets;
        _timeout = timeout;
        _warningTimeThreshold = warningTimeThreshold;
        _warningLossThreshold = warningLossThreshold;
        _errorTimeThreshold = errorTimeThreshold;
        _errorLossThreshold = errorLossThreshold;
    }

    public PluginResult Execute()
    {
        // execute the plugin
    }
}
Run Code Online (Sandbox Code Playgroud)

我以为我可能能够使用反射发现构造函数参数,并向用户提供属性网格以允许配置插件,但我不确定使用此设计提供一组默认值的最佳方法.可能有哪些替代方案?

Eri*_*sch 18

您是否考虑过查看Managed Extensibility Framework

  • 为此投票,MEF太棒了.没有必要在这里彻底重新发明轮子. (3认同)
  • MEF允许发现和注入,但它没有回答基本问题,即如何将参数动态公开和连接到插件对象.MEF本身不是一个插件系统.在决定应该公开哪些服务以及用户如何与插件交互时,仍然需要进行架构工作. (3认同)
  • 插件系统不必过于复杂.如果我不需要它提供的所有东西,为什么要使用MEF?如果我可以将我的插件代码保存到几个小的(ish)类文件中,那么其他开发人员可以更轻松地获取应用程序的维护,而无需了解MEF或了解我是如何使用它的.我理解这个建议,并欣赏它,但我真的无法理解随之而来的敌意...... (3认同)