How to: Create a Module

Overview

A module encapsulates a set of related concerns. Modules are independently developed and deployed, and they interact with each other to create the application. For more information about modules, see the Modules technical concept.

This topic describes how to create a module.

Prerequisites

This topic assumes that you already have a solution based on the Composite Application Library. For information about how to do this, see How to: Create a Solution Using the Composite Application Library.

Steps

The following procedure describes how to create a module.

To create a module

  1. Add a new project to your solution to contain your module. The project type varies, depending on the environment used and the type of module:

    • If you want to create a Windows Presentation Foundation (WPF) module, add a new class library project to your solution.
    • If you want to create a Silverlight module that will be part of the main application XAP file, add a new Silverlight class library project to your solution, and then add a reference to this library from the Shell project.
    • If you want to create a Silverlight module enabled for remote retrieval, add a new Silverlight application to your solution. When the Add Silverlight Application dialog box appears, do the following:
      1. Host the module on the same Web site as the main application. To do this, click Link this Silverlight control into an existing Web site and verify that the correct Web site is selected on the drop-down list.
      2. Clear the Add a test page that references the application check box, and then click OK.
  2. Add references to the appropriate assemblies:

    • If your are creating a WPF module, add the following references:
      • PresentationCore.dll
      • PresentationFramework.dll
      • WindowsBase.dll
      • Microsoft.Practices.Composite.dll
      • Microsoft.Practices.Composite.Presentation.dll
    • If you are creating a Silverlight module, add the following references:
      • Microsoft.Practices.Composite.dll
      • Microsoft.Practices.Composite.Presentation.dll
  3. Delete the file Class1.cs.

  4. Add a new class file named {ModuleName}Module.cs to your module project (replace {ModuleName} with your module's name).

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

    using Microsoft.Practices.Composite.Modularity;
    
  6. Change the class signature to make it public and to implement the IModule interface, as shown in the following code.

    public class MyModuleModule : IModule
    {
    }
    
  7. Implement the Initialize method of the IModule interface. In this method, you implement logic to initialize the module. For example, you can register views and services or add views to regions. The following code, extracted from the Defining Modules in Code QuickStart for Silverlight, shows an example implementation of the Initialize method.

    public class ModuleC : IModule
    {
        private readonly IRegionManager regionManager;
    
        public ModuleC(IRegionManager regionManager)
        {
            this.regionManager = regionManager;
        }
    
        public void Initialize()
        {
            this.regionManager.Regions["MainRegion"].Add(new DefaultViewC());
        }
    }
    

    Note

    For more information about how to register services, see to How to: Register and Use Services.
    For more information about how to add views, see How to: Show a View in a Region Using View Discovery UI Composition and How to: Show a View in a Region Using View Injection UI Composition.

  8. Consider adding folders to organize the project. The following are common folders that you can add to your module to achieve a better organization:

    • Views. In this folder, you store views implementations.
    • Services. In this folder, you store service implementations and service interfaces.
    • Controllers. In this folder, you store controllers.
  9. Configure the module so that it gets loaded when the application starts or on demand. To use modules in an application, they have to be defined in the application's module catalog. Consider the following:

    Note

    If you are creating a module for remote retrieval, see the How to: Prepare a Module for Remote Downloading topic for additional configuration.

Outcome

You will have a new module in your solution.

Next Steps

The following are typical tasks that you perform after you create a module:

More Information

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

Home page on MSDN | Community site