MEF 中有没有办法让组合对象获取指向所使用的 ObjectComposer 的指针?

发布于 2024-10-16 18:19:48 字数 223 浏览 1 评论 0原文

尝试建立一个不平凡的 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 技术交流群。

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

发布评论

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

评论(2

_蜘蛛 2024-10-23 18:19:48

我假设“ObjectComposer”指的是 MEF 容器。我不知道名为 ObjectComposer 的类或概念。

编辑 CompositionContainer 文档 现在明确提到您不应将容器放入其自身中,因此下面有删除线。)


在您的应用程序启动代码中,容器可以自行注册:

CompositionContainer container = new CompositionContainer(...);
container.ComposeExportedValue<ExportProvider>(container);

然后您可以将其导入到任何地方,如下所示:

[Import]
public ExportProvider Composer { get; set; }

但是,您在这里要求的是一个服务定位器,它有某些缺点。最好导入更具体的服务,例如抽象工厂。

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:

CompositionContainer container = new CompositionContainer(...);
container.ComposeExportedValue<ExportProvider>(container);

Then you could import it anywhere like this:

[Import]
public ExportProvider Composer { get; set; }

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.

雨轻弹 2024-10-23 18:19:48

我就是这样做的:

我的很多出口商都进口 IMain。

[Import(typeof(IMain))]
public IMain Main { get; set; }

IMain 用作事件服务和原始组合服务的容器。

public interface IMain
{
    ICompositionService CompositionService { get; set; }
    EventPublisher Events { get; set; }
    // more...
}

最初的组合是这样的:

AggregateCatalog Catalog = new AggregateCatalog();
Catalog.Catalogs.Add(new DirectoryCatalog(Application.StartupPath));
CompositionContainer Container = new CompositionContainer(Catalog);
this.CompositionService = Container;  // <- here I'm setting the IMain composition service
Container.ComposeParts(this);

然后当我想要组合一个新对象时,我只需将它传递给组合服务:

MainPanel discoverPanel = new MainPanel();
Main.CompositionService.SatisfyImportsOnce(discoverPanel);
discoverPanel.Show();

This is how I do it:

Lots of my Exporters Import IMain.

[Import(typeof(IMain))]
public IMain Main { get; set; }

IMain is used as a container for an event service and the original composition service.

public interface IMain
{
    ICompositionService CompositionService { get; set; }
    EventPublisher Events { get; set; }
    // more...
}

The initial composition is like this:

AggregateCatalog Catalog = new AggregateCatalog();
Catalog.Catalogs.Add(new DirectoryCatalog(Application.StartupPath));
CompositionContainer Container = new CompositionContainer(Catalog);
this.CompositionService = Container;  // <- here I'm setting the IMain composition service
Container.ComposeParts(this);

Then when I want to compose a new object, I just pass it to the composition service:

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