简体   繁体   English

在WinForms中实现干净的UI功能同时保持良好的解耦架构的最佳方法是什么?

[英]What's the best way to implement clean UI functionality in WinForms while maintaining a decent decoupled architecture?

I tend to implement UI functionality using fairly self-documenting void doSomething() methods, ie if the user presses this button then perform this action then enable this list box, disable that button, etc. Is this the best approach? 我倾向于使用相当自我记录的void doSomething()方法来实现UI功能,即如果用户按下此按钮然后执行此操作然后启用此列表框,禁用该按钮等。这是最好的方法吗? Is there a better pattern for general UI management ie how to control when controls are enabled/disabled/etc. 是否有更好的模式用于一般UI管理,即如何控制何时启用/禁用控件等。 etc. depending on user input? 等取决于用户输入?

Often I feel like I'm veering towards the 'big class that does everything' anti-pattern as so much seems to interact with the 'main' form class. 我常常觉得自己正在转向“做一切”反模式的“大班”,因为很多人似乎与“主要”形式的班级互动。 Often, even if I'm including private state variables in the class that have been implemented using a relatively modular design, I'm still finding it grows so quickly it's ridiculous. 通常,即使我在类中包含使用相对模块化设计实现的私有状态变量,我仍然发现它增长得如此之快,这是荒谬的。

So could people give me some good advice towards producing quality, testable, decoupled WinForms design without falling into these traps? 那么人们可以给我一些很好的建议来制作高质量,可测试,分离的WinForms设计而不会陷入这些陷阱吗?

You can try MVP if you want to put the logic of the UI in a separate class.. 如果要将UI的逻辑放在单独的类中,可以尝试MVP

In model view presenter just as Martin Fowler or Michael Feathers say, the logic of the UI is separated into a class called presenter, that handles all the input from the user and that tells the "dumb" view what and when to display. 在模型视图演示者中,正如Martin Fowler或Michael Feathers所说,UI的逻辑被分成一个名为presenter的类,它处理来自用户的所有输入,并告诉“哑”视图显示什么以及何时显示。 The special testability of the pattern comes from the fact that the entire view can be replaced with a mock object and in this way the presenter, which is the most important part, can be easily unit tested in isolation. 模式的特殊可测试性来自于整个视图可以用模拟对象替换的事实,并且以这种方式,作为最重要部分的演示者可以容易地单独地进行单元测试。

using the MVP pattern is pretty good with winforms. 使用MVP模式与winforms相当不错。

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

I would only put UI logic in the Form class and put any application logic in its own class: 我只将UI逻辑放在Form类中,并将任何应用程序逻辑放在自己的类中:

class Form1 : Form
 {  
    void Button1_Click
     { 
       Program.DoCommand1();
     }
 }


static class Program
{
  internal static void DoCommand1() {/* ... */}
}

One thing I have been dong lately is leveraging the partial class feature of .NET for some of these larger type forms. 我最近有一件事就是利用.NET的部分类功能来处理这些较大类型的表单。 If I have a tab control with 5 different tabs on it. 如果我有一个标签控件,上面有5个不同的标签。 I'll create partial classes and name the files CardImportMethods.cs, ManageLookupTables.cs, etc. while leaving it all a part of the CentralizedForm class. 我将创建部分类并将文件命名为CardImportMethods.cs,ManageLookupTables.cs等,同时将它们全部留作CentralizedForm类的一部分。

Even with just the UI logic, having this breakdown has helped when it comes to managing those things. 即使只有UI逻辑,在管理这些事情时,这种细分也有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 扩展现有网站功能并同时维护基础库功能的最佳方法? - Best way to extend existing website functionality while maintaining the functionality of base libraries? 在WPF中维护MySQL连接的最佳方法是什么? - What's the best way for maintaining a MySQL connection in WPF? 实施搜索的最佳方法是什么? - What's the best way to implement a search? 在 Clean Architecture 中为 Autofac 实现 Serilog 上下文记录器注入的正确方法是什么? - What is the proper way to implement Serilog Contextual logger injection for Autofac in Clean Architecture? 解耦架构 - Decoupled architecture 在实体上实施跟踪更改的最佳方法是什么? - What's the best way to implement tracking changes on your entities? 在C#中实现全局常量的最佳方法是什么? - What's the best way to implement a global constant in C#? 实施一维碰撞检测的最佳方法是什么? - What's the best way to implement one-dimensional collision detection? 在C#中实现动态代理的最佳方法是什么? - What's the best way to implement a dynamic proxy in C#? 什么是在C#中实现链式事件的最佳方式 - what's the best way to implement chained events in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM