摘要:[ASP.NET]3-Tier Architecture in ASP.NET 實作版
在一個Solution中實現3-tier architecture,所以建立了ExamLoginBO.cs, ExamLoginBLL.cs, ExamLoginDAL.cs 三個類別檔跟一個Web Form ExamLogin.aspx
1. Presentation Layer,網頁UI,按下Button後進資料庫中確認使用者身分 ExamLogin.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
Boolean ck =false;
//3-tier architecture
//1.Bulid entity cross presentation, Business logic, data acess layer
//create ExamLoginBO.cs
ExamLoginBO bo = new ExamLoginBO();
//set entity value form web Control UI
bo.Name = TextBox1.Text;
bo.TestID = DropDownList1.SelectedValue;
//2.Using Business logic layer method with entity
//create ExamLoginBLL.cs
ck = ExamLoginBLL.CheckUserTestAuthor(bo);
}
2.Create Entity, 把要存取的參數拉出來建立屬性 ExamLoginBO.cs
public string _Name;
public string _Testid;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public string TestID
{
get { return _Testid; }
set { _Testid = value; }
}
3. Business Logic Layer, 判斷使用權限後做一些卡控或管理 ExamLoginBLL.cs
public static bool CheckUserTestAuthor(ExamLoginBO bo)
{
bool ck = false;
if (string.IsNullOrEmpty(bo.TestID) && !string.IsNullOrEmpty(bo.Name))
{
ck = true;
}
else
{
//Using acess data base layer and deal with returning result to do if else conditional statements
//create ExamLoginDAL.cs
if (ExamLoginDAL.chkDB(bo))
{
ck = true;
}
else { ck = false; }
}
return ck;
}//end CheckUserAuthor
4. Data Acess Layer, 連線資料庫的ADO.NET Insert, Select, Delete ExamLoginDAL.cs
public static bool chkDB(ExamLoginBO bo) {
string strConn = ConfigurationManager.ConnectionStrings["FAB51HRMS"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(strConn);
String strSQL = @"SELECT * FROM [HRMS].[dbo].[zHrms_N_EXAM_ANS_HIST]
where EMP_ID=@userid and CERT_KEY = @testid";
SqlDataReader dr = null;
SqlCommand command = new SqlCommand(strSQL, sqlConn);
//command.CommandText = strSQL;
//command.Connection = sqlConn;
command.Parameters.AddWithValue("@userid", bo.Name);
command.Parameters.AddWithValue("@testid", bo.TestID);
try
{
sqlConn.Open();
dr = command.ExecuteReader();
if (dr.HasRows)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
finally
{
if (dr != null)
{
command.Cancel();
dr.Close();
}
if (sqlConn.State == ConnectionState.Open)
{
sqlConn.Close();
}
}
}