相比较而言我喜欢MVP V视图,p中介者,m模型,轻量设计模式,将藕合降到最低还是很不错的,今天小小的学习了下~~流个念~~
处理流程
第一由mvp.aspx发起请求(一般该页面会继承一个视图接口)ProductPresenter类接收请求,并传递到合适的模型上
第2模型ProductService接收请求并开始处理
第3~4为后台处理数据
第5返回处理数据到ProductPresenter类
第六ProductPresenter在返回到页面mvp.aspx
代码--没有经过修改~~凑合看吧
using System; namespace Chapter2.Web.Interface { /// <summary> /// 视图接口 /// </summary> public interface IProductView { void DesplayProducts(System.Collections.Generic.List<Chapter2.Repository.Product> data); } }
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// IProductView继承试图操作 /// </summary> public partial class Default4 : System.Web.UI.Page, Chapter2.Web.Interface.IProductView { private Chapter2.Web.Presenter.ProductPresenter preseter=null; protected override void OnInit(EventArgs e) { base.OnInit(e); //--初实化 preseter = new Chapter2.Web.Presenter.ProductPresenter(); preseter.Init(this); } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { // preseter.Init(this); preseter.LoadProductsByCategory(1); } } public void DesplayProducts(System.Collections.Generic.List<Chapter2.Repository.Product> data) { this.GridView1.DataSource = data; this.GridView1.DataBind(); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { int Category =Convert.ToInt16(DropDownList1.SelectedValue); preseter.LoadProductsByCategory(Category); } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mvp.aspx.cs" Inherits="Default4" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> 查询<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> <asp:ListItem>4</asp:ListItem> <asp:ListItem>5</asp:ListItem> <asp:ListItem>6</asp:ListItem> </asp:DropDownList> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div> </form> </body> </html>
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace Chapter2.Web.Presenter { /// <summary> /// ProductPresenter控制器 /// </summary> public class ProductPresenter { Chapter2.Service.ProductService service= null; private Chapter2.Web.Interface.IProductView view = null; public ProductPresenter() { service = new Chapter2.Service.ProductService(); // // TODO: 在此处添加构造函数逻辑 // } public void Init(Chapter2.Web.Interface.IProductView inputView) { view = inputView; } public void LoadProductsByCategory(int categoryid) { System.Collections.Generic.List<Chapter2.Repository.Product> data = service.GetAllProductsFrom(categoryid); if(data!=null&&data.Count>0) { this.view.DesplayProducts(data); } } } }
using System; using System.Collections.Generic; using Chapter2.Repository; namespace Chapter2.Service { /// <summary> /// ProductService 的摘要说明 /// </summary> public sealed class ProductService { private Chapter2.Repository.IProductRepository productRepository = null; private Chapter2.Cache.ICacheStorage cache = null; public ProductService() { productRepository = Chapter2.Factory.RepositoryFactory.GetRepository(); cache=Chapter2.Factory.CacheFactory.GetCache(); } public List<Product> GetAllProductsFrom(int categoryId) { List<Product> l = new List<Product>(); try { string cacheKey = string.Format("products_in_category_{0}", categoryId); l = cache.Get(cacheKey) as List<Product>; if (l == null) { l = productRepository.getProductsFrom(categoryId); if (l != null && l.Count > 0) { cache.Add(cacheKey, l); } } } catch (System.Exception exp) { } finally { } return l; } } }
using System; using System.Collections.Generic; namespace Chapter2.Repository { /// <summary> /// IProductRepository 的摘要说明 /// </summary> public interface IProductRepository { List<Product> getProductsFrom(int categoryId); } }
using System; using System.Collections.Generic; namespace Chapter2.Repository { /// <summary> /// liujiaRepository 的摘要说明 /// </summary> public sealed class liujiaRepository : IProductRepository { public liujiaRepository() { // // TODO: 在此处添加构造函数逻辑 // } public List<Product> getProductsFrom(int categoryId) { List<Product> l = new List<Product>(); Product p = new Product(); p.CategoryId=categoryId; l.Add(p); return l; } } }