Adapted from code supporting article at http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/default.aspx
1
2 public partial class ViewCustomers : System.Web.UI.Page, IViewCustomerView
3 {
4 private ViewCustomerPresenter presenter;
5
6 protected override void OnInit(EventArgs e)
7 {
8 base.OnInit(e);
9 presenter = new ViewCustomerPresenter(this);
10 this.customerDropDownList.SelectedIndexChanged += delegate
11 {
12 presenter.DisplayCustomerDetails();
13 };
14 }
15
16 // ...
17 }
18
19 public class ViewCustomerPresenter
20 {
21 private readonly IViewCustomerView view;
22 private readonly ICustomerTask task;
23
24 public ViewCustomerPresenter(IViewCustomerView view) : this(view, new CustomerTask()) {}
25
26 public ViewCustomerPresenter(IViewCustomerView view, ICustomerTask task)
27 {
28 this.view = view;
29 this.task = task;
30 }
31
32 public void DisplayCustomerDetails()
33 {
34 int? customerId = SelectedCustomerId;
35
36 if (customerId.HasValue)
37 {
38 CustomerDTO customer = task.GetDetailsForCustomer(customerId.Value);
39 UpdateViewFrom(customer);
40 }
41 }
42
43 private void UpdateViewFrom(CustomerDTO customer)
44 {
45 view.CompanyName = customer.CompanyName;
46 view.ContactName = customer.ContactName;
47 view.ContactTitle = customer.ContactTitle;
48 view.Address = customer.Address;
49 view.City = customer.City;
50 view.Region = customer.Region;
51 view.Country = customer.CountryOfResidence.Name;
52 view.Phone = customer.Phone;
53 view.Fax = customer.Fax;
54 view.PostalCode = customer.PostalCode;
55 }
56
57
58
59 // ...
60 }
61
62 public class CustomerTask : ICustomerTask
63 {
64 ...
65 }