Three Tier Architecture in ASP.Net

As we are in development we should know the proper architecture of application programming. Why we should three tiers architecture is we should must understand few points:
1. The main purpose of using three tier architecture is to become application easier and it can be developed by different group of developers (Front End, Business Logic and Data Access Layer).
2. It is also feasible to maintain our code without affecting other tier of codes.
3. We can use code in different application, so code reusability is also beneficial for programmer to develop again and again the same code.
4. It is also make secure application while practicing three tier architecture.
You should the main three layers involved into three tier architecture:
1. Presentation Layer (PL) : or GUI is called as presentation layer (1st Layer), the code written into the .aspx and .cs or .vb is considered as Presentation Layer.
2. Business Logic (BL): the business logic assumed as the code written with respect to presentation layer in .cs (class file). Here we write the logical code against the presentation Layer.
3. Data Access Layer (DAL): here we maintain the code to connect with our database and maintain our command arguments, DataTable Objects etc.
Here is example:
Presentation Layer (PL): Default.aspx.cs
On Button click event :
protected void btnLogin_Click(object sender, EventArgs e)
{
strUserName = txtUserName.Text;
strPass = txtPass.Text;
BL objBL = new BL();
intChk = objBL.ValidUser(strUserName, strPass);
if(intChk > 0)
{
lblMsg.Text=”You are a valid user”;
}
Else
{
lblMsg.Text=”Sorry !”;
}
}

Business Logic (BL):
Public int ValidUser(string strUserName, string strPass)
{
//here initialize Data Access Layer
DAL objDAL = new DAL();
DataTable dt = objDAL.getDataTable (“SELECT userId, username, password FROM mUser WHERE username = ‘”+strUserName+”’ AND password = ‘”+strPass+”’ ”);
If(dt.Rows.Count > 0)
{
return 1;
}
else
{
return 0;
}
}
Data Access Layer:
SqlConnection dbCon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connString"].ToString());
public DataTable getDataTable(string strQry)
{
try
{
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQry;
cmd.Connection = dbCon;
dbCon.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dt.Dispose();
da.Dispose();
cmd.Dispose();
dbCon.Close();
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
Note : how we have implemented the Layer is from PL to BL and BL to DAL.

Comments