zoukankan      html  css  js  c++  java
  • MVP

    MVP是一种广泛使用的UI架构模式,适用于基于事件驱动的应用框架。

    能够与 Model直接进行交互的仅限于 Presenter, View 只能通过 Presenter 间拉地调用 Model。

    MVP分PV(Passive View) 和 (Supervising Controller)两种模式

    SC(Supervising Controller)

    public partial class WebForm1 : System.Web.UI.Page,IEmployeeSearchView
        {
            public EmployeeSearchPresenter Presenter { get; private set; }
            public event EventHandler<DepartmentSelectedEventArgs> DepartmentSelected;
    
            public WebForm1()
            {
                this.Presenter = new EmployeeSearchPresenter(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<Employee> employees)
            {
                this.GridViewEmployees.DataSource = employees;
                this.GridViewEmployees.DataBind();
            }
    
            public void BindDepartments(IEnumerable<string> departments)
            {
                this.DropDownListDepartments.DataSource = departments;
                this.DropDownListDepartments.DataBind();
            }
    
        }
    
        public class EmployeeSearchPresenter
        {
            public IEmployeeSearchView View { get; private set; }
            public EmployeeRepository Repository { get; private set; }
    
            public EmployeeSearchPresenter(IEmployeeSearchView view)
            {
                this.View = view;
                this.Repository = new EmployeeRepository();
                this.View.DepartmentSelected += OnDepartmentSelected;
            }
    
            private void OnDepartmentSelected(object sender, DepartmentSelectedEventArgs e)
            {
                string department = e.Department;
                var employees = this.Repository.GetEmployees(department);
                this.View.BindEmployees(employees);
            }
    
            public void Initialize()
            {
                IEnumerable<Employee> employees = this.Repository.GetEmployees();
                this.View.BindEmployees(employees);
                string[] departments = new string[] { "销售部", "采购部", "人事部", "IT部" };
                this.View.BindDepartments(departments);
            }
        }
    
        public class DepartmentSelectedEventArgs : EventArgs
        {
            public string Department { get; private set; }
            public DepartmentSelectedEventArgs(string department)
            {
                this.Department = department;
            }
        }
    
        public interface IEmployeeSearchView
        {
            void BindEmployees(IEnumerable<Employee> employees);
            void BindDepartments(IEnumerable<string> departments);
            event EventHandler<DepartmentSelectedEventArgs> DepartmentSelected;
        }
    
        public class EmployeeRepository
        {
            private static IList<Employee> employees;
            static EmployeeRepository()
            {
                employees = new List<Employee>();
                employees.Add(new Employee("001", "张三", "", new DateTime(1981, 8, 24), "销售部"));
                employees.Add(new Employee("002", "李四", "", new DateTime(1982, 7, 24), "人事部"));
                employees.Add(new Employee("003", "王五", "", new DateTime(1981, 9, 21), "销售部"));
                employees.Add(new Employee("004", "张三1", "", new DateTime(1981, 8, 24), "销售部"));
                employees.Add(new Employee("005", "张三2", "", new DateTime(1981, 8, 24), "销售部"));
                employees.Add(new Employee("006", "张三3", "", new DateTime(1981, 8, 24), "销售部"));
            }
    
            public IEnumerable<Employee> GetEmployees(string department = "")
            {
                if (string.IsNullOrEmpty(department))
                {
                    return employees;
                }
                return employees.Where(e => e.Department == department).ToArray();
            }
        }
    
        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;
            }
        }

    界面部分aspx

    <form id="form1" runat="server">
        <div>
            <div class="top">
                选择查询部门:
                <asp:DropDownList ID="DropDownListDepartments" runat="server"></asp:DropDownList>
                <asp:Button ID="ButtonSearch" runat="server" Text="查询" OnClick="ButtonSearch_Click" />
            </div>
            <asp:GridView ID="GridViewEmployees" runat="server" AutoGenerateColumns="false" Width="100%">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="姓名" />
                    <asp:BoundField DataField="Gender" HeaderText="性别" />
                    <asp:BoundField DataField="BirthDate" HeaderText="出生日期" DataFormatString="{0:dd/MM/yyyy}" />
                    <asp:BoundField DataField="Department" HeaderText="部门" />
                </Columns>
            </asp:GridView>
        </div>
        </form>

    主要就是aspx实现接口,Presenter接收View,执行相关方法,将Model与界面分离

  • 相关阅读:
    快速生成移动设备应用图标的在线工具 makeappicon
    绝对令人的惊叹的CSS3折叠效果
    GBin1专题之Web热点秀#14
    对一个正整数n,求出n!中末尾0的个数。
    大整数加法
    HDOJ2013
    HDOJ1203 I NEED A OFFER!
    HDOJ1005【苏哥的解法】
    HDOJ1297
    HDOJ1004
  • 原文地址:https://www.cnblogs.com/zhangdx/p/4440268.html
Copyright © 2011-2022 走看看