Model-View-Presenter
MVP
WinForms
Software Architecture
GUI Design

Model-View-Presenter in WinForms

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

WinForms makes it easy to put logic directly in button-click handlers, but that convenience quickly turns forms into hard-to-test classes. Model-View-Presenter, or MVP, is a practical way to separate screen behavior from business logic in WinForms. The presenter coordinates the view and the model, which makes the application easier to maintain and unit test.

Roles in WinForms MVP

In MVP:

  • the model contains business data and rules
  • the view displays data and exposes UI events
  • the presenter reacts to view events, talks to the model, and updates the view

In WinForms, the form usually implements a view interface. That is the key move: the presenter depends on an interface, not on a concrete Form class.

Define a View Interface

A simple example is a customer lookup screen.

csharp
1public interface ICustomerView
2{
3    string CustomerId { get; }
4    string CustomerName { set; }
5    string StatusMessage { set; }
6
7    event EventHandler LoadCustomerClicked;
8}

The interface exposes only what the presenter needs. It does not expose every control on the form.

Write the Presenter

The presenter listens for events and performs the application logic.

csharp
1public class CustomerPresenter
2{
3    private readonly ICustomerView _view;
4    private readonly CustomerService _service;
5
6    public CustomerPresenter(ICustomerView view, CustomerService service)
7    {
8        _view = view;
9        _service = service;
10        _view.LoadCustomerClicked += OnLoadCustomerClicked;
11    }
12
13    private void OnLoadCustomerClicked(object? sender, EventArgs e)
14    {
15        var customer = _service.FindById(_view.CustomerId);
16
17        if (customer == null)
18        {
19            _view.StatusMessage = "Customer not found";
20            _view.CustomerName = string.Empty;
21            return;
22        }
23
24        _view.CustomerName = customer.Name;
25        _view.StatusMessage = "Loaded";
26    }
27}

This code has no dependency on TextBox, Label, or other WinForms controls. That makes it straightforward to test.

Implement the View with a Form

The form wires its controls to the interface.

csharp
1public partial class CustomerForm : Form, ICustomerView
2{
3    public CustomerForm()
4    {
5        InitializeComponent();
6        var presenter = new CustomerPresenter(this, new CustomerService());
7    }
8
9    public string CustomerId => customerIdTextBox.Text;
10    public string CustomerName { set => customerNameLabel.Text = value; }
11    public string StatusMessage { set => statusLabel.Text = value; }
12
13    public event EventHandler? LoadCustomerClicked;
14
15    private void loadButton_Click(object sender, EventArgs e)
16    {
17        LoadCustomerClicked?.Invoke(this, EventArgs.Empty);
18    }
19}

The form stays thin. It forwards events and displays values, but it does not decide what the business rules are.

Why MVP Fits WinForms Well

WinForms already encourages event-driven UI code, so MVP is a natural fit. The presenter becomes a testable home for that event handling. Compared with putting everything into the form code-behind, you gain:

  • better separation of concerns
  • easier unit testing
  • less duplicated UI logic across forms
  • reduced coupling between controls and domain rules

This is especially useful when screens become more complex than a few buttons and text boxes.

Unit Testing the Presenter Is the Real Payoff

Because the presenter depends on ICustomerView, you can replace the form with a fake view in tests. That lets you verify behavior such as “when the service returns no customer, show an error message” without launching a real UI. In practice, that testability is the main reason teams adopt MVP in WinForms rather than keeping logic in event handlers.

Common Pitfalls

  • Letting the presenter manipulate controls directly instead of using the view interface.
  • Making the view interface too large and leaking UI details into the presenter.
  • Duplicating business logic in both the presenter and the model.
  • Treating MVP as ceremony on very small forms where it adds no value.
  • Instantiating dependencies inside the presenter when they should be injected.

Summary

  • In WinForms MVP, the form implements a view interface.
  • The presenter handles events and coordinates with the model.
  • Business behavior moves out of the form code-behind.
  • MVP improves testability and maintainability for non-trivial screens.
  • The view should stay thin and focused on display and user interaction.

Course illustration
Course illustration

All Rights Reserved.