我会让你的新主持人拿在副主持人构造函数的参数,是这样的:
class DialogPresenter {
private readonly IDialogView view;
private readonly PersonalInformationPresenter personal;
private readonly FriendsPresenter friends;
private readonly EmploymentHistoryPresenter history;
void DialogPresenter(IDialogView view, PersonalInformationPresenter personal, FriendsPresenter friends, EmploymentHistoryPresenter history) {
this.view = view;
this.personal = personal;
this.friends = friends;
this.history = history;
}
bool Display() {
this.personal.Display();
this.friends.Display();
this.history.Display();
return this.view.Display() == DialogResult.Ok;
}
void Save() {
this.personal.Save();
this.friends.Save();
this.history.Save();
}
}
当然如果您的主持人有他们之间的通用接口,这可以简化(并使其更加可扩展),像这样:
class DialogPresenter {
private readonly IDialogView view;
private readonly IPresenters[] presenters;
void DialogPresenter(IDialogView view, IPresenters[] presenters)
{
this.view = view;
this.presenters = presenters;
}
bool Display() {
foreach (var item in this.presenters)
item.Display();
return this.view.Display() == DialogResult.Ok;
}
void Save() {
var validation = new List<string>();
foreach (var item in this.presenters)
validation.AddRange(item.Validate());
if (validation.Count > 0) {
_view.ShowErrors(validation);
return;
}
foreach (var item in this.presenters)
validation.AddRange(item.Save());
}
}
编辑: 调用代码将是someth荷兰国际集团这样的:
void DisplayForm() {
using (var frm = new frmDisplay) {
//or just use DI to get the models etc
var personal = new PersonalInformationPresenter(personalModel, frm.PersonalTab); //some properties to expose your views
var friends = new FriendsPresenter(friendslModel, frm.FriendsTab);
var history = new EmploymentHistoryPresenter(employmentHistoryModel, frm.HistoryTab);
var presenter = new DialogPresenter(frm, personal, friends, history);
if (presenter.Display()) {
presenter.Save();
}
}
}
希望这是一些inpsiration /帮助:)
这确实提供了一些启发。但是,我有以下问题。 每个演示者的“保存”方法在保存之前进行数据验证,万一它失败*调用view.DisplayValidationErrors()*并显示msgbox(“邮政编码丢失”等) 如果我选择使用代码你建议用户将得到一个msg.box的第一个选项卡中的验证错误,一个msg.box的第二个选项卡中的验证错误..但我想所有的验证错误汇总在一个msg.box。 – MadSeb 2010-08-05 19:16:27
我已经更新了我的答案(第二个代码块)以显示我如何处理这个问题 – Pondidum 2010-08-12 06:40:35