Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Architecture for WinForms applications?

I have started a WinForms project a few weeks ago and as I did not really know what features I wanted, I just added them along the way. This now caused a horrible mess where my MainForm is a big ball of mud and where for example some important state changes are triggered by UI elements to the point I have to call the OnChange Event of a Control in order to change some state in the database.

In short: I've just started a new project where I want to take a better approach. I just don't know which would be the "good" one. In ASP.net MVC, I found the MVVM Pattern really useful, but on the Desktop, MVVM seems to be only intended for WPF, not for WinForms.

The other approach is a three-tier-architecture: I have my Database-Class which currently talks directly to the UI. I now create a new Static Class ("ApplicationState") that talks to the database and fires events to tell the UI "Hey, Something changed!". The UI would manipulate the State which will then handle the database persistence and again raise Events if the UI needs updating. The point here is that the ApplicationState class never modifies the UI directly, but that the UI subscribes to Events. That looks like a clean/"MVC-y" way of doing it, but maybe I am overlooking something here?

Basically my ultimate goal would be to have the UI completely independent from the database layer just to make sure I don't wire in business logic into the UI again.

like image 301
Michael Stum Avatar asked Aug 24 '09 20:08

Michael Stum


People also ask

Is WinForms dying?

Win Form has been used to develop many applications. Because of its high age (born in 2003), WinForm was officially declared dead by Microsoft in 2014. However, Win Form is still alive and well.

How are Windows Forms applications developed?

You ordinarily build Windows Forms apps by adding controls to forms and developing responses to user actions, such as mouse clicks or key presses. A control is a discrete UI element that displays data or accepts data input. When a user does something to your form or one of its controls, the action generates an event.

Will WinForms be deprecated?

WinForms won't be deprecated until Win32 is ... which could be quite sometime! WPF on the other hand has few direct dependencies on Win32 so could potentially form the basis of a "fresh start" UI layer on a future version of windows.


2 Answers

Don't throw in the towel on MVVM - it's valid for WinForms as well. Basically, if you use data-binding, you have to make a decision about what your objects will bind to. Often, especially for more complex UI, you dont want to bind directly to your domain objects, you want to build specialized classes (sometimes wrappers) that your UI can bind to which provide everything the view needs (the essence of MVVM) and the technique works just as well with Winforms.

A good series on WinForms Model-View-Presenter approach can be found at

The Build Your Own CAB Series Table of Contents

like image 131
HokieMike Avatar answered Sep 18 '22 16:09

HokieMike


What I would always go for (first) is to have a layered application

  • Presentation Layer (JUST UI and databinding logic)
  • Interface Layer to the Business Layer (defining the contracts for accessing the BL)
  • Business Layer implementation (the actual logic, data validation etc...)
  • Interface Layer to the Data Access Layer (defining the contracts for accessing the DAL)
  • Data Access Layer implementation

This organizes your application extremely well. Then I'd look for some MVC kind approach. I did not develop so much with WinForms, more with Asp.net and some Java Desktop clients (where I used MVC). WinForms works more with the .Net databinding approach (DataSource, DataMember,...). You should go for that approach instead of trying to force something other. I found that it doesn't match that well.

What's always useful is to lay out our UI logic into different controls (like UserControls in Asp.net). This facilitates reuse.

like image 44
Juri Avatar answered Sep 22 '22 16:09

Juri