MEF - 我需要实施 IPartImportsSatisfiedNotification

发布于 2024-08-03 14:00:20 字数 599 浏览 8 评论 0原文

public interface IPlugin
{
    public bool execute();
}

我的所有“部分”都实现了这个 IPlugin 接口。我的零件显然有导入/导出要求/产品。

我正在编写一个构建+配置系统,其中用户动态选择他/她想要的内容,这转化为调用一组插件。

例如,以下是插件列表:

(1) Install X ... 导出“XTypeInstalled”

(2) 配置 X ... 导入“XTypeInstalled”,导出“XTypeConfigured”

(3) Install Y ... 导入“XTypeConfigured”

(4) 安装 Z

(5) 配置 A

现在,用户可以选择 (1)、(3) 和 (4) ...,或者可以选择 (1)、(2)、(3)

我遇到的问题面临的是,我所有的插件编写者现在都需要实现 IPartImportsSatisfiedNotification 吗?如果没有,并且用户选择 (1)、(2) 和 (3) 的工作流程...我如何调用 (3) 的execute() 方法。

我说得有道理吗?!

public interface IPlugin
{
    public bool execute();
}

All my "parts" implement this IPlugin interface. My parts obviously have Import/Export requirements/offerings.

I'm writing a build+config system, in which the user dynamically selects what he/she wants, which translates to a set of plugins being called.

For example, here's a list of plugins:

(1) Install X ... exports "XTypeInstalled"

(2) Configure X ... imports "XTypeInstalled", exports "XTypeConfigured"

(3) Install Y ... imports "XTypeConfigured"

(4) Install Z

(5) Configure A

Now, a user could select (1), (3), and (4) ... or could select (1), (2), (3)

The problem I'm facing is, do all my plugin writers now need to implement IPartImportsSatisfiedNotification? If not, and the user selects a workflow of (1), (2), and (3) ... how do I get to calling (3)'s execute() method.

Am I making sense?!

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

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

发布评论

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

评论(1

故事未完 2024-08-10 14:00:20

我不确定是否使用 MEF 来管理这样的程序的运行时流程。 MEF 是定位和加载插件的绝佳选择,但它是为非常松散耦合的系统设计的,因此您可能没有所需的控制级别。

例如,尽管依赖关系解析过程将向任务提供其依赖关系,但没有机会检查依赖关系结构是什么。

例如,当您想保证某项任务仅执行一次,或者当您想用一个任务替换另一个任务时,事情可能会很棘手。

我的建议是您使用 MEF 来查找插件,但开发一个独立于 MEF 的丰富对象模型来执行它们。

例如:

[Export(typeof(IPlugin)), ExportMetadata("Name", "ConfigureX")]
public class ConfigureXPlugin : IPlugin { ...

然后使用 MEF 查找所有可用的插件:

public class BuildRunner {
  [ImportMany]
  Lazy<IPlugin, IPluginMetadata> plugins[];

  void RunBuild(...) {
    // Figure out which tasks should execute, in which order, and call each of them

希望这会有所帮助。如果您仍然遇到问题,发布有关您的问题的更多信息将会有所帮助。

缺口

I'm not sure about using MEF to manage the runtime flow of a program like this. MEF is a great choice for locating and loading plugins, but it is designed for very loosely-coupled systems, so you may not have the level of control you need.

For example, though the dependency resolution process will provide tasks with their dependencies, there's no opportunity to inspect what the dependency structure is.

Things may be tricky when, for instance, you want to guarantee that a task executes only once, or when you want to substitute one task with another.

My recommendation is that you use MEF to find the plug-ins, but develop a rich object model independent of MEF for executing them.

E.g.:

[Export(typeof(IPlugin)), ExportMetadata("Name", "ConfigureX")]
public class ConfigureXPlugin : IPlugin { ...

Then use MEF to find all the available plugins:

public class BuildRunner {
  [ImportMany]
  Lazy<IPlugin, IPluginMetadata> plugins[];

  void RunBuild(...) {
    // Figure out which tasks should execute, in which order, and call each of them

Hope this helps. If you're still having trouble, posting some more information about your problem would help.

Nick

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