How to: Add a User Control with a Presenter

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.

You can use the Model-View-Presenter pattern to separate the responsibilities for the visual display and the event handling behavior into different classes. Separation of these concerns leads to simplified classes, allows reuse of presentation logic, and helps you test the presentation logic without a visual user interface.

This topic describes how to use the Add User Control (with presenter) Recipe to generate a user control with presenter.

Prerequisites

The Add User Control (with presenter) recipe assumes that you have an existing Web client solution. For information about how to create a Web client solution, see How to: Create a Web Client Solution.

The recipe applies to a Web site root node or to a folder in a Web site that contains a Web.config file (typically, this is a Web folder for a business module).

The recipe uses the module name as a prefix for the generated user control class. This means you can only use the recipe to add user controls to modules that have names that are valid identifiers (for example, the module name does not include a period, such as Reports.Module). If you use the recipe to add a user control to a module with a name that is not a valid identifier, the generated code will not compile.

Note

The Add Business Module recipe requires you to use a valid identifier for the module name. If you rename a business module after you create it, select a name that is a valid identifier.

Steps

The following procedure describes how to use the recipe to create a user control with presenter.

To use the Add User Control (with presenter) recipe to create a user control with presenter

  1. In Solution Explorer, right-click a Web site or a folder in a Web site, point to Web Client Factory, and then click Add User Control (with presenter) (C#) or Add User Control (with presenter) (Visual Basic), as shown in Figure 1.

    Ff709824.24c6bced-72d2-4010-b7ab-675fe8c2cb81(en-us,PandP.10).png

    Figure 21

    The Add User Control (with presenter) recipe menu

    Note

    If you are running the recipe in a Web Application project, you will see only the recipe menu items corresponding to the project’s language.

    The recipe starts the Add User Control (with presenter) wizard, as illustrated in Figure 2.

    Ff709824.a4e059c3-d5c4-4d66-9ee1-6cb8177f4837(en-us,PandP.10).png

    Figure 22

    The Add User Control (with presenter) recipe wizard

  2. In the User control name box, type the name for the user control.

  3. Select the module. The recipe will create the presenter class and user control interface in this project.

  4. Click Finish. The recipe generates the user control with presenter.

Outcome

You will have the following elements in your solution, as shown in Figure 3:

  • A user control interface definition in the Views folder of the module project

  • A presenter class in the Views folder of the module project

  • A user control implementation in the module's Web site folder

    Ff709824.5477c9a2-63fa-4ebc-a19b-5a541ab7d5e1(en-us,PandP.10).png

    Figure 23

    Solution with the user control View1 in the business module Module1

Note

If you have a test project for the business module that contains the new user control, the recipe will create a test fixture class for the presenter in the Views folder of the test project.

Next Steps

The following are typical tasks that you perform after you create a user control with presenter:

  • Design the UI of your user control. Define styles and the layout of the user control.
  • Implement your user control. Add controls to the user control and forward user events to the presenter.
  • Define the user control interface. The user control should expose its state through its public interface. Typically, user control interfaces have properties such as Customers or SelectedAccount.
  • Create unit tests and implement the presenter. Handle user events and update the user control state. Interact with the module controller to execute business and navigation logic.
  • Consume the user control. Add the user control to your pages.