亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

asp.net三層架構(gòu)詳解

系統(tǒng) 3583 0
/* ============================================================== */
/* DBMSname:MicrosoftSQLServer2000 */
/* ============================================================== */


if exists ( select 1
from sysobjects
where id = object_id ( ' newsContent ' )
and type = ' U ' )
drop table newsContent
go


/* ============================================================== */
/* Table:newsContent */
/* ============================================================== */
create table newsContent(
ID int identity ( 1 , 1 ) primary key ,
Title nvarchar ( 50 ) not null ,
Content ntext not null ,
AddDate datetime not null ,
CategoryID int not null
)
go

二、項(xiàng)目文件架構(gòu)

實(shí)現(xiàn)步驟為:4-3-6-5-2-1

ID

項(xiàng)目

描述

用途

項(xiàng)目引用關(guān)系

實(shí)例所需文件

相關(guān)方法

1

Web

表現(xiàn)層

Web頁(yè)和控件

引用BLL

WebUI.aspx

WebUI.aspx.cs

GetContent()

2

BLL

業(yè)務(wù)邏輯層

業(yè)務(wù)邏輯組件

引用 IDAL,Model,使用DALFactory創(chuàng)建實(shí)例

Content.cs

ContentInfo GetContentInfo(int id)

3

IDAL

數(shù)據(jù)訪問層接口定義

每個(gè)DAL實(shí)現(xiàn)都要實(shí)現(xiàn)的一組接口

引用 Model

IContent.cs

ContentInfo GetContentInfo(int id)

4

Model

業(yè)務(wù)實(shí)體

傳遞各種數(shù)據(jù)的容器

無引用

ContentInfo.cs

5

DALFactory

數(shù)據(jù)層的抽象工廠

創(chuàng)建反射,用來確定加載哪一個(gè)數(shù)據(jù)庫(kù)訪問程序集的類

引用IDAL,通過讀取web.config里設(shè)置的程序集,加載類的實(shí)例,返回給BLL使用。

Content.cs

IDAL.Icontent create()

6

SQLServerDAL

SQLServer數(shù)據(jù)訪問層

Microsoft SQL Server特定的Pet Shop DAL實(shí)現(xiàn),使用了IDAL接口

引用 Model和IDAL,被DALFactory加載的程序集,實(shí)現(xiàn)接口里的方法。

SqlHelper.cs

Content.cs

SqlDataReader ExecuteReader()

PrepareCommand()

ContentInfo GetContentInfo(int id)

OracleDAL

Oracle數(shù)據(jù)訪問層

7

DBUtility

數(shù)據(jù)庫(kù)訪問組件基礎(chǔ)類

GetSqlServerConnectionString得到數(shù)據(jù)庫(kù)連接字符串,也可省去該項(xiàng)目,在SQLServerDAL.SqlHelper中用static readonly string SqlConnectionString代替。

無引用

實(shí)現(xiàn)步驟過程

1、創(chuàng)建Model,實(shí)現(xiàn)業(yè)務(wù)實(shí)體。

2、創(chuàng)建IDAL,實(shí)現(xiàn)接口。

3、創(chuàng)建SQLServerDAL,實(shí)現(xiàn)接口里的方法。

4、增加web.config里的配置信息,為SQLServerDAL的程序集。

5、創(chuàng)建DALFactory,返回程序集的指定類的實(shí)例。

6、創(chuàng)建BLL,調(diào)用DALFactory,得到程序集指定類的實(shí)例,完成數(shù)據(jù)操作方法。

7、創(chuàng)建WEB,調(diào)用BLL里的數(shù)據(jù)操作方法。

注意:

1、web.config里的程序集名稱必須與SQLServerDAL里的輸出程序集名稱一致。

2、DALFactory里只需要一個(gè)DataAccess類,可以完成創(chuàng)建所有的程序集實(shí)例。

3、項(xiàng)目創(chuàng)建后,注意修改各項(xiàng)目的默認(rèn)命名空間和程序集名稱。

4、注意修改解決方案里的項(xiàng)目依賴。

5、注意在解決方案里增加各項(xiàng)目引用。

三、各層間的訪問過程

1、傳入值,將值進(jìn)行類型轉(zhuǎn)換(為整型)。

2、創(chuàng)建BLL層的content.cs對(duì)象c,通過對(duì)象c訪問BLL層的方法GetContentInfo(ID)調(diào)用BLL層。

3、BLL層方法GetContentInfo(ID)中取得數(shù)據(jù)訪問層SQLServerDAL的實(shí)例,實(shí)例化IDAL層的接口對(duì)象dal,這個(gè)對(duì)象是由工廠層DALFactory創(chuàng)建的,然后返回IDAL層傳入值所查找的內(nèi)容的方法dal.GetContentInfo(id)。

