2013-05-07 45 views
0

曾几何时,我写了一段代码,随着时间的流逝,它开始闻起来。它的编码方式不容易测试。每个儿童窗口与数据库中心的Microsoft控件(如BindingNavigator等)紧密耦合。但是当它厌倦了我自己的代码时,它就变成了不可重用,可测试或可以理解的(甚至是我自己)。WinForms中Model View Presenter(+ Passive View)的应用程序(解决方案)结构?

在阅读了将业务逻辑和数据库访问/持久性分开的更好方法之后,我提出了第一个重大改变。然后,我可以打电话给我的孩子们,比方说,“MainForm”的主持人。

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

此外,我应该只使用一个“MainPresenter”?或者是更好地为每个孩子的形式,我会使用,例如:

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

在我看来,我应该保留几个主持人。

由于提前,

回答

1

我觉得有可能是MVP的一些误解,在这里 - 我写了一篇关于如何做MVP随着Windows Phone 7的文章,但我涵盖MVP的基础知识,你应该能够要了解的一般理论,然后将其应用到的WinForms:

Developing WP7 apps using the MVP pattern

但快速回答你的问题,每一个表格必须实现View接口,并且每一个主持人应该处理且只有1个视图界面。

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

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 

这更像你所要求的吗?

+0

#ZaijiaN,谢谢你的回复。我理解MVP,的确,我已经完成了使用它的整个应用程序。我试图找到的是一种更好的方法来“组织”一个Windows窗体解决方案,比如说,在层中。主要建立应用MVP的解决方案 – 2013-05-08 01:14:41