Prism+MEF:延迟从 prism-module 导出服务
我有一个基于 Prism (v4 ctp) 和 MEF 的应用程序。 该应用程序有一个服务 IService1。我希望这个服务实现是由某个模块导出的(不仅仅是 MEF 发现的)
public interface IService1 {}
public class Service1Impl: IService1 {}
Service1Impl 没有 ExportAttribute。这是因为我想在我的 Prism 模块中手动创建实现:
[ModuleExport(typeof(SomeModule))]
[PartCreationPolicy(CreationPolicy.Shared)]
public SomeModule: IModule
{
[Export]
public IService1 Service1 {get; private set}
public void Initialize()
{
Service1 = new Service1Impl();
}
}
在其他一些组件中,我想通过 MEF 导入获取 IService1 的实现。 问题是在Prism调用Initialize之后如何告诉MEF进行导出(在SomeModule中)?
如果我在模块的构造函数中创建服务的实现,那么一切都会正常工作,但它与 Prism 的模块初始化过程不一致。 问题是 Prism 在 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 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();
}
}
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?
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.
So, what are my options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试过 ModuleDependency 吗?我需要对 MEF 进行更多研究,但在 Prism 中,您可以通过 ModuleDependency 保证一个模块的初始化在其他模块之前触发。
例如,如果您有 SomeOtherModule 在初始化期间需要 IService1,则可以确保 SomeModule 首先以这种方式初始化。
我完全限定这一点,因为我可能不知道 MEF 的某些内部工作原理不允许这样做,但这就是 Prism 的一般工作方式。
顺便说一句,当我看到依赖项时,无论是显式的还是隐式的,我都会问自己:
无论如何,最后一点只是值得思考的事情。
希望这有帮助。
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.
For example, if you had SomeOtherModule that needed an IService1 during Initialize, you could make sure that SomeModule was initialized first this way.
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:
Anyway, that last is just something to think about.
Hope this helps.
我对 MEF 的使用有点生疏,但这些想法可能有用。如果您的属性属于 concreate 实现类型,但您通过指定接口的契约导出该属性(我认为它类似于以下内容),该怎么办:
那么每个具有 IService1 接口导入检查的模块都将获得具体实现来自该模块(这是针对接口进行编码的主要目标)。
我希望这能引导您走向正确的方向。
谢谢,
达米安
I'm a bit rusty on my MEF usage, but these ideas might be useful. What if your property is of the concreate implementation type, but you export the property through a contract specifying the interface (I think it was similar to the following):
Then every module that has an import checking for the IService1 interface will get the concrete implementation from that module (which is the main goal of coding against the interface).
I hope this guides you in the right direction.
Thanks,
Damian