Search code examples
unity-containerprismconfiguration-files

RegisterType from unity configuration file


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.

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- 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- Add config section in the app.config file:

<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" />

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>

Solution

  • It's now called RegisterTypes in the PrismApplicationBase. Just override that and do whatever you would have done in ConfigureContainer.

    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.