来自统一配置文件的RegisterType [英] RegisterType from unity configuration file
问题描述
我正在从Prism 4迁移到Prism 7.1,似乎无法发现ConfigureContainer方法已从最新的Prism版本中删除。过去,我曾使用此方法从文件系统加载统一配置。
I am migrating from Prism 4 to Prism 7.1, I cannot seem to find the ConfigureContainer method has been removed from the latest Prism release. In the past, I had used this method to load the unity configuration from the file system.
使用Prism库的最新版本,这似乎是不可能的。
我已经探索过ModuleConfiguration的选项,对我而言,它不提供以相同方式通过配置文件注入依赖项的功能。
with the latest version of the Prism library, this appears not to be possible. I have already explored the option of ModuleConfiguration, which to me does not provide the ability to inject dependencies through a configuration file in the same way.
为此,我可以通过配置文件提供类型注册。
Is there an alternate approach for this, where I can provide type registration through a configuration file.
这是我过去的操作方式:
Here is how I did it in the past:
1-在BootStrapper中,以下方法被覆盖:
1- In the BootStrapper following method was overridden:
protected override void ConfigureContainer()
{
base.ConfigureContainer();
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
UnityConfigurationSection section = (UnityConfigurationSection)config.GetSection("unity");
if (section != null)
{
section.Configure(Container);
}
}
2-在app.config文件中添加配置部分:
2- Add config section in the app.config file:
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" />
3-添加一个统一的配置文件,如下所示:
3- Add a unity config file which looks like:
<unity xmlns="schemas.microsoft.com/practices/2010/unity">
<sectionExtension type="Unity.FactoryConfig.FactoryConfigExtension, Unity.FactoryConfig"/>
<alias alias="Singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity"/>
<alias alias="ConfigFactory" type="Vms.Pt.Common.DependencyInjection.ComponentBuilder.ConfigClassFactory`1, Vms.Pt.Common.DependencyInjection.ComponentBuilder"/>
<container>
<!--Modal/popup provider service-->
<register type="IPopupService, GUI.Infrastructure"
mapTo="Services.PopupService, GUI.Infrastructure">
<lifetime type="Singleton"/>
</register>
</container>
</unity>
推荐答案
现在称为 RegisterTypes
在 PrismApplicationBase
中。只需重写它,然后执行 ConfigureContainer
中的所有操作即可。
It's now called RegisterTypes
in the PrismApplicationBase
. Just override that and do whatever you would have done in ConfigureContainer
.
提示:如果您不喜欢在您和 IUnityContainer
之间放置抽象 Prism 7,您可以在 GetContainer()
> IContainerRegistry (这是一种扩展方法)来获取隐藏的 IUnityContainer
实例。
Hint: if you don't like the "abstraction" Prism 7 put between you and IUnityContainer
, you can call GetContainer()
on the IContainerRegistry
(it's an extension method) to get the hidden IUnityContainer
instance.
这篇关于来自统一配置文件的RegisterType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!