2013-05-07 39 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

相關問題