Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you implement MVC in a Windows Forms application?

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?

like image 253
Chris Pietschmann Avatar asked Sep 23 '08 17:09

Chris Pietschmann


People also ask

Can we use MVC in Windows application?

MVC is about User Empowerment and Direct Manipulation. It was invented for (the very first) graphical desktops. You can most certainly use it to build Windows applications.

What is MVC implementation process?

-MVC is an architectural pattern consisting of three parts: Model, View, Controller. Model: Handles data logic. View: It displays the information from the model to the user. Controller: It controls the data flow into a model object and updates the view whenever data changes. -It is invented by Trygve Reenskau.

How do I deploy Windows Forms application?

Right click on Project menu and click on "Project name Properties". Click the sign in option and select the checkbox (Sign the Click Once manifests). Go to Security >> select Check Box (Enable Click Once Security Settings). Go to Publish >> select Publishing folder location path and Save.


2 Answers

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.

like image 168
Ryan Lundy Avatar answered Oct 01 '22 00:10

Ryan Lundy


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.

like image 45
dguaraglia Avatar answered Oct 01 '22 01:10

dguaraglia