MEF:如何手动配置导出以执行合同
我有一个 MEF 的 CompositionContainer、一个合约(比如 IFoo)和一个模块(Prism 模块,但这里并不重要,只是一些组件)。 我想在我的模块中注册合约的实现(FooImpl)。
如果是 Unity 我会这样做:
unity.RegisterType<IFoo, FooImpl>().
仅此而已。 对于 MEF 我很困惑。我必须用 ExportAttribute 标记我的实现,但这会导致它会自动导出。我想自己处理这件事。 看一下代码:
class MyModule: IModule {
private CompositionContainer m_container;
public MyModule(CompositionContainer container) {
m_container = container;
}
public void Initialize() {
??? I want something like m_container.CreateExport<IFoo, FooImpl>()
}
}
public interface IFoo {}
public class FooImpl : IFoo {
//[ImportingConstructor]
public FooImpl(ISomeService svc) {}
}
在 Initialize 中,我希望手动导出 FooImpl 作为 IFoo 合约,而不依赖于 FooImpl 类上的 ExportAttribute。 我知道我只能创建一个实例 FooImpl (在上面的 MyModule.Initialize 中),但是 FooImpl 的构造函数依赖于其他组件/服务,我希望它们在创建时得到解决。
所以我可能应该问:如何手动添加具有 CompositionContainer 实例和合同的导出?然后以某种方式标记此导出,因为它具有 ImportingConstructorAttribute?
I have a MEF's CompositionContainer, a contract (say IFoo) and a module (Prism module but it doesn't matter much here, just some component).
I want in my module to register the contract's implementation (FooImpl).
If it's Unity I'd do like this:
unity.RegisterType<IFoo, FooImpl>().
That's all.
With MEF I've puzzled. I have to mark my implementation with ExportAttribute, but that lead to it'll be exported automatically. I want to manage this by myself.
Take a look at the code:
class MyModule: IModule {
private CompositionContainer m_container;
public MyModule(CompositionContainer container) {
m_container = container;
}
public void Initialize() {
??? I want something like m_container.CreateExport<IFoo, FooImpl>()
}
}
public interface IFoo {}
public class FooImpl : IFoo {
//[ImportingConstructor]
public FooImpl(ISomeService svc) {}
}
In Initialize I want manualy export FooImpl as IFoo contract not relying on ExportAttribute on FooImpl class.
I understand that I just can create an instance FooImpl (in MyModule.Initialize above), but
FooImpl has constructor dependencies on other component/services and I want they to be resolved on creation.
So probably I should ask: how to manually add export having a CompositionContainer instance and a contract? And then mark this export somehow as it has ImportingConstructorAttribute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 AttributedModelServices.ComposeExportedValue。你可以像这样使用它:
Have a look at AttributedModelServices.ComposeExportedValue. You would use it like this:
MEF 的最新预览版本有一个名为 RegistrationBuilder 的类,您可以使用它通过代码配置 MEF 部件。您可以从 MEF CodePlex 网站 获取预览版本。
这是 博客文章概述了新功能。
The latest preview version of MEF has a class called RegistrationBuilder which you can use to configure your MEF parts via code. You can get the preview version from the MEF CodePlex site.
Here's a blog post with an overview of the new features.
MEF 默认情况下不是这样工作的。但是,您可以创建自己的
ExportProvider
实现来执行此操作,并将其传递给CompositionContainer
构造函数。MEF contrib 项目有一些 导出提供程序实现。 Fluent Definition Provider 可能是最接近的你描述。还有一个提供统一集成的导出提供商。
That's not how MEF works by default. However, you can create your own
ExportProvider
implementation which does this, and pass it to theCompositionContainer
constructor.The MEF contrib project has a few examples of export provider implementations. The Fluent Definition Provider is probably the closest to what you describe. There is also an export provider which provides integration with unity.