Export (0) Print
Expand All

How to: Create a Solution Using the Composite Application Library

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 solution built with the Composite Application Library is a solution that you can use as a starting point for your composite Windows Presentation Foundation (WPF) application. The solution includes recommended practices and techniques, and it is the basis for the procedures in the Composite Application Guidance for WPF.

The goal of this topic is to create a shell that can compile and be the building block for your composite application.

Prerequisites

This topic requires you to have the following Composite Application Library and Unity Application Block assemblies:

  • Microsoft.Practices.Composite.dll
  • Microsoft.Practices.Composite.Wpf.dll
  • Microsoft.Practices.Composite.UnityExtensions.dll
  • Microsoft.Practices.Unity.dll
  • Microsoft.Practices.ObjectBuilder2.dll

The Composite Application Library ships as source code, which means you must compile it to get the Composite Application Library assemblies (Microsoft.Practices.Composite.dll, Microsoft.Practices.Composite.Wpf.dll, and Microsoft.Practices.Composite.UnityExtensions.dll).

To compile the solution

  1. In Windows Explorer, double-click the following batch file to open the Composite Application Library solution in Visual Studio:

    Open Composite Application Library Solution.bat

  2. Build the solution. The Composite Application Library assemblies will be placed at the CAL\Composite.UnityExtensions\bin\Debug folder.

Steps

To create a solution that uses the Composite Application Library, the following tasks must be performed:

  • Create a solution with a Shell project.
  • Define the Shell window layout.
  • Set up the application's bootstrapper.
  • (Optional) Create an infrastructure project.

The following procedure describes how to create a solution with a Shell project.

To create a solution with a Shell project

  1. In Visual Studio, create a new WPF application. To do this, point to New on the File menu, and then click Project. In the Project types list, click Windows in the Visual C# node. In the Templates box, click WPF Application. Finally, set the project's name, such as CALApplication1, specify a valid location, and then click OK.

    This project will be the Shell project of your application.

  2. (Optional) Using Windows Explorer, create a folder named Library inside your solution's folder, and then copy the following assemblies into the folder:
    • Microsoft.Practices.Composite.dll. This assembly contains the implementation of the Composite Application Library core components, such as modularity, logging and communication services, and several core interfaces’ definitions. This assembly does not contain UI-specific elements.
    • Microsoft.Practices.Composite.Wpf.dll. This assembly contains the implementation of Composite Application Library components that target WPF applications, including commands, regions, and events.
    •  Microsoft.Practices.Composite.UnityExtensions.dll. This assembly contains base and utility classes you can reuse in applications based on the Composite Application Library that consume the Unity Application Block. For example, it contains a bootstrapper base class—the UnityBootstrapper class—that creates and configures a Unity container with default services when the application starts.
    • Microsoft.Practices.Unity.dll and Microsoft.Practices.ObjectBuilder2.dll. These assemblies enable you to use the Unity Application Block in your application. By default, applications built with the Composite Application Guidance use the Unity Application Block. However, developers who prefer to use different container implementations can build adapters for them using the provided extensibility points in the Composite Application Library.
  3. In the Shell project, add references to the assemblies mentioned in the previous step.

The Shell window is a place to host different user interface (UI) components that exposes a way for itself to be dynamically populated by others. The following procedure explains how to define the Shell window layout.

To define the Shell window layout

  1. In Solution Explorer, rename the file Window1.xaml to Shell.xaml.
  2. Open the code behind file Shell.xaml.cs, and then rename the Window1 class to Shell using the Visual Studio refactoring tools. To do this, right-click Window1 in the class signature, point to Refactor, and then click Rename, as illustrated in Figure 1. In the Rename dialog box, type Shell in the New Name box, and then click OK. If the Preview Changes – Rename dialog box appears, click Apply.

    Ff921123.a2181f18-4c6e-43a5-9415-10e3f724d630(en-us,PandP.10).png

    Figure 1
    Renaming Window1 using Visual Studio refactoring tools

  3. Open the Shell.xaml file in XAML view and set the following attribute values to the Window root element:
    • x:Class = “CALApplication1.Shell” (The Class attribute has to match the code behind class’s name)
    • Title = “Shell”

    Your code should look like the following.

    <Window x:Class="CALApplication1.Shell"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Shell" Height="300" Width="300">
        <Grid>
    
        </Grid>
    </Window>
    
    
  4. Open the App.xaml file, and then change the StartupUri attribute of the Application node from Window1.xaml to Shell.xaml, as shown in the following code.
    <Application x:Class="CALApplication1.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Shell.xaml">
        <Application.Resources>
    
        </Application.Resources>
    </Application>
    
    

    The following steps describe how to add an ItemsControl control to the Shell window and associate a region to it.

  5. Add the following namespace definition to the root Window element. You need this namespace to use an attached property for regions; this property is defined in the Composite Application Library.
    xmlns:cal="http://www.codeplex.com/CompositeWPF"
    
    
  6. Replace the Grid control in the Shell window with an ItemsControl control named MainRegion, as shown in the following code.
    <Window x:Class="CALApplication1.Shell"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cal="http://www.codeplex.com/CompositeWPF"
        Title="Shell" Height="300" Width="300">
        <ItemsControl Name="MainRegion" />
    </Window>
    
    
  7. In the ItemsControl control definition, set the attached property cal:RegionManager.RegionName to "MainRegion”, as shown in the following code. This attached property indicates that a region named MainRegion is associated to the control
    <ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />
    
    

