Partager via


How to: Load Modules On Demand

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Overview

A module can be loaded on demand during the application life cycle instead of being loaded during the application startup. For example, a module can be loaded on demand to delay the initialization of a resource-demanding feature of the application until it is required.

The purpose of this topic is to describe how to load modules on demand.

Prerequisites

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

Steps

To load a module on demand

  1. Specify that the module you want to load on demand should not be loaded on startup. This step varies depending on the module loading mechanism used.

    For detailed instructions on how to perform this step using any of the dynamic module loading mechanisms included in the Composite Application Library, see How to: Dynamically Load Modules.

  2. Obtain a reference to the module enumerator service (this service implements the IModuleEnumerator interface) and the module loader service (this service implements the IModuleLoader interface). To do this, you can use constructor dependency injection by adding two parameters to the class constructor that receives instances of the module enumerator service and module loader service.

    Note

    To use constructor dependency injection, your class must be instantiated using a dependency injection container. The container must be able to resolve the module enumerator and module loader services. If you are using the default UnityBootstrapper class to start your application, the container is already configured.

    The following code shows a class that receives a module enumerator and a module loader service instances as parameters, and then it stores them in instance variables.

    private readonly IModuleLoader moduleLoader;
    private readonly IModuleEnumerator moduleEnumerator;
    
    public DefaultViewB(IModuleLoader moduleLoader, IModuleEnumerator moduleEnumerator)
    {
        this.moduleLoader = moduleLoader;
        this.moduleEnumerator = moduleEnumerator;
    
        InitializeComponent();
    }
    
  3. Get a reference to the module you want to load on demand. To do this, invoke the GetModule method on the module enumerator service instance passing the module name as a parameter, as shown in the following code:

    ModuleInfo[] moduleInfo = moduleEnumerator.GetModule("ModuleName");
    
  4. Load the module by invoking the Initialize method on the module loader service instance providing the module information—obtained in the previous step—as a parameter. The following code shows how to do this.

    moduleLoader.Initialize(moduleInfo);
    

Outcome

A module gets loaded on demand.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.