WinForms 应用程序的架构? [英] Architecture for WinForms applications?

查看:35
本文介绍了WinForms 应用程序的架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几周前我开始了一个 WinForms 项目,因为我并不真正知道我想要什么功能,所以我只是在此过程中添加了它们.这现在造成了可怕的混乱,我的 MainForm 是一个大泥球,例如一些重要的状态更改由 UI 元素触发,以至于我必须调用控件的 OnChange 事件以更改数据库中的某些状态.

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.

简而言之:我刚刚开始了一个新项目,我想在其中采用更好的方法.我只是不知道哪个是好"的.在 ASP.net MVC 中,我发现 MVVM 模式非常有用,但在桌面上,MVVM 似乎只适用于 WPF,而不适用于 WinForms.

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.

另一种方法是三层架构:我的数据库类目前直接与 UI 对话.我现在创建了一个新的静态类(ApplicationState"),它与数据库对话并触发事件来告诉 UI嘿,有些东西改变了!".UI 将操纵状态,然后状态将处理数据库持久性并在 UI 需要更新时再次引发事件.这里的重点是 ApplicationState 类从不直接修改 UI,而是 UI 订阅事件.这看起来是一种干净/MVC-y"的方式,但也许我在这里忽略了一些东西?

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?

基本上我的最终目标是让 UI 完全独立于数据库层,以确保我不会再次将业务逻辑连接到 UI.

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.

推荐答案

不要放弃 MVVM - 它也适用于 WinForms.基本上,如果您使用数据绑定,您必须决定您的对象将绑定到什么.通常,尤其是对于更复杂的 UI,您不想直接绑定到域对象,而是希望构建专门的类(有时是包装器),您的 UI 可以绑定到它来提供视图所需的一切(MVVM 的本质)和技术与 Winforms 一起工作.

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.

关于 WinForms Model-View-Presenter 方法的一个很好的系列可以在

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

构建您自己的 CAB 系列目录

这篇关于WinForms 应用程序的架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