WinForms中模型视图展示器(+被动视图)的应用程序(解决方案)结构?

时间:2013-05-07 01:07:50

标签: c# .net winforms design-patterns mvp

曾几何时,我写了一段代码,随着时间的推移,它开始闻起来。它没有以易于测试的方式编码。每个子窗口都有紧密耦合,以数据库为中心的Microsoft控件(如BindingNavigator等)。但是当我厌倦了我自己的代码的那一天,因为它不可重用,可测试或可理解(甚至是我自己)。

在阅读了从业务逻辑和数据库访问/持久性中分离表示的更好方法之后,我想出了第一个重大变化。然后,我可以给我的孩子形式打电话给主持人,让我们说,“MainForm”。

如今,我有一堆演示者,视图,存储库,模型和界面,我想在“标准”项目结构中组织(即商业,模型,UI,测试等项目)。有人可以公开这样的结构,并且如果可能的话,他们在每个结构中都使用了示例文件夹吗?

另外,我应该只使用一个“MainPresenter”吗?或者为每个我将使用的儿童形式设置一个更好,例如:

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

在我看来,我应该留下几位主持人。

提前致谢,

1 个答案:

答案 0 :(得分:1)

我认为这里可能存在一些对MVP的误解 - 我写了一篇关于如何使用Windows Phone 7进行MVP的文章,但我介绍了MVP的基础知识,你应该能够理解一般理论然后应用它到WinForms:

Developing WP7 apps using the MVP pattern

但是要快速回答您的问题,每个表单都应该实现一个View界面,每个Presenter应该只处理1个View界面。

使用WinForms变得棘手的是当你想要打开子表格时。我最终做的是让父Presenter直接在子Presenter上调用Show方法。然后,子Presenter将使用依赖注入来实例化相关View接口的实现。

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

让我描述一下我用于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

这更像是你要求的吗?