這篇文章討論如何在c#中實(shí)現(xiàn)3層架構(gòu),使用MS Access數(shù)據(jù)庫(kù)存儲(chǔ)數(shù)據(jù)。在此,我在3層架構(gòu)中實(shí)現(xiàn)一個(gè)小型的可復(fù)用的組件保存客戶數(shù)據(jù)。并提供添加,更新,查找客戶數(shù)據(jù)的功能。
背景
首先,我介紹一些3層架構(gòu)的理論知識(shí)。簡(jiǎn)單說(shuō)明:什么是3層架構(gòu)?3層架構(gòu)的優(yōu)點(diǎn)是什么?
什么是3層架構(gòu)?
3層架構(gòu)是一種“客戶端-服務(wù)器”架構(gòu),在此架構(gòu)中用戶接口,商業(yè)邏輯,數(shù)據(jù)保存以及數(shù)據(jù)訪問(wèn)被設(shè)計(jì)為獨(dú)立的模塊。主要有3個(gè)層面,第一層(表現(xiàn)層,GUI層),第二層(商業(yè)對(duì)象,商業(yè)邏輯層),第三層(數(shù)據(jù)訪問(wèn)層)。這些層可以單獨(dú)開(kāi)發(fā),單獨(dú)測(cè)試。
為什么要把程序代碼分為3層。把用戶接口層,商業(yè)邏輯層,數(shù)據(jù)訪問(wèn)層分離有許多的優(yōu)點(diǎn)。
在快速開(kāi)發(fā)中重用商業(yè)邏輯組件,我們已經(jīng)在系統(tǒng)中實(shí)現(xiàn)添加,更新,刪除,查找客戶數(shù)據(jù)的組件。這個(gè)組件已經(jīng)開(kāi)發(fā)并且測(cè)試通過(guò),我們可以在其他要保存客戶數(shù)據(jù)的項(xiàng)目中使用這個(gè)組件。
系統(tǒng)比較容易遷移,商業(yè)邏輯層與數(shù)據(jù)訪問(wèn)層是分離的,修改數(shù)據(jù)訪問(wèn)層不會(huì)影響到商業(yè)邏輯層。系統(tǒng)如果從用SQL Server存儲(chǔ)數(shù)據(jù)遷移到用Oracle存儲(chǔ)數(shù)據(jù),并不需要修改商業(yè)邏輯層組件和GUI組件
系統(tǒng)容易修改,假如在商業(yè)層有一個(gè)小小的修改,我們不需要在用戶的機(jī)器上重裝整個(gè)系統(tǒng)。我們只需要更新商業(yè)邏輯組件就可以了。
應(yīng)用程序開(kāi)發(fā)人員可以并行,獨(dú)立的開(kāi)發(fā)單獨(dú)的層。
代碼
這個(gè)組件有3層,第一個(gè)層或者稱(chēng)為GUI層用form實(shí)現(xiàn),叫做FrmGUI。第二層或者稱(chēng)為商業(yè)邏輯層,叫做BOCustomer,是Bussniess Object Customer的縮寫(xiě)。最后是第三層或者稱(chēng)為數(shù)據(jù)層,叫做DACustomer,是Data Access Customer的縮寫(xiě)。為了方便,我把三個(gè)層編譯到一個(gè)項(xiàng)目中。
用戶接口層
下面是用戶接口成的一段代碼,我只選取了調(diào)用商業(yè)邏輯層的一部分代碼。
//This function get the details from the user via GUI
//tier and calls the Add method of business logic layer.
private void cmdAdd_Click(object sender, System.EventArgs e)
{
try
{
cus = new BOCustomer();
cus.cusID=txtID.Text.ToString();
cus.LName = txtLName.Text.ToString();
cus.FName = txtFName.Text.ToString();
cus.Tel= txtTel.Text.ToString();
cus.Address = txtAddress.Text.ToString();
cus.Add();
}
catch(Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
//This function gets the ID from the user and finds the
//customer details and return the details in the form of
//a dataset via busniss object layer. Then it loops through
//the content of the dataset and fills the controls.
private void cmdFind_Click(object sender, System.EventArgs e)
{
try
{
String cusID = txtID.Text.ToString();
BOCustomer thisCus = new BOCustomer();
DataSet ds = thisCus.Find(cusID);
DataRow row;
row = ds.Tables[0].Rows[0];
//via looping
foreach(DataRow rows in ds.Tables[0].Rows )
{
txtFName.Text = rows["CUS_F_NAME"].ToString();
txtLName.Text = rows["CUS_L_NAME"].ToString();
txtAddress.Text = rows["CUS_ADDRESS"].ToString();
txtTel.Text = rows["CUS_TEL"].ToString();
}
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
//this function used to update the customer details.
private void cmdUpdate_Click(object sender, System.EventArgs e)
{
try
{
cus = new BOCustomer();
cus.cusID=txtID.Text.ToString();
cus.LName = txtLName.Text.ToString();
cus.FName = txtFName.Text.ToString();
cus.Tel= txtTel.Text.ToString();
cus.Address = txtAddress.Text.ToString();
cus.Update();
}
catch(Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
NET技術(shù):在C#中實(shí)現(xiàn)3層架構(gòu),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。