How to: Add a Page with a Presenter
You can use the 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 Page (with presenter) recipe
to generate a page with presenter.
The Add Page (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 page class. This means you can only use the recipe to add pages to modules that have names that are valid identifiers (for example, the module name does not include a period, such as
). If you use the recipe to add a page to a module with a name that is not a valid identifier, the generated code will not compile.
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.
The following procedure describes how to use the recipe to create a page with presenter.
To use the Add Page (with presenter) recipe to create a page with presenter
The Add Page (with presenter) recipe menu.
- In Solution Explorer, right-click a Web site or a folder in a Web site, point to
Web Client Factory, and then click Add Page (with presenter) (C#) or
Add Page (with presenter) (Visual Basic), as shown in Figure 1.
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 Page (with presenter) Wizard, as illustrated in Figure 2
The Add Page (with presenter) recipe wizard.
- In the Page name box, type the name for the page.
- Select the module. The recipe will create the presenter class and page interface in this project.
- (Optional) If you want to see a summary of the recipe actions and suggested next steps after the recipe completes, select the
Show documentation after recipe completes check box.
- Click Finish. The recipe generates the page with presenter.
You will have the following elements in your solution, as shown in Figure 3
Solution with the page View1 in the business module Module1.
- A page interface definition in the Views folder of the module project
- A presenter class in the Views folder of the module project
- A page implementation in the module's Web site folder
If you have a test project for the business module that contains the new page, the recipe will create a test fixture class for the presenter in the Views folder of the test project.
The following are typical tasks that you perform after you create a page with presenter:
- Design the UI of your page. Define styles and/or configure the page to use the correct master page.
- Implement your page. Add controls to the page and forward user events to the presenter.
- Define the page interface. The page should expose its state through its public interface. Typically, page interfaces have properties such as
Customers or SelectedAccount.
- Write unit tests and implement the presenter. Handle user events and update the page state. Interact with the module controller to execute business and navigation logic.