WinForms中模型视图呈现器(+被动视图)的应用(解决方案)结构

本文关键字:视图 应用 解决方案 结构 被动 WinForms 模型 | 更新日期: 2023-09-27 18:12:18

有一次我写了一段代码,随着时间的推移,它开始发臭。它没有以一种容易测试的方式进行编码。每个子窗口都与以数据库为中心的Microsoft控件(如BindingNavigator等)紧密耦合。但有一天,我厌倦了自己的代码,因为它不可重用、不可测试、不可理解(即使是我自己)。

在阅读了将表示从业务逻辑和数据库访问/持久化中分离出来的更好方法之后,我提出了第一个大的改变。然后,我就可以为我的"MainForm"的子窗体调用演示器了。

现在,我有一堆演示器、视图、存储库、模型和接口,我想把它们组织在一个"标准"的项目结构中(即像业务、模型、UI、测试这样的项目)。有人能公开这样的结构吗?如果可能的话,还能提供每个结构中使用的示例文件夹吗?

另外,我应该使用一个"MainPresenter"吗?还是我将使用的每个子窗体都有一个更好,例如:

var searchReceivalPresenter = new SearchReceivalsPresenter(
       new SearchReceivalsForm { MdiParent = this }, new SearchReceivalsRepository());

我认为我应该保留几个演讲者。

提前致谢

WinForms中模型视图呈现器(+被动视图)的应用(解决方案)结构

我认为这里可能有一些关于MVP的误解-我写了一篇关于如何在Windows Phone 7上实现MVP的文章,但我涵盖了MVP的基础知识,你应该能够理解一般理论,然后将其应用于WinForms:

使用MVP模式开发WP7应用

但是为了快速回答你的问题,每个Form应该实现一个View接口,每个Presenter应该处理一个且只有一个View接口。

当你想打开子窗体时,WinForms就变得棘手了。我最后做的是让父演示器直接调用子演示器上的Show功能。然后,子呈现器将使用依赖注入来实例化相关视图接口的实现。

UPDATE (因为我没有完全回答这个问题):)

让我描述一个项目结构,我已经用于winforms/MVP应用程序:

/ - solution root
/[App]Core - project that contains the Model - pure business logic
/[App]Core/Model - data model (lowercase "m"), POCOs
/[App]Core/Daos - data access layer (all interfaces)
/[App]Core/Services - business logic classes (interfaces and implementations)
/[App]Ui - project that contains all UI-related code, but is UI-agnostic
/[App]Ui/Model - contains POCOs that are used by the UI but not the Core
/[App]Ui/Presenters - contains presenters
/[App]Ui/Views - contains view interfaces
/[App][Platform] - project that contains all UI-specific code (e.g. WinRT, WinPhone, WinForms, WPF, Silverlight, etc)
/[App][Platform]/Daos - implementation of DAO interfaces defined in [App]Core
/[App][Platform]/Services - implementation of business logic interfaces defined in [App]Core
/[App][Platform]/Views - implementation of view interfaces defined in [App]Ui

这更像你想要的吗?