How would you implement MVC in a Windows Forms application?

WinformsModel View-Controller

Winforms Problem Overview


I don't develop too many desktop / Windows Forms applications, but it had occurred to me that there may be some benefit to using the MVC (Model View Controller) pattern for Windows Forms .NET development.

Has anyone implemented MVC in Windows Forms? If so, do you have any tips on the design?

Winforms Solutions


Solution 1 - Winforms

What I've done in the past is use something similar, Model-View-Presenter.

[NOTE: This article used to be available on the web. To see it now, you'll need to download the CHM, and then view the file properties and click Unblock. Then you can open the CHM and find the article. Thanks a million, Microsoft! sigh]

The form is the view, and I have an IView interface for it. All the processing happens in the presenter, which is just a class. The form creates a new presenter, and passes itself as the presenter's IView. This way for testing you can pass in a fake IView instead, and then send commands to it from the presenter and detect the results.

If I were to use a full-fledged Model-View-Controller, I guess I'd do it this way:

  • The form is the view. It sends commands to the model, raises events which the controller can subscribe to, and subscribes to events from the model.
  • The controller is a class that subscribes to the view's events and sends commands to the view and to the model.
  • The model raises events that the view subscribes to.

This would fit with the classic MVC diagram. The biggest disadvantage is that with events, it can be hard to tell who's subscribing to what. The MVP pattern uses methods instead of events (at least the way I've implemented it). When the form/view raises an event (e.g. someButton.Click), the form simply calls a method on the presenter to run the logic for it. The view and model don't have any direct connection at all; they both have to go through the presenter.

Solution 2 - Winforms

Well, actually Windows Forms implements a "free-style" version of MVC, much like some movies implement some crappy "free-style" interpretation of some classic books (Romeo & Juliet come to mind).

I'm not saying Windows Forms' implementation is bad, it's just... different.

If you use Windows Forms and proper OOP techniques, and maybe an ORM like EntitySpaces for your database access, then you could say that:

  1. The ORM/OOP infrastructure is the Model
  2. The Forms are the Views
  3. The event handlers are the Controller

Although having both View and Controller represented by the same object make separating code from representation way more difficult (there's no easy way to plug-in a "GTK+ view" in a class derived from Microsoft.Windows.Forms.Form).

What you can do, if you are careful enough. Is keep your form code completely separate from your controller/model code by only writing GUI related stuff in the event handlers, and all other business logic in a separate class. In that case, if you ever wanted to use GTK+ to write another View layer, you would only need to rewrite the GUI code.

Solution 3 - Winforms

Windows Forms isn't designed from the ground up to use MVC. You have two options.

First, you can roll your own implementation of MVC.

Second, you can use an MVC framework designed for Windows Forms.

The first is simple to start doing, but the further in you get, the more complex it is. I'd suggest looking for a good, preexisting and well-tested, MVC framework designed to work with Windows Forms. I believe this blog post is a decent starting point.

For anybody starting out, I'd suggest skipping Windows Forms and developing against WPF, if you have the option. It's a much better framework for creating the UI. There are many MVC frameworks being developed for WPF, including this one and that one.

Solution 4 - Winforms

According to Microsoft, the UIP Application Block mentioned by @jasonbunting is "archived." Instead, look at the Smart Client Application Block or the even newer Smart Client Software Factory, which supports both WinForms and WPF SmartParts.

Solution 5 - Winforms

Check into the User Interface Process (UIP) Application Block. I don't know much about it but looked at it a few years ago. There may be newer versions, check around.

"The UIP Application Block is based on the model-view-controller (MVC) pattern."

Solution 6 - Winforms

Take a look at the MS Patterns and Practices Smart Client application block which has some guidance and classes which walk you through implementing a model view presenter patter in windows forms - take a look at the reference application included.

For WPF this is being superseced by the prism project

The software factories approach is a great way to learn best practices

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris PietschmannView Question on Stackoverflow
Solution 1 - WinformsRyan LundyView Answer on Stackoverflow
Solution 2 - WinformsdguaragliaView Answer on Stackoverflow
Solution 3 - Winformsuser1228View Answer on Stackoverflow
Solution 4 - WinformsaridlehooverView Answer on Stackoverflow
Solution 5 - WinformsJason BuntingView Answer on Stackoverflow
Solution 6 - WinformsRichardView Answer on Stackoverflow