How to: Add a View with a Presenter
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
The Model-View-Presenter (MVP) pattern enables you 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 with testing the presentation logic without using a visual user interface.
The View (with presenter) recipe adds three items to an existing C# project in a smart client solution. It requires that you have an existing smart client solution with a C# project.
The smart client solution must contain the Infrastructure.Interface project with the Presenter base class, located in the rootnamespace.Infrastructure.Interface namespace (for example, GlobalBank.Infrastructure.Interface). The recipe extends this class to create a specific presenter for the view.
You can execute the View (with presenter) recipe to add a view interface, view, and presenter to your C# project.
To add the view to your project
- Using Visual Studio, open an existing smart client solution.
- In Solution Explorer, right-click a C# project node, point to Add, point to Smart Client Factory, and then click AddView (with presenter), as shown in Figure 1.
The Add View (with presenter) recipe menu
- The recipe starts the Add View (with presenter) Wizard. Figure 2 illustrates the first page of the wizard.
The Add View (with presenter) recipe wizard
- (Optional) If you want to create a new folder in the project where the recipe will create the new items, select the Create a folder for the view check box. If this check box is clear, the new items are placed in the project's root folder.
- (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.
You will have the following project items added to the selected project:
- A view interface class. This is an empty interface definition for the view. You modify this interface to define the public interface to the view.
- A view implementation user control for the view implementation. The class has the SmartPart attribute, derives from UserControl, and implements the view interface. It contains a reference to its presenter. You modify this class to call the presenter for user interface actions that affect other views or business logic.
- A presenter class for the view. This class extends the Presenter base class and contains the business logic for the view. You modify this class to update the view for your business logic.
If you selected the Create a folder for the view check box, you will have a new folder created that contains the preceding items. Figure 3 illustrates the solution structure after executing the View (with presenter) recipe.
Solution structure after executing View (with presenter) recipe, with the option to create a folder for the view
The following are typical tasks that you perform after you create a view with presenter:
- Design the UI of the view. Add controls to the view and forward user events to the presenter.
- Define the public view interface. The view should expose its state through its public interface.
- Create unit tests and implement the presenter. Handle user events and update the view state. Interact with services and the module controller to execute business and navigation logic.