How to configure modules with unity DI

Topics: Prism v4 - WPF 4
Jul 5, 2011 at 1:41 PM

Hi,

we want to use the PRISM bootstrapper with a configuration file (app.config).

All PRISM modules shall be configured in this configuration file.
Additional we want to configure dependencies of this modules via unity configuration in this configuration file.

How can we bring the two configurations together? In other words: how can we tell the bootstrapper to get modules dependencies from unity configuration?

Snippet of configuration file. ILogger is configured in the unity section.The module Navigation has a dependency of that ILogger. Can bootstrapper resolve this dependency without custom code?

    <container>
      <register type="IShell" mapTo="Shell">
        <lifetime type="singleton"/>
      </register>
      <register type="ILogger" mapTo="Logger">
        <lifetime type="singleton"/>
      </register>
      <register type="ILoggerFacade" mapTo="Logger">
        <lifetime type="singleton"/>
      </register>
    </container>
  </unity>
 
  <modules>
    <module assemblyFile="Gui.Common.dll" moduleType="Gui.Common.Navigation" moduleName="Navigation" startupLoaded="false">
    </module>
  </modules>

Snippet from Navigation code

class Navigation
{
[Dependancy(ILogger)]
ILogger Logger
{get;set;}
}

TIA, Matthias
Developer
Jul 5, 2011 at 3:26 PM

Hi Matthias,

In order to achieve what you're mentioining, you need to indicate the Unity container that it's going to retrieve the mappings from the configuration file, which should be done in the ConfigureContainer method in the Bootstrapper (which can be overriden).

You might find the following article useful, as it shows how to configure Unity from a separate configuration file (which should help keeping concerns clear, as the Unity configuration will be separated from the modules' configuration):

Unity Configuration from Separate Configuration File

Therefore, since the container is configured before the modules are loaded, at the time you load your modules, the mappings will be in place and your module's dependencies will be resolved.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jul 6, 2011 at 8:47 AM

Hi Guido,

thank you for answering. My problem is not to load unity containers from various files.

My bootstrapper container (UnityContainer since the bootstrapper derives from UnityBootstrapper)is initialized with the default configuration (IServiceLocator, IModuleCatalog etc) and additional with a unity section:

public class Bootstrapper:UnityBootstrapper
{
  ...

protected
override void ConfigureContainer() { base.ConfigureContainer(); var unitySection = ConfigurationManager.GetSection("unity") as UnityConfigurationSection; unitySection.Configure(base.Container); }

The bootstrapper container now has several additional (to the default mappings) type mappings in its Container. One of them is a mapping "ILogger" that is mapped to my implementation of an EntLib logger. The unity section where the logger is configured looks like that:

<unity>
 <container>
   <register type="ILogger" mapTo="ACustomLogger"/>
 </container>
</unity>

No I want the bootstrapper to create the modules from the same configuration in the modules section. Therefore I tell the bootstrapper to load the ModuleCatalog from the configuration file.

public class Bootstrapper:UnityBootstrapper
{
 ...
 protected override IModuleCatalog CreateModuleCatalog()
 {
   return new ConfigurationModuleCatalog();
 }
 ...

The module section in the configuration file looks like this:

<modules>
 <module assemblyFile="Modules.dll"
   moduleType="Modules.Navigation, Modules"
   moduleName="Navigation"/>
 ...

The bootstrapper loads the module Navigation and displays it in the region - all is fine so far.

But now I want the module Navigation to get a dependancy resolved from the bootstappers Catalog. Here is the Navigation class with the dependancy that is defined in the unity section and is mapped in the bootstrappers catalog:

public class Navigation:IModule
{
 ... 
 [Dependency()]
 private ILogger Logger
 {
  get;
  set;
 }
 ...

The Logger property is alwas null since the module catalog does not lokk for dependancies in the UnityCatalog. How can I achieve the resolution of dependancies without custom code?

TIA, Matthias

Jul 6, 2011 at 9:24 AM

Sorrs, sorry, sorry!

Now as I read my own posting, I hardly believe what I see.
Obviously even Unity can not access privat properties...

When I change the Navigations Logger property to public it works fine.

Thank you for your time Guido.

 

Regards, Matthias