A Bootstrapper class is a class that is in charge of initializing the application. Typically, the bootstrapper initializes the container used for dependency injection, registers services and region adapters, creates and shows the Shell window, and loads modules. The following procedure explains how to set up the application's bootstrapper.

To set up the application's bootstrapper

  1. Add a new class file to the Shell project named Bootstrapper.cs.
  2. Add the following using statements at the top of the file.
    using Microsoft.Practices.Composite.UnityExtensions;
    using System.Windows;using Microsoft.Practices.Composite.Modularity;
    
    
  3. Update the signature of the Bootstrapper class to inherit from the UnityBootstrapper class.
    public class Bootstrapper : UnityBootstrapper
    {
    }
    
    
  4. Override the CreateShell method in the Bootstrapper class. The CreateShell method should obtain an instance of the Shell window and display it to the user. The following code illustrates a possible implementation of the CreateShell method.
    protected override DependencyObject CreateShell()
    {
        Shell shell = Container.Resolve<Shell>();
        shell.Show();
        return shell;
    }
    
    
    Ff921123.note(en-us,PandP.10).gifNote:
    You can return the Shell object or null. If you return the Shell object, the UnityBootstrapper base class will attach the default region manager to it.

  5. Override the GetModuleEnumerator method. In this template method, you typically create an instance of the module enumerator service, populate it with modules, and return it. The module enumerator service is a service included Composite Application Library; its interface is Microsoft.Practices.Composite.Modularity.IModuleEnumerator, and it is responsible for enumerating all the modules in the application. The Composite Application Library includes three module enumerator implementations out-of-the-box: the StaticModuleEnumerator, the DirectoryLookupModuleEnumerator, and ConfigurationModuleEnumerator. The following code illustrates how the Dynamic Modularity (Configuration) QuickStart implements the GetModuleEnumerator method.
    protected override IModuleEnumerator GetModuleEnumerator()
    { 
        return new ConfigurationModuleEnumerator();
    }
    
    
    Ff921123.note(en-us,PandP.10).gifNote:
    For more information about how to choose a module enumerator implementation, see How to: Dynamically Load Modules and How to: Statically Load Modules.

  6. Open the file App.xaml.cs, and then paste the following code inside the body of the App class to initialize the bootstrapper in the handler for the Startup event of the application.
    
    protected override void OnStartup(StartupEventArgs e))
    {
        base.OnStartup(e);
    
        Bootstrapper bootstrapper = new Bootstrapper();
        bootstrapper.Run();
    }
    
    
  7. Open the file App.xaml, and then remove the attribute StartupUri. This attribute is not required because you are manually instantiating the Shell window.
    Ff921123.note(en-us,PandP.10).gifNote:
    Further customization can be made to the Bootstrapper class by overriding methods of its base class. For more information, see How to: Register and Use Services.

Additionally, you can create an infrastructure project to store elements shared by multiple modules in the application. The following procedure describes how to create an infrastructure project.

To create an infrastructure project

  1. In your solution, add a new class library project named Infrastructure.
  2. Add references to the project to the following Composite Application Library assemblies:
    • Microsoft.Practices.Composite.dll
    • Microsoft.Practices.Composite.Wpf.dll
  3. Add references to the project to the following WPF assemblies:
    • PresentationCore.dll
    • PresentationFramework.dll
    • WindowsBase.dll
  4. Add a reference in the Shell project, to the Infrastructure project.

Outcome

You will have a Composite Application Library solution that you can use as the base to build a composite application. This solution is the base required for multiple procedures in this guidance. Figure 2 illustrates the final solution in Solution Explorer.

Ff921123.0576a03c-f4a7-4933-9f70-36a0cb6ad9fb(en-us,PandP.10).png

Figure 2
Final Solution in Solution Explorer

Next Steps

The following are typical tasks that you perform after you create a solution using the Composite Application Library:

  • Modify the Shell layout. For example, you can add or remove regions, or you can change the appearance and behavior (look and feel). For information about how to add a region, see How to: Add a Region.
  • Create modules. 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 information about how to create a module, see How to: Create a Module.

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.

Show:
© 2014 Microsoft