tags:

views:

5156

answers:

6

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?

+6  A: 

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.

dguaraglia
+1  A: 

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."

Jason Bunting
+6  A: 

WinForms 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 WinForms.

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. Edit: looks like the blog got hacked. Here's the cached version

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

Will
Watch out - the blog link triggered my antivirus (ESET Nod32). It detected the threat "HTML/ScrInject.B.Gen virus" coming from the URL vancouvererrorsonfile.com/js2.php through the blog page.
Pat
@Pat thanks. changed the link to point to the google cache version.
Will
+8  A: 

What I've done in the past is use something similar, Model-View-Presenter. 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.

Kyralessa
Gishu
+1  A: 

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

Richard
http://en.wikipedia.org/wiki/Software_factory - not sure what part @Richard is emphasizing in this answer, though.
Pat
+3  A: 

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.

Alan Ridlehoover