Prism + MEF:延迟了从棱镜模块导出服务 [英] Prism+MEF: delayed a service export from prism-module

查看:132
本文介绍了Prism + MEF:延迟了从棱镜模块导出服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Prism(v4 ctp)和MEF的应用程序. 该应用程序具有服务IService1.我希望此服务实现由某个模块导出(而不仅仅是由MEF发现)

I have an app based on Prism (v4 ctp) and MEF. The app has a service IService1. I want this service implementation was exported by some module (not just discovered by MEF)

public interface IService1 {}
public class Service1Impl: IService1 {}

Service1Impl没有ExportAttribute.这是因为我想在我的Prism模块中手动创建实现:

Service1Impl doesn't have ExportAttribute. This' because I want to create the implementation by hand in my Prism-module:

[ModuleExport(typeof(SomeModule))]
[PartCreationPolicy(CreationPolicy.Shared)]
public SomeModule: IModule
{
    [Export]
    public IService1 Service1 {get; private set}

    public void Initialize()
    {
        Service1 = new Service1Impl();
    }
}

在其他一些组件中,我想通过MEF Import获得IService1的实现. 问题是在Prism调用Initialize之后如何告诉MEF进行导出(在SomeModule中)?

In some other components I want to get IService1's implementation through MEF Import. The problem is how to tell MEF to do export (in SomeModule) after Initialize was called by Prism?

如果我在模块的构造函数中创建服务的实现,则一切正常,但与Prism的模块初始化过程不一致. 问题是Prism在MEF的编写完成后初始化模块. 此外,在创建服务的实现之前,我需要执行一些复杂的初始化逻辑,并且我不希望它包含在构造函数中.

If I create the service's implementation in the module's constructor then everything works fine, but it's inconsistent with Prism's modules initialization process. The thing is that Prism initializes modules after MEF's composition completes. Moreover before creation of the service's implementation I need to perform some complex initialization logic and I don't want it to be in constructor.

那我有什么选择?

推荐答案

您是否尝试过ModuleDependency?我需要对MEF进行更多研究,但是在Prism中,您可以通过ModuleDependency保证一个模块的Initialize先于其他模块触发.

Have you tried a ModuleDependency? I need to do some more research on MEF, but in Prism, you can guarantee that one Module's Initialize fires before the others via a ModuleDependency.

例如,如果您有在初始化期间需要IService1的SomeOtherModule,则可以确保以这种方式首先初始化SomeModule.

For example, if you had SomeOtherModule that needed an IService1 during Initialize, you could make sure that SomeModule was initialized first this way.

[ModuleDependency("SomeModule")]
public class SomeOtherModule : IModule
{
    [Import]
    public IService1 Service1 {get; set;}

    public void Initialize()
    {
        //This ought to be populated now.
        Service1.DoSomething();
    }
}

我通过说我可能不知道MEF的某些内部工作不允许这样做来完全证明了这一点,但这是Prism总体上的工作方式.

I'm fully-qualifying this by saying I might not know some inner working of MEF that does not allow this, but this is how things work generally with Prism.

顺便说一句,当我看到依赖项时,无论是显式还是隐式,我都会问自己:

As an aside, when I see a dependency, whether it be explicit or implicit, I ask myself:

  1. 该服务是否将由多个模块使用?那么,将服务提升为在Bootstrapper中构建,由托管应用程序而非其他模块提供的东西有意义吗?
  2. 如果不是#1,如果模块之间存在依赖关系,那么这些模块在逻辑上是否相同?应该合并吗?如果一个人离不开另一个人,那么没有理由不将它们结合在一起.

无论如何,最后只是要考虑的事情.

Anyway, that last is just something to think about.

希望这会有所帮助.

这篇关于Prism + MEF:延迟了从棱镜模块导出服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