本文标签: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 { public class Database { protected SqlConnection conn; protected string connStr; public Database() { this.connStr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString; } 'Database() { try { if (conn != null) { conn.Close(); } } catch { } } protected void Open() { if (conn == null) { conn = new SqlConnection(connStr); } if (conn.State.Equals(ConnectionState.Closed)) { conn.Open(); } } protected void Close() { if (conn != null) { conn.Close(); } } 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
- {
-
-
-
-
- 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"];
- }
- }
-
-
-
- #region
- private int categoryID;
-
-
-
- public int CategoryID
- {
- get { return categoryID; }
- set { categoryID = value; }
- }
- private string categoryName;
-
-
-
- public string CategoryName
- {
- get { return categoryName; }
- set { categoryName = value; }
- }
- private string description;
-
-
-
- public string Description
- {
- get { return description; }
- set { description = value; }
- }
- private byte[] image;
-
-
-
- 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);
-
-
- }
- catch
- {
- Response.Write("< mce:script type="text/javascript">< !--
- alert(只能输入1-9之间的数字)
-
- 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;
-
- 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;
- }
- }
- }
-
|