Adapted from code supporting article at http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/default.aspx
public partial class ViewCustomers : System.Web.UI.Page, IViewCustomerView
{
private ViewCustomerPresenter presenter;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
presenter = new ViewCustomerPresenter(this);
this.customerDropDownList.SelectedIndexChanged += delegate
{
presenter.DisplayCustomerDetails();
};
}
// ...
}
public class ViewCustomerPresenter
{
private readonly IViewCustomerView view;
private readonly ICustomerTask task;
public ViewCustomerPresenter(IViewCustomerView view) : this(view, new CustomerTask()) {}
public ViewCustomerPresenter(IViewCustomerView view, ICustomerTask task)
{
this.view = view;
this.task = task;
}
public void DisplayCustomerDetails()
{
int? customerId = SelectedCustomerId;
if (customerId.HasValue)
{
CustomerDTO customer = task.GetDetailsForCustomer(customerId.Value);
UpdateViewFrom(customer);
}
}
private void UpdateViewFrom(CustomerDTO customer)
{
view.CompanyName = customer.CompanyName;
view.ContactName = customer.ContactName;
view.ContactTitle = customer.ContactTitle;
view.Address = customer.Address;
view.City = customer.City;
view.Region = customer.Region;
view.Country = customer.CountryOfResidence.Name;
view.Phone = customer.Phone;
view.Fax = customer.Fax;
view.PostalCode = customer.PostalCode;
}
// ...
}
public class CustomerTask : ICustomerTask
{
...
}