自定义“ExportFactory”
使用 MEF
的桌面应用程序导入许多“ServiceProviders”。每个部分(ServiceProvider)都是一个单独的 DLL 中的类。 所有 DLL 都位于桌面应用程序使用的“Plugin”文件夹中。
由于我需要部件的新实例,ExportFactory
是最佳选择。问题是我的部件有构造函数。我需要传递一些参数到 ExportFactory
不支持的部件的构造函数中(MEF2,Preview2)
我需要这样的东西:
// Each part has its own dependency
Dependency dependency = LoadDependency(myPart.Metedata["Name"]);
// Injecting dependency into part's constructor
myPart.CreateExport(dependency);
我不想从我的部件端导入任何东西(
带有参数-) 。 less 构造函数)可以在此处找到。
A desktop application using MEF
imports many 'ServiceProviders'. Each part (ServiceProvider) is a class inside a separate DLL.
All DLLs are in 'Plugin" folder which is used by desktop application.
Since I needed new Instances of my parts, ExportFactory
was the best choice. The problem is my parts have constructors. I need pass some parameters into constructor of the part which is not supported by ExportFactory
(MEF2, Preview2).
I need something like this:
// Each part has its own dependency
Dependency dependency = LoadDependency(myPart.Metedata["Name"]);
// Injecting dependency into part's constructor
myPart.CreateExport(dependency);
I don't want import anything from my parts-side.
A sample project (with parameter-less constructor) can be found here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 MEF 看到
ExportFactory
类型的导入时,它会以特殊方式处理它。它不是逐字查找ExportFactory
导出,而是查找IFoo
导出并神奇地生成该类型的工厂。您的错误是您希望这种魔法也能自动用于您自己的
ExportFactory
替代方案(您称为SrviceProviderFactory
)。这不是真的。当您在某处导入SrviceProviderFactory
时,MEF 实际上会查找该类型的导出。最简单的解决方案是为其提供此导出。为每个 IServiceProvider 实现手动导出工厂。例如,如果您有一个 FooServiceProvider:
那么您还需要有一个 FooServiceProviderFactory:
然后您的导入程序可以根据元数据选择正确的工厂:
这里烦人的是,对于每个服务提供者实现,您还需要创建并导出工厂实现。您可以通过创建通用工厂基类(例如
SrviceProviderFactory
)来节省工作量,但您仍然必须派生特定的类,因为您无法在 MEF 导出中使用泛型类型参数。 更新:我相信.NET 4.5现在支持导出开放泛型类型。这就是为什么 我已经建议您导出
Func
而不是,但显然您不喜欢这个答案。您还可以尝试复制
ExportFactory
魔法。这是可能的,但这是 MEF 的一个非常高级的用例。如果您想这样做,我建议您查看ExportFactoryProvider
的 MEF 源,了解如何构建您自己的支持参数的实现。When MEF sees an import of the type
ExportFactory<IFoo>
, it treats this in a special way. Instead of looking literally for anExportFactory<IFoo>
export, it looks instead for aIFoo
export and magically generates a factory for that type.Your mistake is that you expect this magic to also automatically work for your own alternative to
ExportFactory
which you calledSrviceProviderFactory
. This is not true. When you importSrviceProviderFactory<IFoo,IFooMetadata>
somewhere, MEF literally looks for an export of that type.The straightforward solution is to give it this export. Manually export a factory for each IServiceProvider implementation. For example, if you have a
FooServiceProvider
:Then you also need to have a FooServiceProviderFactory:
And then your importer can select the right factory based on metadata:
The annoying thing here is that for each service provider implementation, you also need to create and export a factory implementation. You can save work by creating a common factory base class (like your
SrviceProviderFactory
) but you still have to derive specific classes because you can't use generic type parameters in MEF exports. update: I believe .NET 4.5 now supports exporting open generic types.That's why I already suggested you export
Func
instead, but apparently you didn't like that answer.You could also try to replicate the
ExportFactory
magic. This is possible but a very advanced use case of MEF. If you want to do that I suggest you take a look at the MEF sources ofExportFactoryProvider
to see how to build your own implementation with support for parameters.