作为包含应用状态和状态操作行为的Model通过如下一个简单的EmployeeRepository类型来体现如代码所示表示所有员工列表的数据通过一个静态字段来维护而GetEmployees返回指定部门的员工列表如果没有指定筛选部门或者指定的部门字符为空则直接返回所有的员工列表
public class EmployeeRepository
{
private static IList<Employee> employees;
static EmployeeRepository()
{
employees = new List<Employee>()
employeesAdd(new Employee( 张三 男
new DateTime( ) 销售部))
employeesAdd(new Employee( 李四 女
new DateTime( ) 人事部))
employeesAdd(new Employee( 王五 男
new DateTime( ) 人事部))
}
public IEnumerable<Employee> GetEmployees(string department = )
{
if (stringIsNullOrEmpty(department))
{
return employees;
}
return employeesWhere(e => eDepartment == department)ToArray()
}
}
接下来我们来看作为View接口的IEmployeeSearchView的定义如下面的代码片段所示该接口定义了BindEmployees和BindDepartments两个方法分别用于绑定基于部门列表的DropDownList和基于员工列表的GridView除此之外IEmployeeSearchView接口还定义了一个事件DepartmentSelected该事件会在用户选择了筛选部门后点击查询按钮时触发DepartmentSelected事件参数类型为自定义的DepartmentSelectedEventArgs属性Department表示用户选择的部门
public interface IEmployeeSearchView
{
void BindEmployees(IEnumerable<Employee> employees)
void BindDepartments(IEnumerable<string> departments)
event EventHandler<DepartmentSelectedEventArgs> DepartmentSelected;
}
public class DepartmentSelectedEventArgs : EventArgs
{
public string Department { get; private set; }
public DepartmentSelectedEventArgs(string department)
{
thisDepartment = department;
}
}
返回目录ASPNET MVC 框架揭秘
编辑推荐
Java程序性能优化让你的Java程序更快更稳定
Visual C++音频/视频技术开发与实战
[] []