1.2 MVC 变体


MVC 是一种Pattern 另外一种说法是ParaDigm 范例

模式和范例的区别在于前者可以应用到具体的应用上,而后者则仅仅提供一些指导方针

1.2.1 MVP

Model View Presenter 交互图

MVC 模式中元素之间 “混乱”的交互主要体现在允许View 和 Model 绕开Controller进行单独交流,这在MVP 模式中得到了充分解决

PV 模式 (passive View)

解决View很难测试的最好方法是让他无需测试,让UI处理更少逻辑,被动的交给Presenter来处理

暴漏UI不一定必须暴漏UI控件,

没有必要写成

public interface IemployeeSearchView1
{
DropDownList Department { get; }
GridView Employees { get; }
}

而可以通过数据的属性来定义接口,这样就可以将presenter中的UI处理逻辑分离出来

public interface IemployeeSearchView
{
IEnumerable<string> Departments { set; }
string SelectedDedpartment { get; }
IEnumerable<Employee> Employees { set; }
}

具体的实现方法如下:

public partial class _Default : System.Web.UI.Page, IemployeeSearchView
{
protected void Page_Load(object sender, EventArgs e)
{ } public IEnumerable<string> Departments
{
set
{
this.DropDownListDepartments.DataSource = value;
this.DropDownListDepartments.DataBind();
}
} public string SelectedDedpartment
{
get { return this.DropDownListDepartments.SelectedValue; }
} public IEnumerable<Employee> Employees
{
set
{
this.GridViewEmployees.DataSource = value;
this.GridViewEmployees.DataBind();
}
}
}

这样做意味着所有的UI逻辑都可以被测试,但是它要求将所有可供操作的UI元素定义在对应的接口中,无疑会增加Presenter 的复杂度,这就引出SC模式

SC模式(supervsing Controller)

View 和Presenter 之间的交互式整个MVP的核心,能否正确地应用MVP模式来架构应用,主要取决于View 和Presenter 之间的关系 在由model、View Presenter 组成的三角关系中,核心不是View 而是Presenter,Presenter 不仅是View调用的中介,而是最终决定如何响应用户交互行为的决策者。

View 本身仅仅实现单纯的独立的UI处理逻辑,他处理的数据应该是Presneter实时推送过来的,所以View 尽可能不去维护数据状态,定义在IView的接口最好只包含方法,而避免属性的定义,Presenter 所需的关于View的状态应该在接收到View发送的用户请求时一次性推送,而不需要通过View 的属性去获得它

以下为所有代码

Model

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;
}
}

EmployeeRespository

   public class EmployeeRepository
{
private static IList<Employee> employees; static EmployeeRepository()
{
employees = new List<Employee>();
employees.Add(new Employee("", "张三", "男", new DateTime(, , ), "销售部"));
employees.Add(new Employee("", "李四", "女", new DateTime(, , ), "人事部"));
employees.Add(new Employee("", "王五", "男", new DateTime(, , ), "人事部")); } public IEnumerable<Employee> GetEmployees(string department = "")
{
if (string.IsNullOrEmpty(department))
{
return employees;
}
return employees.Where(e => e.Department == department).ToArray(); }
}

IEmployeeSearchView

 public interface IEmployeeSearchView
{
void BindEmployees(IEnumerable<Employee> employees);
void BindDepartments(IEnumerable<string> departments); } public class DepartMentSelectedEventArgs : EventArgs
{
public string Department { get; private set; }
public DepartMentSelectedEventArgs(string department)
{
this.Department = department;
}
}

EmployeeSearchPresenter

 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();
view.DepartmentSelected += view_DepartmentSelected;
} public void Initialize()
{
var employees = this.Repository.GetEmployees();
this.View.BindEmployees(employees);
string[] departments = new string[] { "销售部", "人事部", "IT部" };
this.View.BindDepartments(departments);
} void view_DepartmentSelected(object sender, DepartMentSelectedEventArgs e)
{
string department = e.Department;
var employees = Repository.GetEmployees(department);
this.View.BindEmployees(employees);
}
}

Web

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Chaper1SC.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="page">
<div style="text-align: center; clear: both">
请选择部门:<asp:DropDownList ID="dpDownListDepartments" runat="server"></asp:DropDownList><asp:Button Text="查询" runat="server" OnClick="Unnamed_Click" />
</div>
<div>
<asp:GridView ID="GvEmployees" 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/yy}" />
<asp:BoundField DataField="Gender" HeaderText="性别" />
</Columns>
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
 public partial class WebForm1 : System.Web.UI.Page, IEmployeeSearchView
{
public event EventHandler<DepartMentSelectedEventArgs> DepartmentSelected;
public EmployeeSearchPresenter Presenter { get; private set; } public WebForm1()
{
this.Presenter = new EmployeeSearchPresenter(this);
} protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Presenter.Initialize();
}
} public void BindEmployees(IEnumerable<Employee> employees)
{
this.GvEmployees.DataSource = employees;
this.GvEmployees.DataBind();
} public void BindDepartments(IEnumerable<string> departments)
{
this.dpDownListDepartments.DataSource = departments;
this.dpDownListDepartments.DataBind();
} protected void Unnamed_Click(object sender, EventArgs e)
{
string deparment = this.dpDownListDepartments.SelectedValue;
DepartMentSelectedEventArgs args = new DepartMentSelectedEventArgs(deparment);
if (null != DepartmentSelected)
{
DepartmentSelected(sender, args);
}
} }

