看了2天的三層架構,其實自己以前也看過這個,可以由于沒有使用過,所以對于三層架構也只是知道罷了,昨天看了一下他一些內(nèi)部的具體架構,三層分別是BLL、WEB、DAL,在web接受用戶請求,bll處理業(yè)務,dal處理數(shù)據(jù)庫事務,下面是一個簡單的例子!這是一個添加新員工的頁面:
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Text;
usingMaticsoft.Common;
usingLTP.Accounts.Bus;
usingSystem;
namespaceMaticsoft.Web.Employees
{
publicpartialclassMYAdd:Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
protectedvoidButtonOK_Click(objectsender,EventArgse)
{
stringERRM ="";
if(!PageValidate.IsNumber(Temployeeid.Text.Trim()))
{
ERRM +="輸入的id不是數(shù)字,不合法!";
}
if(this.Temployeeid.Text.Trim().Length ==0)
{
ERRM +="用戶id不能為空!";
}
if(ERRM !="")
{
MessageBox.Show(this, ERRM);
ERRM ="";
return;
} intEmployeeID =int.Parse(this.Temployeeid.Text); /
stringEmployeesName =this.Temployeename.Text;
Maticsoft.Model.Employeesmodel =newModel.Employees(); //新建一個在dal層Employees的model對象,用于對數(shù)據(jù)庫Employees表的數(shù)據(jù)處理
model.EmployeeID = EmployeeID; //將text里面用戶輸入的id數(shù)據(jù)傳給model
model.EmployeesName = EmployeesName; //將text里面用戶輸入的name數(shù)據(jù)傳給model
Maticsoft.BLL.Employeesbll =newBLL.Employees(); //新建一個在bll層的employees對象,用于調(diào)用dal層的model對象
bll.Add(model); //在bll層調(diào)用add方法添加一條數(shù)據(jù)
////////////////////////////////////// bll層的add 方法 ///////////////////////////////////////////////////////////////////////////
/// 增加一條數(shù)據(jù)
publicboolAdd(Maticsoft.Model.Employeesmodel)
{
returndal.Add(model); //調(diào)用dal層的add方法添加一條數(shù)據(jù)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// dal層的 add 方法 ///////////////////////////////////////////////////////////////////////////
///增加一數(shù)據(jù)
publicboolAdd(Maticsoft.Model.Employeesmodel)
{
StringBuilderstrSql=newStringBuilder();
strSql.Append("insert into Employees(");
strSql.Append("EmployeeID,EmployeesName)");
strSql.Append(" values (");
strSql.Append("@EmployeeID,@EmployeesName)");
SqlParameter[] parameters = {
newSqlParameter("@EmployeeID",SqlDbType.Int,4),
newSqlParameter("@EmployeesName",SqlDbType.VarChar,90)};
parameters[0].Value = model.EmployeeID;
parameters[1].Value = model.EmployeesName;
introws=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
if(rows >0)
{
returntrue;
}
else
{
returnfalse;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Maticsoft.Common.MessageBox.ShowAndRedirect(this,"保存成功|!","MYAdd.aspx"); //成功創(chuàng)建后彈出對話框,并跳轉(zhuǎn)到當前頁面!
}
protectedvoidButtonNo_Click(objectsender,EventArgse)
{
Response.Redirect("list.aspx");
}
}
}