How to: Populate the Module Catalog from Code


This topic describes how to populate the module catalog from code. This is the most straightforward method of populating the module catalog. In this approach, the module information is added to the module catalog in code. If you have modules referenced directly, you can use the module type to add the module. However, directly referencing modules results in a less decoupled design. If you do not directly reference modules from the shell, you must provide the fully qualified type name and the location of the assembly.

Another advantage of this approach is that you can easily add conditional logic to determine which modules should be loaded.

This topic assumes that you are familiar with modules. For more information, see the Module technical concept.


This topic assumes that you have a solution built with the Composite Application Library that has a module. For information about how to do this, see the following topics:


The following procedure describes how to statically load modules by using a module catalog populated from code.

To create and populate the module catalog from code

  1. Add a reference to the module project in your Shell project.
  2. In your application's bootstrapper class file, add the following using statement at the top of the file. You will use it to refer to modularity elements in the Composite Application Library.
    using Microsoft.Practices.Composite.Modularity;
  3. Update the Bootstrapper class of your application to use a module catalog to specify which modules will be loaded and initialized. To do this, you typically override the Bootstrapper class's GetModuleCatalog template method. In this method, perform the following tasks:
    • Create a new instance of the ModuleCatalog class. This class also allows you to programmatically register modules groups and modules.
    • Register ModuleInfo instances within the ModuleCatalog instance by invoking the AddModule method on that instance.
    • The AddModule method takes the following parameters:
      • The module initializer class's type. A module initializer class is a class that implements the IModule interface.
      • The names of the modules that the module depends on.
      • The initialization mode of the module. This property can take the following values:
      • WhenAvailable. This is the default value. When the property takes this value, the module will be initialized as soon as it is available.
      • OnDemand. When the property takes this value, the module will not be initialized unless it is explicitly requested.
    • Return the ModuleCatalog instance. By returning the instance, the base class will register it with the Unity container.

The following code shows the implementation of the GetModuleCatalog method in the Defining Modules in Code QuickStart.

protected override IModuleCatalog GetModuleCatalog()
    ModuleCatalog catalog = new ModuleCatalog();
    catalog.AddModule(typeof (ModuleA), "ModuleD")
        .AddModule(typeof (ModuleB))
        .AddModule(typeof (ModuleD), "ModuleB")
        .AddModule(typeof (ModuleC), InitializationMode.OnDemand)
    return catalog;
This topic demonstrates how to populate the module catalog from code when you have direct references to the module classes. However, remote modules can also be defined in the module catalog from code by providing the fully qualified name and the location of the module assembly.


A module catalog, which contains metadata for all the application's modules, is created and populated from code.

More Information

For information about other ways that you can populate the module catalog, see the following topics:

For more information related to working with modules, see the following topics:

For a complete list of How-to topics included with the Composite Application Guidance, see Development Activities.

Home page on MSDN | Community site