4、數(shù)據(jù)工廠通過web.config配置文件中給定的webdal字串訪問SQLServerDAL層,返回一個(gè)完整的調(diào)用SQLServerDAL層的路徑給 BLL層。

5、到此要調(diào)用SQLServerDAL層,SQLServerDAL層完成賦值Model層的對(duì)象值為空,給定一個(gè)參數(shù),調(diào)用SQLServerDAL層的SqlHelper的ExecuteReader方法,讀出每個(gè)字段的數(shù)據(jù)賦值給以定義為空的Model層的對(duì)象。

6、SqlHelper執(zhí)行sql命令,返回一個(gè)指定連接的數(shù)據(jù)庫(kù)記錄集,在這里需要引用參數(shù)類型,提供為打開連接命令執(zhí)行做好準(zhǔn)備PrepareCommand。

7、返回Model層把查詢得到的一行記錄值賦值給SQLServerDAL層的引入的Model層的對(duì)象ci,然后把這個(gè)對(duì)象返回給BLL。

8、回到Web層的BLL層的方法調(diào)用,把得到的對(duì)象值賦值給Lable標(biāo)簽,在前臺(tái)顯示給界面

四、項(xiàng)目中的文件清單

1、DBUtility項(xiàng)目

(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項(xiàng)目

(1)SqlHelper.cs抽象類

using System;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

namespace SQLServerDAL
{
/// <summary>
/// SqlHelper的摘要說明。
/// </summary>
public abstract class SqlHelper
{
public static readonly string CONN_STR=ConnectionInfo.GetSqlServerConnectionString();

/// <summary>
/// 用提供的函數(shù),執(zhí)行SQL命令,返回一個(gè)從指定連接的數(shù)據(jù)庫(kù)記錄集
/// </summary>
/// <remarks>
/// 例如:
/// SqlDataReaderr=ExecuteReader(connString,CommandType.StoredProcedure,"PublishOrders",newSqlParameter("@prodid",24));
/// </remarks>
/// <paramname="connectionString"> SqlConnection有效的SQL連接字符串 </param>
/// <paramname="commandType"> CommandType:CommandType.Text、CommandType.StoredProcedure </param>
/// <paramname="commandText"> SQL語句或存儲(chǔ)過程 </param>
/// <paramname="commandParameters"> SqlParameter[]參數(shù)數(shù)組 </param>
/// <returns> SqlDataReader:執(zhí)行結(jié)果的記錄集 </returns>
public static SqlDataReaderExecuteReader( string connString,CommandTypecmdType, string cmdText, params SqlParameter[]cmdParms)
{
SqlCommandcmd= new SqlCommand();
SqlConnectionconn= new SqlConnection(connString);

// 我們?cè)谶@里用try/catch是因?yàn)槿绻@個(gè)方法拋出異常,我們目的是關(guān)閉數(shù)據(jù)庫(kù)連接,再拋出異常,
// 因?yàn)檫@時(shí)不會(huì)有DataReader存在,此后commandBehaviour.CloseConnection將不會(huì)工作。
try
{
PrepareCommand(cmd,conn, null ,cmdType,cmdText,cmdParms);
SqlDataReaderrdr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw ;
}
}


/// <summary>
/// 為執(zhí)行命令做好準(zhǔn)備:打開數(shù)據(jù)庫(kù)連接,命令語句,設(shè)置命令類型(SQL語句或存儲(chǔ)過程),函數(shù)語取。
/// </summary>
/// <paramname="cmd"> SqlCommand組件 </param>
/// <paramname="conn"> SqlConnection組件 </param>
/// <paramname="trans"> SqlTransaction組件,可以為null </param>
/// <paramname="cmdType"> 語句類型:CommandType.Text、CommandType.StoredProcedure </param>
/// <paramname="cmdText"> SQL語句,可以為存儲(chǔ)過程 </param>
/// <paramname="cmdParms"> SQL參數(shù)數(shù)組 </param>
private static void PrepareCommand(SqlCommandcmd,SqlConnectionconn,SqlTransactiontrans,CommandTypecmdType, 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 (SqlParameterparm 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= " SelectID,Title,Content,AddDate,CategoryIDFromnewsContentWhereID=@ID " ;


public ContentInfoGetContentInfo( int id)
{
// 創(chuàng)意文章內(nèi)容類
ContentInfoci= null ;

// 創(chuàng)建一個(gè)參數(shù)
SqlParameterparm= new SqlParameter(PARM_ID,SqlDbType.BigInt, 8 );
// 賦上ID值
parm.Value=id;

using (SqlDataReadersdr=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項(xiàng)目

(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>
/// 文章內(nèi)容構(gòu)造函數(shù)
/// </summary>
/// <paramname="id"> 文章流水號(hào)ID </param>
/// <paramname="content"> 文章內(nèi)容 </param>
/// <paramname="title"> 文章標(biāo)題 </param>
/// <paramname="from"> 文章來源 </param>
/// <paramname="clsid"> 文章的分類屬性ID </param>
/// <paramname="tmpid"> 文章的模板屬性ID </param>
public ContentInfo( int id, string title, string content, string from ,DateTimeaddDate, 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 DateTimeAddDate
{
get { return _AddDate;}
}
public int ClsID
{
get { return _clsID;}
}
public int TmpID
{
get { return _tmpID;}
}



}
}

4、IDAL項(xiàng)目

(1)Icontent.cs

using System;
using Model;

namespace IDAL
{
/// <summary>
/// 文章內(nèi)容操作接口
/// </summary>
public interface IContent
{
/// <summary>
/// 取得文章的內(nèi)容。
/// </summary>
/// <paramname="id"> 文章的ID </param>
/// <returns></returns>
ContentInfoGetContentInfo( int id);
}
}

5、DALFactory項(xiàng)目

(1)Content.cs

using System;
using System.Reflection;
using System.Configuration;
using IDAL;

namespace DALFactory
{
/// <summary>
/// 工產(chǎn)模式實(shí)現(xiàn)文章接口。
/// </summary>
public class Content
{
public static IDAL.IContentCreate()
{
// 這里可以查看DAL接口類。
string path=System.Configuration.ConfigurationSettings.AppSettings[ " WebDAL " ].ToString();
string className=path+ " .Content " ;

// 用配置文件指定的類組合
return (IDAL.IContent)Assembly.Load(path).CreateInstance(className);
}
}
}

6、BLL項(xiàng)目

(1)Content.cs

using System;

using Model;
using IDAL;

namespace BLL
{
/// <summary>
/// Content的摘要說明。
/// </summary>
public class Content
{

public ContentInfoGetContentInfo( int id)
{

// 取得從數(shù)據(jù)訪問層取得一個(gè)文章內(nèi)容實(shí)例
IContentdal=DALFactory.Content.Create();

// 用DAL查找文章內(nèi)容
return dal.GetContentInfo(id);
}
}
}

7、Web項(xiàng)目

1、Web.config:

<appSettings>

<addkey= " SQLConnString " value= " DataSource=localhost;PersistSecurityinfo=True;InitialCatalog=newsDB;UserID=sa;Password= " />
<addkey= " WebDAL " value= " SQLServerDAL " />
</appSettings>

2、WebUI.aspx

<%@Pagelanguage= " c# " Codebehind= " WebUI.aspx.cs " AutoEventWireup= " false " Inherits= " Web.WebUI " %>
<!DOCTYPEHTMLPUBLIC " -//W3C//DTDHTML4.0Transitional//EN " >
<HTML>
<HEAD>
<title>WebUI</title>
<metaname= " GENERATOR " Content= " MicrosoftVisualStudio.NET7.1 " >
<metaname= " CODE_LANGUAGE " Content= " C# " >
<metaname= " vs_defaultClientScript " content= " JavaScript " >
<metaname= " vs_targetSchema " content= " http://schemas.microsoft.com/intellisense/ie5 " >
</HEAD>
<bodyMS_POSITIONING= " GridLayout " >
<formid= " Form1 " method= " post " runat= " server " >
<FONT " >宋體 " ></FONT>
<tablewidth= " 600 " border= " 1 " >
<tr>
<tdstyle= " WIDTH:173px " >&nbsp;</td>
<td>&nbsp;
<asp:Labelid= " lblTitle " runat= " server " ></asp:Label></td>
</tr>
<tr>
<tdstyle= " WIDTH:173px;HEIGHT:22px " >&nbsp;</td>
<tdstyle= " HEIGHT:22px " >&nbsp;
<asp:Labelid= " lblDataTime " runat= " server " ></asp:Label></td>
</tr>
<tr>
<tdstyle= " WIDTH:173px " >&nbsp;</td>
<td>&nbsp;
<asp:Labelid= " lblContent " runat= " server " ></asp:Label></td>
</tr>
<tr>
<tdstyle= " WIDTH:173px " >&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdstyle= " WIDTH:173px;HEIGHT:23px " >&nbsp;</td>
<tdstyle= " HEIGHT:23px " >&nbsp;</td>
</tr>
<tr>
<tdstyle= " WIDTH:173px " >&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdstyle= " WIDTH:173px " >&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdstyle= " WIDTH:173px " >&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdstyle= " WIDTH:173px " >&nbsp;</td>
<td>&nbsp;
<asp:Labelid= " lblMsg " runat= " server " >Label</asp:Label></td>
</tr>
</table>
</form>
</body>
</HTML>

3、WebUI.aspx.cs后臺(tái)調(diào)用顯示:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using BLL;
using Model;

namespace myWeb
{
/// <summary>
/// WebForm1的摘要說明。
/// </summary>
public class WebUI:System.Web.UI.Page
{
protected System.Web.UI.WebControls.LabellblTitle;
protected System.Web.UI.WebControls.LabellblDataTime;
protected System.Web.UI.WebControls.LabellblContent;
protected System.Web.UI.WebControls.LabellblMsg;

private ContentInfoci;


private void Page_Load( object sender,System.EventArgse)
{
if (!Page.IsPostBack)
{
GetContent( " 1 " );
}
}

private void GetContent( string id)
{
int ID=WebComponents.CleanString.GetInt(id);

Contentc= new Content();
ci=c.GetContentInfo(ID);
if (ci!= null )
{
this .lblTitle.Text=ci.Title;
this .lblDataTime.Text=ci.AddDate.ToString( " yyyy-MM-dd " );
this .lblContent.Text=ci.Content;
}
else
{
this .lblMsg.Text= " 沒有找到這篇文章 " ;
}
}

#region Web窗體設(shè)計(jì)器生成的代碼
override protected void OnInit(EventArgse)
{
//
// CODEGEN:該調(diào)用是ASP.NETWeb窗體設(shè)計(jì)器所必需的。
//
InitializeComponent();
base .OnInit(e);
}

/// <summary>
/// 設(shè)計(jì)器支持所需的方法-不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this .Load+= new System.EventHandler( this .Page_Load);

}
#endregion
}
}

4、WebComponents項(xiàng)目

(1)CleanString.cs

using System;
using System.Text;

namespace myWeb.WebComponents
{
/// <summary>
/// CleanString的摘要說明。
/// </summary>
public class CleanString
{

public static int GetInt( string inputString)
{
try
{
return Convert.ToInt32(inputString);
}
catch
{
return 0 ;
}

}


public static string InputText( string inputString, int maxLength)
{
StringBuilderretVal= new StringBuilder();

// checkincomingparametersfornullorblankstring
if ((inputString!= null )&&(inputString!=String.Empty))
{
inputString=inputString.Trim();

// chopthestringincasetheclient-sidemaxlength
// fieldsarebypassedtopreventbufferover-runs
if (inputString.Length>maxLength)
inputString=inputString.Substring( 0 ,maxLength);

// convertsomeharmfulsymbolsincasetheregular
// expressionvalidatorsarechanged
for ( int i= 0 ;i<inputString.Length;i++)
{
switch (inputString[i])
{
case ' " ' :
retVal.Append( " &quot; " );
break ;
case ' < ' :
retVal.Append( " &lt; " );
break ;
case ' > ' :
retVal.Append( " &gt; " );
break ;
default :
retVal.Append(inputString[i]);
break ;
}
}

// Replacesinglequoteswithwhitespace
retVal.Replace( " ' " , " " );
}

return retVal.ToString();

}

}
}

asp.net三層架構(gòu)詳解


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 欧美香蕉爽爽人人爽观看猫咪 | 99热久久国产精品免费观看 | 久久福利免费视频 | 亚洲性片| 国产成人午夜性视频影院 | 天天操比 | 老司机午夜在线视频免费观 | 欧美香蕉视频在线观看 | 亚洲一区在线视频观看 | 国产国语高清在线视频二区 | 成人精品第一区二区三区 | 欧美不卡在线视频 | 九色九色九色在线综合888 | 欧美性久久久久 | 久久午夜夜伦伦鲁鲁片 | 日韩一级大毛片欧美一级 | 亚洲午夜网| 成人在线免费网站 | 成人黄色一级视频 | 香蕉视频免费在线播放 | 高清欧美日本视频免费观看 | 毛片女人 | 中文精品久久久久国产网址 | 福利姬在线精品观看 | 护士日本xxxxx丰满hd4k | 中文字幕视频免费在线观看 | 一区二区三区欧美日韩 | 高清二区| 久久精品国产国产精品四凭 | 青青青国产在线视频 | 夜夜操影院 | 爱爱小视频在线观看网站 | 日韩在线一区视频 | 国产精品高清一区二区 | 日本永久免费 | 日本一区精品久久久久影院 | 国产亚洲精品久久久久久久网站 | 99久久免费国产香蕉麻豆 | 日本不卡一区二区三区 | 韩国精品一区二区久久 | 日本午夜影院 |