asp.net MVC4 框架揭秘 读书笔记系列2的更多相关文章

  1. asp.net MVC4 框架揭秘 读书笔记系列3

    IIS/ASP.net管道 本节全部用图形表示便于理解和记忆 1.3.1 IIS5.x与asp.net 1.3.2 IIS 6.0与asp.net 1.3.3 IIS7.0与asp.net 基于IIS ...

  2. asp.net MVC4 框架揭秘 读书笔记系列1

    1.1 传统MVC 名词解释 Autonomous View. AV. 自制视图 GUI图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式 ...

  3. ASP.NET MVC5框架揭秘 学习笔记01

    1.自治视图 在早期(作者也没说明是多早,自己猜吧),我们倾向于将所有与UI相关的操作糅合在一起(现在我身边还有很多人这样做),这些操作包括UI界面的呈现. 用户交互操作的捕捉与响应(UI处理逻辑). ...

  4. Android 框架揭秘 --读书笔记

    Android 框架揭秘 Insied the Android Framework

  5. ASP.NET WEB API 2 框架揭秘 读书笔记(一)

    第一章 概述 主要内容是介绍Web的基本概念,Restfull的基本概念及特性.最后介绍创建简单WebApi程序的步骤. Web的基本概念 IP/TCP协议簇分层,分为两种 链路层->网络层-& ...

  6. ASP.NET MVC4框架揭秘 源代码下载

    http://files.cnblogs.com/artech/asp.net.mvc.4.samples.rar

  7. 《ASP.NET Core In Action》读书笔记系列,这是一个手把手的从零开始的教学系列目录

    最近打算系统学习一下asp.net  core ,苦于没有好的中文书藉,只好找来一本英文的 <ASP.NET Core In Action>学习.我和多数人一样,学习英文会明显慢于中文.希 ...

  8. C#刨根究底:《你必须知道的.NET》读书笔记系列

    一.此书到底何方神圣? <你必须知道的.NET>来自于微软MVP—王涛(网名:AnyTao,博客园大牛之一,其博客地址为:http://anytao.cnblogs.com/)的最新技术心 ...

  9. C#温故知新:《C#图解教程》读书笔记系列

    一.此书到底何方神圣? 本书是广受赞誉C#图解教程的最新版本.作者在本书中创造了一种全新的可视化叙述方式,以图文并茂的形式.朴实简洁的文字,并辅之以大量表格和代码示例,全面.直观地阐述了C#语言的各种 ...

随机推荐

  1. Go 编译和runtime

    一篇题为:Analysis of the Go runtime scheduler 的论文,其中部分章节介绍到了Go runtime. 先上图,这张图描述了Go语言程序,Runtime和操作系统之间的 ...

  2. 自定义针对Product Key处理的TextBox

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  3. ruby字符串连接

    malls = Mall.allcount = 0malls.each do |mall|    count += 1    if mall.parent_ids[0]        province ...

  4. 如何编写makefile文件

    最近一直在学习makefile是如何编写的.       当我们写的程序文件比较少的时候,敲入gcc /g++,当你在大型工程中,在一个个编译文件的话,你可能就会很郁闷.linux有一个自带的make ...

  5. Resharper 使用帮助-自动生成文件头

    VS2012 安装完resharper 后,在resharper选项中选择 Code Editing – File Header Text . 输入自定义的文件头格式.如果需要在文件头外层添加regi ...

  6. oracle网页客户端工具

    Oracle数据库的管理相较于其他数据库,是比较麻烦的,客户端工具的安装都要花一些时间,目前有一款treesoft软件,通过网页的方式管理Oracle. 功能包括:数据库的展示,库表的展示,表字段结构 ...

  7. SpringMVC配置式开发

    所谓配置式开发是指“处理器类是程序员手工定义,实现了特定接口的类,然后再在SpringMVC 配置文件中对该类进行显示的.明确的注册”的开发方式. 一.处理器映射器HandlerMapping Han ...

  8. CSS选择器深入探讨(细节东西)(转)

    细节决定成败,越是注重细节方面的东西,那么你完成的作品就越完美. 1.父子选择器(看作组合比较好理解) 父子选择器可以有多级(但是在实际开发中最后不好超过三层) 如:html中文件片段: <!- ...

  9. JSON.stringify时间的问题

    使用Element UI控件el-date-picker后台传值用到JSON.stringify转换字符串传后台发现输出国际标准时间(ISO),减少了8个小时, 解决方式: 由于JSON.string ...

  10. 通过UA判断,对滚动条样式进行不同的操作

    浏览器滚动条的默认样式比较丑,有些情况下,又不能直接overflow:hidden掉. 本文阐述如何通过 document.styleSheets[0].insertRule 简单的实现pc端和移动端 ...