背景

  首先,我介紹一些3層架構(gòu)的 " /> 六月天综合网,日韩五码在线,精品精品导航

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

在C#中實(shí)現(xiàn)3層架構(gòu)

  這篇文章討論如何在c#中實(shí)現(xiàn)3層架構(gòu),使用MS Access數(shù)據(jù)庫存儲(chǔ)數(shù)據(jù)。在此,我在3層架構(gòu)中實(shí)現(xiàn)一個(gè)小型的可復(fù)用的組件保存客戶數(shù)據(jù)。并提供添加,更新,查找客戶數(shù)據(jù)的功能。

  背景

  首先,我介紹一些3層架構(gòu)的理論知識。簡單說明:什么是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ù)訪問被設(shè)計(jì)為獨(dú)立的模塊。主要有3個(gè)層面,第一層(表現(xiàn)層,GUI層),第二層(商業(yè)對象,商業(yè)邏輯層),第三層(數(shù)據(jù)訪問層)。這些層可以單獨(dú)開發(fā),單獨(dú)測試。

  為什么要把程序代碼分為3層。把用戶接口層,商業(yè)邏輯層,數(shù)據(jù)訪問層分離有許多的優(yōu)點(diǎn)。

  在快速開發(fā)中重用商業(yè)邏輯組件,我們已經(jīng)在系統(tǒng)中實(shí)現(xiàn)添加,更新,刪除,查找客戶數(shù)據(jù)的組件。這個(gè)組件已經(jīng)開發(fā)并且測試通過,我們可以在其他要保存客戶數(shù)據(jù)的項(xiàng)目中使用這個(gè)組件。

  系統(tǒng)比較容易遷移,商業(yè)邏輯層與數(shù)據(jù)訪問層是分離的,修改數(shù)據(jù)訪問層不會(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)用程序開發(fā)人員可以并行,獨(dú)立的開發(fā)單獨(dú)的層。

  代碼

  這個(gè)組件有3層,第一個(gè)層或者稱為GUI層用form實(shí)現(xiàn),叫做FrmGUI。第二層或者稱為商業(yè)邏輯層,叫做BOCustomer,是Bussniess Object Customer的縮寫。最后是第三層或者稱為數(shù)據(jù)層,叫做DACustomer,是Data Access Customer的縮寫。為了方便,我把三個(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)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 黑龙江省| 泽州县| 咸宁市| 咸宁市| 肥东县| 长宁区| 洞口县| 若尔盖县| 芮城县| 将乐县| 平邑县| 江门市| 来凤县| 翼城县| 宿迁市| 寿阳县| 乌苏市| 湘潭市| 株洲县| 栾城县| 广汉市| 锦州市| 汝南县| 景谷| 醴陵市| 正安县| 永济市| 芦溪县| 西乡县| 安庆市| 和龙市| 乐山市| 邵阳县| 常德市| 青州市| 哈尔滨市| 延津县| 射阳县| 渭源县| 道孚县| 钟山县|