MVC三层架构的示例分析

MVC三层架构的示例分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

成都创新互联专业为企业提供拉孜网站建设、拉孜做网站、拉孜网站设计、拉孜网站制作等企业网站建设、网页设计与制作、拉孜企业网站模板建站服务,十余年拉孜做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

前几天收到CodeProject的电邮,asp.net mvc 的E文教程正在编写,一个老外蛮伟大的,免费贡献***章,也有100多页的内容。其中大量应用了LINQ技术(看来得花时间看看了,用统一的方式来面对所有数据源,确实还是蛮吸引人的)。当然,LINQ不是MVC必须的,你可以用很多技术实现,比如NHibernate,甚至原生的ADO.NET。

既然是实例,我直接上代码了,基础理论一搜一大把,但我还是觉得实践才是最重要的:

ASP.NET MVC三层架构实例:首先的数据访问层,Database类:

using System;     using System.Data;     using System.Configuration;     using System.Linq;     using System.Web;     using System.Web.Security;     using System.Web.UI;     using System.Web.UI.HtmlControls;     using System.Web.UI.WebControls;     using System.Web.UI.WebControls.WebParts;     using System.Xml.Linq;         using System.Data.SqlClient;         namespace northWind3Tier.DataAccessLayer     {         /// < summary>         /// 用于数据访问         /// < /summary>         public class Database         {             /// < summary>             /// 数据库连接             /// < /summary>             protected SqlConnection conn;             /// < summary>             /// 数据库连接字符串             /// < /summary>             protected string connStr;                 public Database()             {                 this.connStr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;             }             /// < summary>             /// 关闭数据库连接             /// < /summary>              ~Database()//析构函数不带访问修饰符             {                 try                {                     if (conn != null)                     {                         conn.Close();                     }                 }                 catch { }             }             /// < summary>             /// 打开数据库连接             /// < /summary>             protected void Open()             {                 if (conn == null)                 {                     conn = new SqlConnection(connStr);                 }                 if (conn.State.Equals(ConnectionState.Closed))                 {                     conn.Open();                 }             }             /// < summary>             /// 关闭数据库连接             /// < /summary>             protected void Close()             {                 if (conn != null)                 {                     conn.Close();                 }             }             /// < summary>             /// 获取数据,返回一个dataset             /// < /summary>             /// < param name="sql">sql语句< /param>             /// < returns>< /returns>             public DataSet GetDataSet(string sql)             {                 Open();                 SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);                 DataSet dataset = new DataSet();                 adapter.Fill(dataset);                 Close();                 return dataset;                       }         }     }

ASP.NET MVC三层架构实例:业务逻辑层 Category类:

using System;     using System.Data;     using System.Configuration;     using System.Linq;     using System.Web;     using System.Web.Security;     using System.Web.UI;     using System.Web.UI.HtmlControls;     using System.Web.UI.WebControls;     using System.Web.UI.WebControls.WebParts;     using System.Xml.Linq;         using System.IO;     using northWind3Tier.DataAccessLayer;     namespace northWind3Tier.BusinessLayer     {         public class Category         {             /// < summary>             /// 根据货物ID获取该货物的详细信息             /// < /summary>             /// < param name="categoryID">< /param>             public void LoadData(int categoryID)             {                 Database db = new Database();                 string sql = "select * from [Categories] where [CategoryID]="+categoryID;                 DataSet ds = db.GetDataSet(sql);                 //如果有查询到数据的话,填充属性                 if (ds.Tables[0].Rows.Count > 0)                 {                     this.categoryID =(int) ds.Tables[0].Rows[0]["CategoryID"];                     this.categoryName = ds.Tables[0].Rows[0]["CategoryName"].ToString();                     this.description = ds.Tables[0].Rows[0]["Description"].ToString();                     this.image =(byte[]) ds.Tables[0].Rows[0]["Picture"];                              }             }             /// < summary>             /// 字段和属性             /// < /summary>            #region             private int categoryID;             /// < summary>             /// 编号             /// < /summary>             public int CategoryID             {                 get { return categoryID; }                 set { categoryID = value; }             }             private string categoryName;             /// < summary>             /// 名称             /// < /summary>             public string CategoryName             {                 get { return categoryName; }                 set { categoryName = value; }             }             private string description;             /// < summary>             /// 说明             /// < /summary>             public string Description             {                 get { return description; }                 set { description = value; }             }             private byte[] image;             /// < summary>             /// 图像             /// < /summary>             public byte[] Image             {                 get { return image; }                 set { image = value; }             }            #endregion                         }     }

ASP.NET MVC三层架构实例:***就是显示层,前台aspx代码:

< %@ Page Language="C#" AutoEventWireup="true" CodeBehind="CateqoryQuery.aspx.cs" Inherits="northWind3Tier.CateqoryQuery" %>         < !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>Untitled Page< /title>     < /head>     < body>         < form id="form1" runat="server">         < div>                      < asp:Label ID="Label1" runat="server" Text="货物编号(1-9):">< /asp:Label>                     < asp:TextBox ID="TextBox1" runat="server">< /asp:TextBox>                     < asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="查询"                  Width="65px" />             < br />             < br />             < asp:Label ID="lblCategoryInfo" runat="server" Text="Label">< /asp:Label>             < br />             < asp:Image ID="Image1" runat="server" />                  < /div>         < /form>     < /body>     < /html>

ASP.NET MVC三层架构实例:后台cs代码:

using System;     using System.Collections;     using System.Configuration;     using System.Data;     using System.Linq;     using System.Web;     using System.Web.Security;     using System.Web.UI;     using System.Web.UI.HtmlControls;     using System.Web.UI.WebControls;     using System.Web.UI.WebControls.WebParts;     using System.Xml.Linq;     using northWind3Tier.BusinessLayer;     using System.IO;         namespace northWind3Tier     {         public partial class CateqoryQuery : System.Web.UI.Page         {             protected void Page_Load(object sender, EventArgs e)             {                 }                 protected void Button1_Click(object sender, EventArgs e)             {                 int categoryID = -1;                     if (TextBox1.Text != "")                 {                     try                    {                         categoryID = Convert.ToInt32(TextBox1.Text);                         //if ((categoryID <  1) || (categoryID > 9))                                                 }                     catch                    {                         Response.Write("< mce:script type="text/javascript">< !--     alert('只能输入1-9之间的数字')     // -->< /mce:script>");                          return;                     }                 }                 Category category = new Category();                 category.LoadData(categoryID);                     lblCategoryInfo.Text = "编号:" + category.CategoryID;                 lblCategoryInfo.Text += "< BR>名称:" + category.CategoryName;                 lblCategoryInfo.Text += "< BR>描述:" + category.Description;                 byte[] image = category.Image;                 //northwind数据库中的image字段(byte数组)的前面78是无用的,必须剔除才能正常显示图像                 byte[] temp = new byte[image.Length - 78];                 Array.Copy(image , 78, temp, 0, image.Length - 78);                     string strPath = "photo/temp.JPG";                 string strPhotoPath =strPath;                 //保存图片文件                 BinaryWriter bw = new BinaryWriter(File.Open(Server.MapPath (strPhotoPath), FileMode.OpenOrCreate));                 bw.Write(temp);                 bw.Close();                 //显示图片                 this.Image1.ImageUrl = strPath;             }         }     }

关于MVC三层架构的示例分析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


网站题目:MVC三层架构的示例分析
转载注明:http://scjbc.cn/article/geophd.html

其他资讯