【问题标题】:Application(solution) structure for Model View Presenter(+ Passive View) in WinForms?WinForms中模型视图演示器(+被动视图)的应用程序(解决方案)结构?
【发布时间】:2013-05-07 01:07:49
【问题描述】:

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

在阅读了将表示与业务逻辑和数据库访问/持久性分开的更好方法之后,我想到了第一个重大变化。然后,我可以为我的子表单调用演示者,比如说“MainForm”。

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

另外,我应该只使用一个“MainPresenter”吗?或者最好为我将使用的每个子表单设置一个,例如:

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

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

提前致谢,

【问题讨论】:

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


    【解决方案1】:

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

    Developing WP7 apps using the MVP pattern

    但是为了快速回答你的问题,每个 Form 都应该实现一个 View 接口,每个 Presenter 应该处理 1 个且只有 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
    

    这更像你要求的吗?

    【讨论】:

    • #ZaijiaN,感谢您的回复。我了解 MVP,确实,我已经使用它完成了整个应用程序。我试图找到一种更好的方法来“组织”一个 Windows 窗体解决方案,比如说,分层。主要是应用MVP构建的解决方案
    猜你喜欢
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 2012-07-03
    • 2011-06-09
    • 2014-10-31
    • 2011-02-25
    相关资源
    最近更新 更多