使用 Windows 窗体实现 MVC [英] Implementing MVC with Windows Forms

查看:36
本文介绍了使用 Windows 窗体实现 MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里可以找到关于如何在 Windows 窗体中完全实现 MVC 模式的好例子?

我在各种网站(例如,The Code Project 和.NetHeaven),但许多比MVC更能代表观察者模式.由于我要开发的应用是针对学校项目的,所以不太愿意使用PureMVCMVC#.

解决方案

我认为应用程序彼此如此不同,我们对应用程序应该如何编写的理解仍然非常有限.我过去使用过的 Windows 窗体应用程序彼此如此不同.我看到的一些设计差异是(包括大多数组合):

  • 直接与数据库对话(2 层)
  • 使用为给定应用程序编写的后端(3 层)
  • 使用一组专为许多应用程序使用而编写且无法为您的应用程序更改的网络服务.(面向服务的架构)
  • CRUD 操作完成的更新
  • 正在使用命令模式(向后端服务器发送命令)进行更新
  • 数据绑定的很多用法/没有数据绑定的用法
  • 大多数数据都是类似表格"的(例如发票),在标准网格控件中运行良好/需要为大多数 UI 数据自定义控件.
  • 一个开发者/一个由 10 或 20 个开发者组成的团队(仅在 UI 上)
  • 使用模拟等进行大量单元测试/无单元测试

因此,我认为不可能创建一种始终适合的 MVC(或 MVP)实现.

我见过的最好的帖子是解释MVC和为什么构建MVC系统的方式是"构建您自己的 CAB" 系列作者:杰里米·D·米勒.在工作之后,您应该能够更好地理解您的选择.Microsoft 的智能客户端指南(CAB/Microsoft 复合应用程序块) 也应予以考虑.它有点复杂,但它可以很好地适用于非常适合的应用程序.

选择Winforms 项目的 MVC/MVP 实现 给出了一个值得阅读的概述.很多人喜欢 PureMVC.我从来没有用过,但下次我需要一个 MVC 框架时我会看看它.

"Presenter First" 是一种结合了 Model View Presenter 思想的软件开发方法(MVP) 设计模式和测试驱动开发.它让您可以从用客户的语言编写测试开始.例如:

<块引用>

当我点击保存"按钮时文件应该被保存并且未保存的文件警告应该消失."

我没有使用Presenter First"的经验,但有机会我会尝试一下,因为它看起来很有前景.

您可能希望查看的其他堆栈溢出问题是此处这里.

如果您想在任何时候使用 WPF,请查看 模型-视图视图模型 (MVVM) 模式.这是一个非常好的视频,您应该看看:Jason Dolinger on Model-View-ViewModel.

MVVM(模型视图视图模型)设计模式Winforms 提供了另一个选项,可以在需要时更轻松地转换为 WPF.Magical.Trevor 是另一个适用于 Windows 窗体的 MVVM 示例,它还包括基于属性名称的自动绑定.><小时>

还要问问自己为什么使用 MVC.

  • 您是否希望能够对尽可能多的代码进行单元测试?
  • 您是否希望尽可能多地重用代码?
  • 您是否试图让您的代码库易于理解?
  • 其他 101 个对给定项目有效的原因.

一旦您明确了目标,就可以更轻松地选择一种或另一种实现方式.

Where can I find a good example on how to completely implement the MVC pattern in Windows Forms?

I found many tutorials and code examples on various sites (for example, The Code Project and .NetHeaven), but many are more representative for the observer pattern than MVC. Since the application I want to develop is for a school project, I am reluctant to using frameworks like PureMVC or MVC#.

解决方案

I am of the view that applications are so different from each other and our understanding of how applications should be written is still very limited. Past Windows Forms applications I have worked on have been so different from each other. Some of the design differences I have seen are (including most combinations):

  • Directly talk to the database (2 tier)
  • Use a backend that has been written for the given application (3 tier)
  • Use a set of web services that were written for use by many applications and can’t be changed for your application. (Service-oriented architecture)
  • Updates being done by CRUD operations
  • Updates being done with the command pattern (sending commands to backend server)
  • Lots of usages of data binding / no usages of data binding
  • Most data being "table like" (e.g. invoices) that work well in standard grid controls / need custom controls for most of the UI data.
  • One developer / teams of 10 or 20 developers (just on the UI)
  • Lots of unit test using mocks etc / no unit tests

Therefore I don’t think it’s possible to create one implementation of MVC (or MVP) that always fits well.

The best posts I have seen really explaining MVC and why an MVC system is built the way it is, is the "Build Your Own CAB" series by Jeremy D Miller. After working though it you should be able to understand your options a lot better. Microsoft's Smart Client Guidance (CAB / Microsoft Composite Application Block) should also be considered. It is a bit complex, but it can work well for applications that have a good fit.

Selecting an MVC/MVP Implementation for a Winforms Project give an overview that is worth reading. A lot of people like PureMVC. I have never used it, but I would look at it the next time I need a MVC framework.

"Presenter First" is a software development approach that combines the ideas of the Model View Presenter (MVP) design pattern and test-driven development. It lets you start off by writing tests in the customer’s language. For example:

"When I click the 'save' button then the file should be saved and the unsaved file warning should disappear."

I have no experience using "Presenter First," but I will give it a try when I get a chance, as it looks very promising.

Other Stack Overflow questions you may may wish to look at are here and here.

If you are thinking of using WPF at any point take a look at the Model-View ViewModel (MVVM) pattern. Here is a very good video you should take a look at: Jason Dolinger on Model-View-ViewModel.

MVVM (Model View View Model) Design Pattern for Winforms give another option that may make it easer to convert to WPF if ever needed. Magical.Trevor is yet another MVVM sample for Windows Forms that also includes auto binding based on property names.


Also ask yourself why you are using MVC.

  • Do you wish to be able to unit test as much code as possible?
  • Are you trying to allow as much code as possible to be reused?
  • Are you trying to make your code base easy to understand?
  • 101 other reasons that can be valid for a given project.

Once you are clear on your aims, it becomes easier to choose one implementation or another.

这篇关于使用 Windows 窗体实现 MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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