標籤:style http io os 使用 java ar for 檔案
專案檔架構
實現步驟為:4-3-6-5-2-1
ID
項目
描述
用途
項目參考關聯性
執行個體所需檔案
相關方法
1
Web
表現層
Web頁和控制項
引用BLL
WebUI.aspx
WebUI.aspx.cs
GetContent()
2
BLL
商務邏輯層
商務邏輯組件
引用 IDAL,Model,使用DALFactory建立執行個體
Content.cs
ContentInfo GetContentInfo(int id)
3
IDAL
資料訪問層介面定義
每個DAL實現都要實現的一組介面
引用 Model
IContent.cs
ContentInfo GetContentInfo(int id)
4
Model
業務實體
傳遞各種資料的容器
無引用
ContentInfo.cs
5、DALFactory
資料層的抽象工廠
建立反射,用來確定載入哪一個資料庫訪問程式集的類
引用IDAL,通過讀取web.config裡設定的程式集,載入類的執行個體,返回給BLL使用。
Content.cs
IDAL.Icontent create()
6、SQLServerDAL
SQLServer資料訪問層
Microsoft SQL Server特定的Pet Shop DAL實現,使用了IDAL介面
引用 Model和IDAL,被DALFactory載入的程式集,實現介面裡的方法。
SqlHelper.cs
Content.cs
SqlDataReader ExecuteReader()
PrepareCommand()
ContentInfo GetContentInfo(int id)
OracleDAL
Oracle資料訪問層
7、DBUtility
資料庫訪問組件基礎類
GetSqlServerConnectionString得到資料庫連接字串,也可省去該項目,在SQLServerDAL.SqlHelper中用static readonly string SqlConnectionString代替。
無引用
實現步驟過程
1、建立Model,實現業務實體。
2、建立IDAL,實現介面。
3、建立SQLServerDAL,實現介面裡的方法。
4、增加web.config裡的配置資訊,為SQLServerDAL的程式集。
5、建立DALFactory,返回程式集的指定類的執行個體。
6、建立BLL,調用DALFactory,得到程式集指定類的執行個體,完成資料操作方法。
7、建立WEB,調用BLL裡的資料操作方法。
注意:
1、web.config裡的程式集名稱必須與SQLServerDAL裡的輸出程式集名稱一致。
2、DALFactory裡只需要一個DataAccess類,可以完成建立所有的程式集執行個體。
3、項目建立後,注意修改各項目的預設命名空間和程式集名稱。
4、注意修改解決方案裡的項目依賴。
5、注意在解決方案裡增加各項目引用。
三、各間的訪問過程
1、傳入值,將值進行類型轉換(為整型)。
2、建立BLL層的content.cs對象c,通過對象c訪問BLL層的方法GetContentInfo(ID)調用BLL層。
3、BLL層方法GetContentInfo(ID)中取得資料訪問層SQLServerDAL的執行個體,執行個體化IDAL層的介面對象dal,這個對象是由工廠層DALFactory建立的,然後返回IDAL層傳入值所尋找的內容的方法dal.GetContentInfo(id)。
4、資料處理站通過web.config設定檔中給定的webdal字串訪問SQLServerDAL層,返回一個完整的調用SQLServerDAL層的路徑給 BLL層。
5、到此要調用SQLServerDAL層,SQLServerDAL層完成賦值Model層的對象值為空白,給定一個參數,調用SQLServerDAL層的SqlHelper的ExecuteReader方法,讀出每個欄位的資料賦值給以定義為空白的Model層的對象。
6、SqlHelper執行sql命令,返回一個指定串連的資料庫記錄集,在這裡需要引用參數類型,提供為開啟串連命令執行做好準備PrepareCommand。
7、返回Model層把查詢得到的一行記錄值賦值給SQLServerDAL層的引入的Model層的對象ci,然後把這個對象返回給BLL。
8、回到Web層的BLL層的方法調用,把得到的對象值賦值給Lable標籤,在前台顯示給介面
四、項目中的檔案清單
1、DBUtility項目
(1)connectionInfo.cs
using System;
using System.Configuration;
namespace Utility
{
/// <summary>
/// ConnectionInfo 的摘要說明。
/// </summary>
public class ConnectionInfo
{
public static string GetSqlServerConnectionString()
{
return ConfigurationSettings.AppSettings["SQLConnString"];
}
}
}
2、SQLServerDAL項目
(1)SqlHelper.cs抽象類別
using System;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
namespace SQLServerDAL
{
/// SqlHelper 的摘要說明。
public abstract class SqlHelper
{
public static readonly string CONN_STR = ConnectionInfo.GetSqlServerConnectionString();
/// 用提供的函數,執行SQL命令,返回一個從指定串連的資料庫記錄集
/// 例如:
/// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">SqlConnection有效SQL連接字串</param>
/// <param name="commandType">CommandType:CommandType.Text、CommandType.StoredProcedure</param>
/// <param name="commandText">SQL語句或預存程序</param>
/// <param name="commandParameters">SqlParameter[]參數數組</param>
/// <returns>SqlDataReader:執行結果的記錄集</returns>
public static SqlDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connString);
// 我們在這裡用 try/catch 是因為如果這個方法拋出異常,我們目的是關閉資料庫連接,再拋出異常,
// 因為這時不會有DataReader存在,此後commandBehaviour.CloseConnection將不會工作。
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
/// <summary>
/// 為執行命令做好準備:開啟資料庫連接,命令語句,設定命令類型(SQL語句或預存程序),函數語取。
/// </summary>
/// <param name="cmd">SqlCommand 組件</param>
/// <param name="conn">SqlConnection 組件</param>
/// <param name="trans">SqlTransaction 組件,可以為null</param>
/// <param name="cmdType">語句類型:CommandType.Text、CommandType.StoredProcedure</param>
/// <param name="cmdText">SQL語句,可以為預存程序</param>
/// <param name="cmdParms">SQL參數數組</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
}
(2)Content.cs類
using System;
using System.Data;
using System.Data.SqlClient;
using Model;
using IDAL;
namespace SQLServerDAL
{
/// <summary>
/// Content 的摘要說明。
/// </summary>
public class Content:IContent
{
private const string PARM_ID = "@ID";
private const string SQL_SELECT_CONTENT = "Select ID, Title, Content, AddDate, CategoryID From newsContent Where ID = @ID";
public ContentInfo GetContentInfo(int id)
{
//創意文章內容類別
ContentInfo ci = null;
//建立一個參數
SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.BigInt, 8);
//賦上ID值
parm.Value = id;
using(SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper.CONN_STR, CommandType.Text, SQL_SELECT_CONTENT, parm))
{
if(sdr.Read())
{
ci = new ContentInfo(sdr.GetInt32(0),sdr.GetString(1), sdr.GetString(2),
sdr.GetDateTime(3), sdr.GetInt32(4), sdr.GetInt32(5), sdr.GetString(6));
}
}
return ci;
}
}
}
3、Model項目
(1)contentInfo.cs
using System;
namespace Model
{
/// <summary>
/// Class1 的摘要說明。
/// </summary>
public class ContentInfo
{
private int _ID;
private string _Content;
private string _Title;
private string _From;
private DateTime _AddDate;
private int _clsID;
private int _tmpID;
/// <summary>
/// 文章內容建構函式
/// </summary>
/// <param name="id">文章流水號ID</param>
/// <param name="content">文章內容</param>
/// <param name="title">文章標題</param>
/// <param name="from">文章來源</param>
/// <param name="clsid">文章的分類屬性ID</param>
/// <param name="tmpid">文章的模板屬性ID</param>
public ContentInfo(int id,string title,string content,string from,DateTime addDate,int clsid,int tmpid )
{
this._ID = id;
this._Content = content;
this._Title = title;
this._From = from;
this._AddDate = addDate;
this._clsID = clsid;
this._tmpID = tmpid;
}
//屬性
public int ID
{
get { return _ID; }
}
public string Content
{
get { return _Content; }
}
public string Title
{
get { return _Title; }
}
public string From
{
get { return _From; }
}
public DateTime AddDate
{
get { return _AddDate; }
}
public int ClsID
{
get { return _clsID; }
}
public int TmpID
{
get { return _tmpID; }
}
}
}
4、IDAL項目
(1)Icontent.cs
using System;
using Model;
namespace IDAL
{
/// <summary>
/// 文章內容操作介面
/// </summary>
public interface IContent
{
/// <summary>
/// 取得文章的內容。
/// </summary>
/// <param name="id">文章的ID</param>
/// <returns></returns>
ContentInfo GetContentInfo(int id);
}
}
5、DALFactory項目
(1)Content.cs
using System;
using System.Reflection;
using System.Configuration;
using IDAL;
namespace DALFactory
{
/// <summary>
/// 工產模式實現文章介面。
/// </summary>
public class Content
{
public static IDAL.IContent Create()
{
// 這裡可以查看 DAL 介面類。
string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"].ToString();
string className = path+".Content";
// 用設定檔指定的類組合
return (IDAL.IContent)Assembly.Load(path).CreateInstance(className);
}
}
}
6、BLL項目
(1)Content.cs
using System;
using Model;
using IDAL;
namespace BLL
{
/// <summary>
/// Content 的摘要說明。
/// </summary>
public class Content
{
public ContentInfo GetContentInfo(int id)
{
// 取得從資料訪問層取得一個文章內容執行個體
IContent dal = DALFactory.Content.Create();
// 用DAL尋找文章內容
return dal.GetContentInfo(id);
}
}
}
7、Web項目
1、Web.config:
<appSettings>
<add key="SQLConnString" value="Data Source=localhost;Persist Security info=True;Initial Catalog=newsDB;User ID=sa;Password= " />
<add key="WebDAL" value="SQLServerDAL" />
</appSettings>
2、WebUI.aspx
<%@ Page language="c#" Codebehind="WebUI.aspx.cs" AutoEventWireup="false" Inherits="Web.WebUI" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebUI</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT">宋體"></FONT>
<table width="600" border="1">
<tr>
<td style="WIDTH: 173px"> </td>
<td>
<asp:Label id="lblTitle" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="WIDTH: 173px; HEIGHT: 22px"> </td>
<td style="HEIGHT: 22px">
<asp:Label id="lblDataTime" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="WIDTH: 173px"> </td>
<td>
<asp:Label id="lblContent" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="WIDTH: 173px"> </td>
<td> </td>
</tr>
<tr>
<td style="WIDTH: 173px; HEIGHT: 23px"> </td>
<td style="HEIGHT: 23px"> </td>
</tr>
<tr>
<td style="WIDTH: 173px"> </td>
<td> </td>
</tr>
<tr>
<td style="WIDTH: 173px"> </td>
<td> </td>
</tr>
<tr>
<td style="WIDTH: 173px"> </td>
<td> </td>
</tr>
<tr>
<td style="WIDTH: 173px"> </td>
<td>
<asp:Label id="lblMsg" runat="server">Label</asp:Label></td>
</tr>
</table>
</form>
</body>
</HTML>
asp.net網站三層架構詳解和反射知識