How to: Load Modules on Demand
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.
This topic assumes that you have a solution built with the Composite Application Library that has a module configured to be statically or dynamically loaded. For information about how to do this, see the following topics:
- How to: Create a Solution Using the Composite Application Library. This topic describes how to create a solution with the Composite Application Library.
- How to: Create a Module. This topic describes how to add a new module to your solution.
- How to: Populate the Module Catalog from Code. This topic describes how to define modules in the module catalog in code.
- How to: Populate the Module Catalog from XAML. This topic describes how to populate the module catalog from a XAML file.
- How to: Populate the Module Catalog from a Configuration File or a Directory in WPF. This topic describes how to create and populate the module catalog from a configuration file or a directory to dynamically load modules in Windows Presentation Foundation (WPF) applications.
To load a module on demand
- Specify that the module you want to load on demand must not be loaded on startup. This step varies, depending on the method used to define the modules in the catalog.
If you intend to define the modules in a Silverlight or WPF application, see the following topics for information about how to perform this step:
In a WPF application, you can also populate the module catalog from a configuration file or a directory. If you are using one of these approaches, see the following topic for information about how to perform this step:
- Obtain a reference to the module manager service (this service implements the IModuleManager interface). To do this, you can use constructor dependency injection by adding a parameter to the class constructor that receives an instance of the module manager 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 manager 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 manager service instance as a parameter, and then it stores it in instance variables.
- Load the module by invoking the LoadModule method on the module manager service instance, providing the module name as a parameter. The following code shows how to do this.
A module gets loaded on demand.
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.