MVP()
作为MVP三角关系核心的Presenter通过EmployeeSearchPresenter表示如下面的代码片段所示表示View的只读属性类型为IEmployeeSearchView接口而另一个只读属性Repository则表示作为Model的EmployeeRepository对象两个属性均在构造函数中初始化
public class EmployeeSearchPresenter
{
public IemployeeSearchView View { get; private set; }
public EmployeeRepository Repository { get; private set; }
public EmployeeSearchPresenter(IEmployeeSearchView view)
{
thisView = view;
thisRepository = new EmployeeRepository()
thisViewDepartmentSelected += OnDepartmentSelected;
}
public void Initialize()
{
IEnumerable<Employee> employees = thisRepositoryGetEmployees()
thisViewBindEmployees(employees)
string[] departments =
new string[] { 销售部 采购部 人事部 IT部 };
thisViewBindDepartments(departments)
}
protected void OnDepartmentSelected(object sender
DepartmentSelectedEventArgs args)
{
string department = argsDepartment;
var employees = thisRepositoryGetEmployees(department)
thisViewBindEmployees(employees)
}
}
在构造函数中我们注册了View的DepartmentSelected事件作为事件处理器的OnDepartmentSelected方法通过调用Repository(即Model)得到了用户选择部门下的员工列表返回的员工列表通过调用View的BindEmployees方法实现了在View上的数据绑定在Initialize方法中我们通过调用Repository获取所有员工的列表并通过View的BindEmployees方法显示在界面上作为筛选条件的部门列表通过调用View的BindDepartments方法绑定在View上
最后我们来看看作为View的Web页面如何定义如下所示的是作为页面主体部分的HTML核心部分是一个用于绑定筛选部门列表的DropDownList和一个绑定员工列表的GridView
<html xmlns=http://wwwworg//xhtml>
<head>
<title>员工管理</title>
<link rel=stylesheet href=Stylecss />
</head>
<body>
<form id=form runat=server>
<div id=page>
<div class=top>
选择查询部门
<asp:DropDownList ID=DropDownListDepartments
runat=server />
<asp:Button ID=ButtonSearch runat=server Text=查询
OnClick=ButtonSearch_Click />
</div>
<asp:GridView ID=GridViewEmployees runat=server
AutoGenerateColumns=false Width=%>
[] []