MEF 中有没有办法让组合对象获取指向所使用的 ObjectComposer 的指针?
尝试建立一个不平凡的 MEF 应用程序。
我想知道 - 有没有一种方法可以让正在组合的对象获得指向用于组合它的 ObjectComposer 的指针?
场景是对象可能必须进行内部合成。我可以通过属性或构造函数上的 IMPORT attrobites 推送到目前为止的所有链接,但我错过了组合对象获取对象组合器的方法(因此它可以为其内部项目创建子组合器)。
有什么标准/最佳实践方法吗?
trying to put up a nontrivial MEF application.
I wonder - is there a way for an object being composed to get a pointer to the ObjectComposer used to compose it?
The scenario is that the obejct may have to make inner compositions. I can push all links so far wia IMPORT attrobites on properties or the constructor, but I miss a way for the composed object to get the object composer (so it could create a sub-composer for it's inner items).
Any standard / best practice way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设“ObjectComposer”指的是 MEF 容器。我不知道名为
ObjectComposer
的类或概念。(编辑: CompositionContainer 文档 现在明确提到您不应将容器放入其自身中,因此下面有删除线。)
在您的应用程序启动代码中,容器可以自行注册:
然后您可以将其导入到任何地方,如下所示:
但是,您在这里要求的是一个服务定位器,它有某些缺点。最好导入更具体的服务,例如抽象工厂。
ExportFactory
在这里也可能有帮助,但这并没有将其纳入 .NET 4 版本。如果您想尝试的话,您必须使用最新的 codeplex 版本的 MEF。I assume that by "ObjectComposer", you mean the MEF container. I am not aware of a class or concept named
ObjectComposer
.(edit: the CompositionContainer documentation now explicitly mentions that you shouldn't put a container in itself. Hence the strike-through below.)
In your application start-up code, the container could register itself in itself:
Then you could import it anywhere like this:
However, what you are asking for here is a Service Locator, which has certain disadvantages. It is better to import more specific services, like an abstract factory.
ExportFactory
could also be helpful here, but that didn't make it into the .NET 4 release. You'll have to use the latest codeplex release of MEF if you want to try that.我就是这样做的:
我的很多出口商都进口 IMain。
IMain 用作事件服务和原始组合服务的容器。
最初的组合是这样的:
然后当我想要组合一个新对象时,我只需将它传递给组合服务:
This is how I do it:
Lots of my Exporters Import IMain.
IMain is used as a container for an event service and the original composition service.
The initial composition is like this:
Then when I want to compose a new object, I just pass it to the composition service: