Wednesday, November 16, 2005

Smart Client Development Part IV

In Part III I completed the UpdatePresenter and had a working demo application. Unfortunately, you may have noticed that in MainPresenter the Navigate of each view is subscribed to each time it is returned from the PresenterFactory. This causes the MainPresenter to reload the View as many times as it has been returned from the PresenterFactory. To solve this issue you can add another class called NavigationDispatcher.
public class NavigationDispatcher
{
public static void RaiseNavigate(Type type)
{
Navigate(type);
}

public static event NavigateHandler Navigate;
}

NavigationDispatcher allows you to raise the Navigate event from both the MainForm and the UpdateView. The UpdateView button click event changes to:
private void saveButton_Click(object sender, EventArgs e)
{
Save();
NavigationDispatcher.RaiseNavigate(typeof(ReadView));
}

The only other change to make is in MainPresenter.
public class MainPresenter
{
private readonly MainForm form;
private PresenterFactory presenterFactory;

public MainPresenter(MainForm form, PresenterFactory presenterFactory)
{
this.form = form;
NavigationDispatcher.Navigate+=new NavigateHandler(Navigate);
this.presenterFactory = presenterFactory;
}

private void Navigate(System.Type viewType)
{
form.ContentPanel.Controls.Clear();
IView view = presenterFactory.Find(viewType);
form.ContentPanel.Controls.Add((Control) view);
}
}

Subscription to the NavigationDispatcher's Navigate event occurs in the constructor; however, the subscription is no longer necessary in the Navigate event (where we used to subscribe to the Navigate event of the View). Obviously, the Navigate event can be removed from the IView at this point also.
Post a Comment