Partager via


How to: Define Dependencies Between Modules

Overview

This topic describes how to define dependencies between the modules that compose a solution that uses the Composite Application Guidance. Defining dependencies varies, depending on the mechanism used to create the application's module catalog.

Note

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

Prerequisites

This topic assumes that you have a solution built with the Composite Application Library that has modules that will be loaded using one of the different loading mechanisms. For information about how to do this, see the following topics:

Steps

The following procedures describe how to define dependencies between modules when the catalog is populated from XAML.

To define dependencies between modules on a module catalog populated from XAML

  1. On a module catalog populated from XAML, a ModuleInfo element represents a module. Each ModuleInfo object has the DependsOn property; this property can be set to a list of module names that the module depends on. Use the DependsOn property element to set the dependencies of a module.

  2. The DependsOn property type is a collection of strings. Therefore, use one sys:String element—using the module name as the element value—for each module that the module depends on. The following code, extracted from the Remote Modularity QuickStart, shows that ModuleY depends on ModuleW.

    <Modularity:ModuleInfo ModuleName="ModuleY" ModuleType="ModuleY.ModuleY, ModulesWY.Silverlight, Version=1.0.0.0">
        <Modularity:ModuleInfo.DependsOn>
            <sys:String>ModuleW</sys:String>
        </Modularity:ModuleInfo.DependsOn>
    </Modularity:ModuleInfo>
    

    Note

    In a XAML-based catalog, modules can be defined in a Module Group element. Modules contained in a module group can only depend on other modules that are in the same module group or modules without a group.

The following procedure describes how to define dependencies between modules when the catalog is populated from code.

To define dependencies between modules on a module catalog populated from code

  • To add dependencies between modules when the module catalog is populated from code, use the DependsOn parameter of the AddModule method of the ModuleCatalog class. This property's type is a collection of strings. The following code extracted from the Defining Modules in Code QuickStart shows how dependencies are defined in the ModuleCatalog class.

    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;
    }
    

    As seen in the preceding code, ModuleA depends on ModuleD and ModuleD depends on ModuleB. Therefore, when the modules are initialized, ModuleB is going to be initialized before ModuleD, and ModuleA is going to be initialized after ModuleD.

The following procedure describes how to define dependencies between modules when the module catalog is populated from a directory. This is approach is only available on Windows Presentation Foundation (WPF) solutions.

To define dependencies between modules on a module catalog populated from a directory

  • To specify dependencies when using directory lookup module loading add a ModuleDependency attribute on the module class definition for every module that the module depends on, specifying the name as a parameter.

    [Module(ModuleName = "ModuleD")]
    [ModuleDependency("ModuleB")]
    public class ModuleD : IModule
    {
    …
    }
    

    As shown in the preceding code, ModuleD depends on ModuleB; therefore, ModuleB will be initialized before ModuleD.

The following procedure describes how to define dependencies between modules when the module catalog is populated from a configuration file. This is approach is only available on Windows Presentation Foundation (WPF) solutions.

To define dependencies between modules on a module catalog populated from a configuration file

  • To specify dependencies when using configuration module loading, between the modules tags in the App.config file, create a dependencies element that will contain one dependency object for every module—each dependency object specifies the module name in the moduleName property—that the module depends on.

    <module assemblyFile="Modules/ModuleA.dll" moduleType="ModuleA.ModuleA, ModuleA" moduleName="ModuleA">
      <dependencies>
        <dependency moduleName="ModuleD"/>
      </dependencies>
    </module>
    

    As shown in the preceding code, ModuleA depends on ModuleD; therefore, ModuleD will be initialized before ModuleA.

Outcome

Dependencies between modules are configured in the application's module catalog.

More Information

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