這篇文章討論如何在c#中實現
背景
首先
什麼是
為什麼要把程序代碼分為
在快速開發中重用商業邏輯組件
系統比較容易遷移
系統容易修改
應用程序開發人員可以並行
代碼
這個組件有
用戶接口層
下面是用戶接口成的一段代碼
//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
{
try
{
cus = new BOCustomer();
cus
cus
cus
cus
cus
cus
}
catch(Exception err)
{
MessageBox
}
//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
//the content of the dataset and fills the controls
private void cmdFind_Click(object sender
{
try
{
String cusID = txtID
BOCustomer thisCus = new BOCustomer();
DataSet ds = thisCus
DataRow row;
row = ds
//via looping
foreach(DataRow rows in ds
{
txtFName
txtLName
txtAddress
txtTel
}
}
catch (Exception err)
{
MessageBox
//this function used to update the customer details
private void cmdUpdate_Click(object sender
{
try
{
cus = new BOCustomer();
cus
cus
cus
cus
cus
cus
}
catch(Exception err)
{
MessageBox
}
}
商業邏輯層
下面是商業邏輯層的所有代碼
商業邏輯層是一個中間層
using System;
using System
namespace _
{
///
/// Summary description for BOCustomer
///
public class BOCustomer
{
//Customer properties
private String fName;
private String lName;
private String cusId;
private String address;
private String tel;
private DACustomer cusData;
public BOCustomer()
{
//An instance of the Data access layer!
cusData = new DACustomer();
}
///
/// Property FirstName (String)
///
public String FName
{
get
{
return this
}
set
{
try
{
this
if (this
{
throw new Exception(
}
}
catch(Exception e)
{
throw new Exception(e
}
}
}
///
/// Property LastName (String)
///
public String LName
{
get
{
return this
}
set
{
//could be more checkings here eg revmove
//change to proper case
//blah blah
this
if (this
{
throw new Exception(
}
}
}
///
/// Property Customer ID (String)
///
public String cusID
{
get
{
return this
}
set
{
this
if (this
{
throw new Exception(
}
}
}
///
/// Property Address (String)
///
public String Address
{
get
{
return this
}
set
{
this
if (this
{
throw new Exception(
}
}
}
///
/// Property Telephone (String)
///
public String Tel
{
get
{
return this
}
set
{
this
if (this
{
throw new Exception(
}
}
///
/// Function Add new customer
/// the function in Data layer
///
public void Add()
{
cusData
}
///
/// Function Update customer details
/// Calls the function in Data layer
///
public void Update()
{
cusData
}
///
/// Function Find customer
/// function in Data layer
/// It returns the details of the customer using
/// customer ID via a Dataset to GUI tier
///
public DataSet Find(String str)
{
if (str ==
throw new Exception(
DataSet data = null;
data = cusData
return data;
}
}
數據訪問層
數據層包括處理MS Access數據庫的細節
using System;
using System
using System
namespace _
{
///
/// Summary description for DACustomer
///
public class DACustomer
{
private OleDbConnection cnn;
//change connection string as per the
//folder you unzip the files
private const string CnnStr =
//local variables
private String strTable=
private String strFields=
private String strValues=
private String insertStr=
//this needs to be changed based on customer
//table fields
private const String thisTable =
private const String cus_ID =
private const String cus_LName =
private const String cus_FName =
private const String cus_Tel =
private const String cus_Address =
public DACustomer()
{
public DACustomer(BOCustomer cus)
{
// A reference of the business object class
}
//standard dataset function that adds a new customer
public void Add(BOCustomer cus)
{
String str = BuildAddString(cus);
OpenCnn();
//Open command option
OleDbCommand cmd = new OleDbCommand(str
//execute connection
cmd
// close connection
CloseCnn();
}
//standard dataset function that updates
//details of a customer based on ID
public void Update(BOCustomer cus)
{
OpenCnn();
String selectStr =
OleDbCommand cmd = new OleDbCommand(selectStr
cmd
CloseCnn();
}
//standard dataset function that finds and
//return the detail of a customer in a dataset
public DataSet Find(String argStr)
{
DataSet ds=null;
try
{
OpenCnn();
String selectStr =
OleDbDataAdapter da =
new OleDbDataAdapter(selectStr
ds = new DataSet();
da
CloseCnn();
}
catch(Exception e)
{
String Str = e
}
return ds;
}
private void OpenCnn()
{
// initialise connection
String cnnStr = CnnStr;
cnn = new OleDbConnection(cnnStr);
// open connection
cnn
}
private void CloseCnn()
{
//
cnn
}
// just a supporting function that builds
// and return the insert string for dataset
private String BuildAddString(BOCustomer cus)
{
// these are the constants as
// set in the top of this module
strTable=
strFields=
//these are the attributes of the
//customer business object
strValues=
insertStr = strTable + strFields + strValues;
return insertStr;
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/12878.html