基于插件的应用程序,其插件可以修改其他插件

发布于 2024-10-17 22:32:48 字数 446 浏览 6 评论 0原文

我正在研究如何用 C# 构建模块化、基于插件的应用程序。 我正在阅读有关 Prism 和 MEF (我已经在我的一些项目中使用过)。

我发现的所有示例和文章都讨论了离散模块。我的问题就是关于这个。假设其中一个模块不提供新视图,但它“简单”需要通过添加一个或多个字段和一些进一步的逻辑来更改由不同模块提供的现有视图。你会怎么做?

在组合时检查目录中存在哪些其他部分并以编程方式修改它们是一个好方法吗?

如果有一个“模块 1”和一个“模块 2”改变了“模块 1”中的某些内容,我可以认为这是一个可能的解决方案。但如果情况变得更加复杂呢?例如,如果我们有一个基本的“模块 1”,必须由“模块 2”和“模块 3”修改,但也存在一个“模块 4”,它修改“模块 2”提供的 UI 和逻辑,并且很快... ?

您能否建议我如何实现这一点?

预先感谢,

干杯,

Gianluca

I am taking a look at how to build modular, plugins-based applications in C#.
I am reading about Prism and MEF (that I've already used in some projects of mine).

All examples and articles that I found talk about discrete modules. My question is just about this. Let's say that one of the modules doesnt provide new views, but it "simply" needs to alter an existing view, which is provided by a different module, by adding one or more fields and some further logic. How would you do that?

Would it be a good approach to check at the composition-time which other parts are present in the catalog, and programmatically modify them?

I can think of this as a possible solution if there is a "module 1" and a "module 2" that changes something in "module 1". But if the scenario gets far more complex? for example if we have a basic "module 1" that has to be modified by "module 2" and "module 3", but also it exists a "module 4" that modifies the UI and logic provided by "module 2", and so on... ?

Could you advice me on how this can be realized?

Thanks in advance,

Cheers,

Gianluca

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎盛开的樱花 2024-10-24 22:32:48

从您的示例中,我推断您的导出看起来有点像这样,您可以在某处执行 [ImportMany]

[Export(typeof(IView))]
public class BarView : IView
{
    ...
}

假设一个特定的视图实现本身需要可插入。然后你可以这样做:

[Export(typeof(IView))]
public class FooView : IView
{
    [ImportMany(IFooViewPlugin)]
    public IEnumerable<IFooViewPlugin> Plugins { get; set; }

    ...
}

当然,仍然由你来塑造 IFooViewPlugin 并决定 FooView 如何调用它来自定义自身。这取决于您想要什么样的定制。

From your example I infer that you have exports that look a little bit like this, for which you do an [ImportMany] somewhere:

[Export(typeof(IView))]
public class BarView : IView
{
    ...
}

Suppose one particular view implementation needs to be pluggable itself. Then you could do something like this:

[Export(typeof(IView))]
public class FooView : IView
{
    [ImportMany(IFooViewPlugin)]
    public IEnumerable<IFooViewPlugin> Plugins { get; set; }

    ...
}

Of course, it is still up to you to shape IFooViewPlugin and decide how FooView invokes it to customize itself. That depends on what kind of customizations you had in mind.

贪了杯 2024-10-24 22:32:48

您可以让主机应用程序通过“插件”属性公开插件集合。然后您的插件可以迭代插件集合,直到找到正确的类型之一,然后对其进行修改。

为了安全起见,您可能需要在正在修改的插件中添加一些测试代码(例如设置密码)以防止不需要的插件进行修改。

You can have the host application expose the collection of plugins via say a "Plugins" property. Then your plugin can iterate through the plugins collection till you find one of the right type and then modify it.

For security, you may want to add some test code in the plugin being modified (say set a password) to prevent modifications by unwanted plugins.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文