欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

学习ASP.NET MVC框架揭秘笔记-实例演示:SC模式的应用

程序员文章站 2022-07-01 18:38:02
实例演示:sc模式的应用 为了对sc模式下的mvp,尤其是该模式下的view和presenter之间的交互方式有一个深刻的认识,我们现在来做一个实例演示。我们采用员工查询的场景,用asp.net w...

实例演示:sc模式的应用

为了对sc模式下的mvp,尤其是该模式下的view和presenter之间的交互方式有一个深刻的认识,我们现在来做一个实例演示。我们采用员工查询的场景,用asp.net web forms来建立这个简单的应用。

我们先来定义员工的数据类型,employee来表示一个员工,有5个属性:id、姓名、性别、出生日期和部门。

 

public class employee
    {
        public string id { get; private set; }
        public string name { get; private set; }
        public string gender { get; private set; }
        public datetime birthdate { get; private set; }
        public string department { get; private set; }

        public employee(string id, string name, string gender,datetime birthdate, string department)
        {
            this.id = id;
            this.name = name;
            this.gender = gender;
            this.birthdate = birthdate;
            this.department = department;
        }

 

作为包含应用状态和状态操作行为的model,通过如下一个简单的employeerepository类型来体现。表示所有员工列表的数据通过一个静态字段来维护,而getemployees方法返回指定部门的员工列表。如果没有指定筛选部门或者指定的部门字符为空,该方法直接返回所有员工列表。

 

public class employeerepository
    {
        private static ilist employees;
        static employeerepository()
        {
            employees = new list();
            employees.add(new employee("001", "张三", "男",new datetime(1981, 8, 24), "销售部"));
            employees.add(new employee("002", "李四", "女",new datetime(1982, 7, 10), "人事部"));
            employees.add(new employee("003", "王五", "男",new datetime(1981, 9, 21), "人事部"));
        }

        public ienumerable getemployees(string department = "")
        {
            if (string.isnullorempty(department))
            {
                return employees;
            }
            return employees.where(e => e.department == department).toarray();
        }
    }

 

接下来我们看看作为view接口的iemployeeview的定义。该接口定义了bindemployees和binddepartments两个方法,分别用于绑定基于部门列表的dropdownlist和基于员工列表的gridview。除此之外,iemployeeview接口还定义了一个事件departmentselected,该事件会在用户选择了筛选部门之后单机查询按钮时触发。departmentselected事件参数类型为自定义的departmentselectedeventages,属性department表示用户选择的部门。

public interface iemployeeview
    {
        void bindemployees(ienumerable employees);
        void binddepartments(ienumerable departments);
        event eventhandler departmentselected;
    }
public class departmentselectedeventargs : eventargs
    {
        public string department { get; private set; }
        public departmentselectedeventargs(string department)
        {
            this.department = department;
        }
    }

作为mvp三角关系核心的presenter通过employeepresenter表示。里面包含两个属性,一个代表view的只读属性iemployeeview接口 的view,而另一个代表model的只读属性employeerepository对象的repository,两个属性均在构造函数中初始化。

 

public class employeepresenter
    {
        public iemployeeview view { get; private set; }
        public employeerepository repository { get; private set; }

        public employeepresenter(iemployeeview view)
        {
            this.view = view;
            this.repository = new employeerepository();
            this.view.departmentselected += ondepartmentselected;
        }

        public void initialize()
        {
            ienumerable employees = this.repository.getemployees();
            this.view.bindemployees(employees);
            string[] departments = new string[] { "", "销售部", "采购部", "人事部", "it部" };
            this.view.binddepartments(departments);
        }

        protected void ondepartmentselected(object sender,  departmentselectedeventargs args)
        {
            string department = args.department;
            var employees = this.repository.getemployees(department);
            this.view.bindemployees(employees);
        }
    }

 

我们在构造函数中注册了view的departmentselected事件,作为事件处理器的ondepartmentselected方法通过调用repository(即model)得到了用户选择部门下的员工列表,并且绑定到了view上。

下面我们来看看view的web页面。





:dropdownlist cssclass="form-inline" id="dropdownlistdepartments" runat="server">

下面是web页面的后台代码定义,他实现了定义在iemployeeview接口的方法和事件。

public partial class default : system.web.ui.page, iemployeeview
    {
        public employeepresenter presenter { get; private set; }
        public event eventhandler departmentselected;

        public default()
        {
            this.presenter = new employeepresenter(this);
        }

        protected void page_load(object sender, eventargs e)
        {
            if (!this.ispostback)
            {
                this.presenter.initialize();
            }
        }

        protected void buttonsearch_click(object sender, eventargs e)
        {
            string department = this.dropdownlistdepartments.selectedvalue;
            departmentselectedeventargs eventargs = new departmentselectedeventargs(department);
            if (null != departmentselected)
            {
                departmentselected(this, eventargs);
            }
        }

        public void bindemployees(ienumerable employees)
        {
            this.gridviewemployees.datasource = employees;
            this.gridviewemployees.databind();
        }


        public void binddepartments(ienumerable departments)
        {
            this.dropdownlistdepartments.datasource = departments;
            this.dropdownlistdepartments.databind();
        }
    }