Wednesday, November 16, 2005

Smart Client Development Part II

In Part I I described the basic set up for creating a smart client application. The next step is to add some data to the UserData object created in the EntryPoint and then display this data in the ReadView User Control.

EntryPoint should be updated by adding some values to UserData and passing the user object to ReadView's presenter.
public class EntryPoint
{
[STAThread]
static void Main()
{
MainForm mainForm = new MainForm();
UserData userData = createUser();
new MainPresenter(mainForm, userData, createViewFactory(userData));
Application.Run(mainForm);
}

private static UserData createUser()
{
UserData user = new UserData();
user.Name = "John Doe";
user.JobTitle = "Rockstar";
user.PhoneNumber = "212-555-1212";
return user;
}

private static ViewFactory createViewFactory(UserData user)
{
return new ViewFactory(getViews(user));
}

private static UserControl[] getViews(UserData user)
{
return new UserControl[] { getReadView(user), getUpdateView(user) };
}

private static UserControl getReadView(UserData user)
{
ReadView view = new ReadView();
new ReadPresenter(view, user);
return view;
}

private static UserControl getUpdateView(UserData user)
{
return new UpdateView();
}
}

ReadView's presenter handles the logic that puts the correct data in the ReadView.
public class ReadPresenter
{
private readonly ReadView view;
private readonly UserData user;

public ReadPresenter(ReadView view, UserData user)
{
this.view = view;
this.user = user;
push();
}

private void push()
{
view.NameLabel.Text = user.Name;
view.JobLabel.Text = user.JobTitle;
view.PhoneLabel.Text = user.PhoneNumber;
}
}

The only other changes that need to be made are making the NameLabel, JobLabel, and PhoneLabel public on the view.
The result is a dumb view and all the behavior is in the Presenter. The view should look similar to this and be working at this point if you click on "View."



In Part III I'll create the presenter for the UpdateView.
Post a Comment