問題描述
協調 MVP 三元組 (Coordinating MVP triads)
假設您的應用程序(WinForms .NET 2.0 應用程序)中有多個 MVP 三元組,每個三元組負責一個責任區域。您首選的協調 MVP 三元組之間通信的方式是什麼?
選項 1 一個協調器對象,它“擁有”每個模型並通過訂閱每個模型中的必要事件來負責協調,然後決定什麼模型在什麼場景下調用方法。
擔心這可能是“神”課。
選項2 一個Presenter“擁有”另一個Presenter,當模型中發生感興趣的事情時,Presenter使用另一個Presenter來推動交流。
擔心演示者不應該有一個公共界面(演示者優先方法),這打破了這一點。< /p>
我只是想知道其他人做了什麼來以可擴展的 OO 方式解決這個問題。如果我添加另一個 MVP 三元組會怎樣?將它融入我的協調器有多難?一定有一些很好的例子說明如何在一個 WinForms 應用中管理多個 MVP 三元組?
參考解法
方法 1:
Not to be vague... but it depends. The two approaches I've used in the past:
- Use an Event Aggregator pattern, and have the individual presenters fire off events that other presenters can handle. e.g. PresenterA does:
events.Raise<MyEvent> ()
and PresenterB implements:IHandler<MyEvent>
and reacts accordingly in itspublic void Handle (MyEvent @event)
method. - Use a shared model that is injected into the presenters that need to be coordinated. For example, if one presenter handles selection of a given Foo, and the other presenter needs to update a details panel with Foo details, I might use an IFooSelection state model and inject that into both presenters that need to coordinate the concept of 'current selection'.
The interface:
public interface IFooSelection {
public event EventHandler Changed;
Foo Selected { get; set; }
}
(by Peter Kelly、Pete)