【问题标题】:MEF ComposeExportedValue vs Export attributeMEF ComposeExportedValue 与导出属性
【发布时间】:2015-05-18 13:02:08
【问题描述】:

我很难理解为什么我们需要 ComposeExportedValue(objval) 而不是仅仅使用 [Export] 属性。

我有一个在shell中创建的应用程序对象,这个应用程序对象需要注入到prism模块中。

public class ShellBootsrapper : MefBootstrapper
{

    [Export(typeof(IMyApplication))]
    public MyApplication myApp; 

    protected override DependencyObject CreateShell()
    {
        this.Container.ComposeExportedValue<IMyApplication>(myApp);
        return this.Container.GetExportedValue<Shell>();
    }

    protected override void ConfigureAggregateCatalog()
    {
       base.ConfigureAggregateCatalog();
       myApp = new MyApplication();
       this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
      this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Module1.Module1).Assembly));
}

}

只有当我使用ComposeExportedValue&lt;IMyApplication&gt;(myApp);时,模块1才能导入

[ModuleExport(typeof(Module1))] 
public class Module1 : IModule
{

    private readonly IRegionManager regionManager;


    [Import]
    private IMyApplication myApp;

}

我希望 [Export] 就足够了,但显然不是?

编辑: 我从引导程序中删除了 public MyApplication myApp; 到 shell.xaml.cs(more sensible) ,然后一切正常。我得出结论; MEF 组合正在进行中,导出根本不起作用。这就是 prism 内部库使用 ComposeExportedValue(object val) 进行导出的原因

protected virtual void RegisterBootstrapperProvidedTypes()
{
    this.Container.ComposeExportedValue<ILoggerFacade>(this.Logger);
    this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatalog);
    this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocatorAdapter(this.Container));
    this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);
}

【问题讨论】:

  • 我更习惯将模块注入外壳,而不是相反。您确定要将 shell 注入到模块中吗?
  • 感谢您的建议。除了棱镜模块,我还有小型模块化业务应用程序对象。每个 prism 模块都需要将自己的小型业务应用程序模块/对象注册到位于 shell 中的主要业务应用程序。我发现将有限的接口(仅用于注册业务对象)发送到 Prism 模块更容易。 Prism 模块通过这个提供的接口将自己的业务模块注册到主业务应用程序。除此以外;我必须将所有业务模块导出/发送到 shell,它看起来不会让事情变得更简单。

标签: c# dependency-injection prism mef prism-5


【解决方案1】:

我从引导程序中删除了 public MyApplication myApp; 到 shell.xaml.cs(more sensible) ,然后一切正常。我得出结论;在引导程序 MEF 组合中正在进行中,导出根本不起作用。这就是 prism 内部库使用 ComposeExportedValue(object val) 进行导出的原因

protected virtual void RegisterBootstrapperProvidedTypes()
{
    this.Container.ComposeExportedValue<ILoggerFacade>(this.Logger);
    this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatalog);
    this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocatorAdapter(this.Container));
    this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);
}

我还发现 ComposeExportedValue 的目的之一是进行受控导出;即配置对象、设置属性等,然后将其导出。否则 MEF 将仅导出创建一个实例。

【讨论】:

    猜你喜欢
    • 2012-09-03
    • 2017-10-05
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多