MEF 的 ComposableParts 合约是基于实例的吗?

发布于 2024-09-04 19:04:08 字数 990 浏览 8 评论 0原文

我真的不知道如何表达我的问题标题,所以我提前道歉。我通读了 MEF 文档的部分内容,试图找到我的问题的答案,但找不到。

我使用 ImportMany 允许 MEF 创建特定插件的多个实例。该插件导入多个部分,并且在对特定实例的调用中,它希望这些导入是单例的。但是,我不希望该插件的所有实例都使用相同的单例。

例如,假设我的应用程序 ImportMany 是 Blender 设备。每次我要一个时,我都想要一个不同的搅拌机。但是,每个 Blender Import 都是一个控制面板。我希望每个搅拌机都有自己的控制面板。为了让事情变得更有趣,每个 Blender 都可以加载 BlendPrograms,这些程序也包含在它们自己的程序集中,并且 MEF 负责此加载。 BlendProgram 可能需要访问 ControlPanel 才能获得速度,但我想确保它访问正确的 ControlPanel(即与与程序关联的 Blender 关联的控制面板!)

此图可能会澄清问题一点点:

alt text

正如注释所示,我相信这种混乱可能来自于本质上的贫困设计。 BlendProgram 不应直接接触 ControlPanel,相反,BlendProgram 可能应通过 Blender 获取速度,然后 Blender 将请求委托给其 ControlPanel。

如果是这种情况,那么我认为 BlendProgram 需要引用特定的 Blender。为了做到这一点,正确的方法是利用 MEF 并使用 BlendProgram 的 ImportingConstructor,即

[ImportingConstructor] 公共类 BlendProgram : IBlendProgram { 公共 BlendProgram(搅拌机搅拌机){} 如果

是这种情况,我怎么知道 MEF 将使用预期的 Blender 插件?

I didn't really know how to phrase the title of my questions, so my apologies in advance. I read through parts of the MEF documentation to try to find the answer to my question, but couldn't find it.

I'm using ImportMany to allow MEF to create multiple instances of a specific plugin. That plugin Imports several parts, and within calls to a specific instance, it wants these Imports to be singletons. However, what I don't want is for all instances of this plugin to use the same singleton.

For example, let's say my application ImportManys Blender appliances. Every time I ask for one, I want a different Blender. However, each Blender Imports a ControlPanel. I want each Blender to have its own ControlPanel. To make things a little more interesting, each Blender can load BlendPrograms, which are also contained within their own assemblies, and MEF takes care of this loading. A BlendProgram might need to access the ControlPanel to get the speed, but I want to ensure that it is accessing the correct ControlPanel (i.e. the one that is associated with the Blender that is associated with the program!)

This diagram might clear things up a little bit:

alt text

As the note shows, I believe that the confusion could come from an inherently-poor design. The BlendProgram shouldn't touch the ControlPanel directly, and instead perhaps the BlendProgram should get the speed via the Blender, which will then delegate the request to its ControlPanel.

If this is the case, then I assume the BlendProgram needs to have a reference to a specific Blender. In order to do this, is the right way to leverage MEF and use an ImportingConstructor for BlendProgram, i.e.

[ImportingConstructor]
public class BlendProgram : IBlendProgram
{
public BlendProgram( Blender blender) {}
}

And if this is the case, how do I know that MEF will use the intended Blender plugin?

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

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

发布评论

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

评论(1

人心善变 2024-09-11 19:04:08

你应该像提到的那样打破它。正如你所说,通过搅拌机暴露你的控制面板。如果您确实想保留当前的设计,则必须使用元数据装饰导入,并创建一个自定义导出提供程序,该提供程序将使用此元数据来确定应使用什么导出。如果你可以去掉这个额外的步骤,那就去做吧。

You should break it as mentioned. Expose your control panel through your blender as you say. If you really want to keep your current design, you'll have to decorate the import with metadata and make a custom export provider that will use this metadata to determine what export it should use. If you can get rid of this extra step, do it.

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