Using the Directory Lookup Module Enumerator

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.

Prerequisites

This topic assumes that you have a solution based on the Composite Application Library with a module:

Steps

The following procedure describes how to set up dynamic module loading using the directory lookup module loading mechanism (you do this only once).

To set up the directory lookup module loading mechanism

  1. Update the Bootstrapper class of your application to load and initialize modules with the DirectoryLookupModuleEnumerator class. To do this, you typically override the Bootstrapper class’ GetModuleEnumerator template method. In this method, perform the following tasks:

    • Create a new instance of the DirectoryLookupModuleEnumerator class. In the constructor parameter, supply the folder path where module assemblies are stored. This class is responsible for discovering modules within a particular folder.
    • Return the DirectoryLookupModuleEnumerator instance. By returning the instance, the base class will register it with the Unity container.

    Note

    Module retrieval and module initialization is automatically handled by the Bootstrapper’s InitializeModules base method. When customization is needed, the InitializeModules method can be overridden to adjust it to your needs.

    The following code, extracted from the Dynamic Modularity (Directory Lookup) QuickStart, shows the overridden method GetModuleEnumerator. This method is invoked when the application starts, and demonstrates how to set up the DirectoryLookupModuleEnumerator module enumerator.

    protected override IModuleEnumerator GetModuleEnumerator()
    {
        return new DirectoryLookupModuleEnumerator(@".\Modules");
    }
    

    Note

    The preceding code assumes that module assemblies are stored in a folder named Modules under the folder where the application executable is located. This implies that the preceding code will not run successfully on your development environment by default. You can change your module assemblies' output path or set up a post-build event to automatically copy the assemblies to the Modules folder after each build. The Directory Lookup Modularity QuickStart uses the latter approach. This approach is explained in the next procedure.

The following procedure describes how to configure a module to be loaded with the directory lookup module loading mechanism.

To configure a module to be loaded with the directory lookup module loading mechanism

  1. Open the module's class file. A module class file contains the definition for a class that implements the IModule interface and it is typically named using the following format: {ModuleName}Module.cs.

  2. Add the following using statement at the top of the file.

    using Microsoft.Practices.Composite.Modularity;
    
  3. Configure the module by adding attributes to its class signature. To do this, add the required ModuleAttribute attribute and, optionally, the ModuleDependencyAttribute attribute to the module class signature.

    • The ModuleAttribute has two properties:
      • string ModuleName. Use this property to define the module's name. This property is required.
      • bool StartupLoaded. Set this property to true if you want the module to be loaded at startup (this is the default value). Set this property to false if you want the module to be loaded on demand. For more information about loading modules on demand, see How to: Load Modules on Demand.
    • The ModuleDependencyAttribute. This attribute is used to define the module's dependencies on other modules. If your module has multiple dependencies, add multiple ModuleDependencyAttribute instances on the module class.

    The following code shows a module class with the ModuleAttribute and ModuleDependencyAttribute attributes applied.

    [Module(ModuleName = "ModuleA")]
    [ModuleDependency("ModuleD")]
    public class ModuleA : IModule
    {
        ...
    }
    
  4. Add a post-build event to the module's project to copy the module's assembly to a common folder for module assemblies (the folder you specify must be the same that you specify when instantiating the DirectoryLookupModuleEnumerator). To do this, open the properties for the module's project, go to the Build Events tab, and insert a command in the Post-build event command line text box. You can use a command similar to the following, which was extracted from the Directory Lookup Modularity QuickStart.

    xcopy "$(TargetDir)*.*" "$(SolutionDir)DirectoryLookupModularity\bin\$(ConfigurationName)\Modules\" /Y
    
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.