What is the best way to implement the pure user interface functionality in WinForms while maintaining a decent decoupled architecture?

I tend to implement user interface functionality using the rather self-documented void doSomething () methods, that is, if the user clicks this button, then perform this action, then enable this list, disable this button, etc. Is this a better approach? Is there a better template for general user interface control, i.e. how to control when the controls are on / off / etc. etc. depending on user input?

Often it seems to me that I'm leaning towards the โ€œbig class that does everythingโ€ anti-pattern, as it seems to interact with the โ€œmainโ€ class of the form. Often, even if I include private state variables in a class that were implemented using a relatively modular design, I still find that it grows so fast that it's ridiculous.

So can people give me good advice on creating a high-quality, testable, untied WinForms design without falling into these traps?

+4
source share
4 answers

You can try MVP if you want to put the interface logic in a separate class.

In the model view, just like Martin Fowler or Michael Perce say, the user interface logic is divided into a class called the host, which processes all the user input and which tells the stupid views what and when to show. Special template validation comes from the fact that the whole view can be replaced by a mock object, and thus the lead, which is the most important part, can be easily tested individually.

+12
source

using the MVP pattern is pretty good with winforms.

take a look at http://www.objectmentor.com/resources/articles/TheHumbleDialogBox.pdf

+3
source

I would put the UI logic in the Form class and put any application logic in my class:

class Form1 : Form { void Button1_Click { Program.DoCommand1(); } } static class Program { internal static void DoCommand1() {/* ... */} } 
0
source

One thing I got recently is the use of a partial function of the .NET class for some of these larger forms. If I have a tab control with 5 different tabs. I will create partial classes and name the files CardImportMethods.cs, ManageLookupTables.cs etc., leaving all this part of the CentralizedForm class.

Even with the user interface logic, when this splitting helped, when it came to managing these things.

0
source

Source: https://habr.com/ru/post/1277094/


All Articles