3-Tier architecture generally contains UI or Presentation Layer, Business Access Layer (BAL) or Business Logic Layer and Data Access Layer (DAL).
Presentation Layer : Presentation layer cotains pages like .aspx or windows form where data is presented to the user or input is taken from the user.
Business Access Layer (BAL) or Business Logic Layer BAL contains business logic, validations or calculations related with the data, if needed. I will call it Business Access Layer in my demo.
Data Access Layer (DAL): DAL contains methods that helps business layer to connect the data and perform required action, might be returning data or manipulating data (insert, update, delete etc). For this demo application, I have taken a very simple example. I am assuming that I have to play with record of persons (FirstName, LastName, Age) and I will refer only these data through out this article.
example code:
[I have performed insert alone in following coding:]
Data Access Layer (DAL): public class PersonDAL3 { string connStr = ConfigurationManager.ConnectionStrings["conn"].ToString();
public PersonDAL() {
} public int Insert(string firstName, string lastName, int age) { SqlConnection conn = new SqlConnection(connStr); conn.Open(); SqlCommand dCmd = new SqlCommand("InsertData", conn); dCmd.CommandType = CommandType.StoredProcedure; try { dCmd.Parameters.AddWithValue("@firstName", firstName); dCmd.Parameters.AddWithValue("@lastName", lastName); dCmd.Parameters.AddWithValue("@age", age); return dCmd.ExecuteNonQuery(); } catch { throw; } finally { dCmd.Dispose(); conn.Close(); conn.Dispose(); } } }
Business Access Layer : public class PersonBAL { public PersonBAL3() {
}
public int Insert(string firstName, string lastName, int age) { PersonDAL pDAL = new PersonDAL(); try { return pDAL.Insert(firstName, lastName, age); } catch { throw; } finally { pDAL = null; } } } Presentation layer:
in aspx:
Add Records |
|---|
First Name: | | Display="dynamic"> | Last Name: | | Display="dynamic"> | Age: | | Display="dynamic"> Operator="DataTypeCheck" Type="Integer"> | | |
in aspx.cs: protected void AddRecords(object sender, EventArgs e) { //Lets validate the page first if (!Page.IsValid) return;
int intResult = 0; PersonBAL pBAL = new PersonBAL3(); // Instantiate the object we have to deal with string firstName = txtFirstName.Text; string lastName = txtLastName.Text; int age = Int32.Parse(txtAge.Text); try { intResult = pBAL.Insert(firstName, lastName, age); if (intResult > 0) lblMessage.Text = "New record inserted successfully."; else lblMessage.Text = "FirstName ["+ txtFirstName.Text +"] alredy exists, try another name";
} catch (Exception ee) { lblMessage.Text = ee.Message.ToString(); } finally { pBAL = null; } }
|
| Author: Kapil Dhawan 17 Jun 2008 | Member Level: Gold Points : 2 |
Hello Nice piece of code Thanks for sharing your knowledge with us. I hope to see more good code from your side This code is going to help lots of guys. Ton Thanks to you Regards, Kapil
|
| Author: Olufemi Hunjenukon 11 Jul 2008 | Member Level: Bronze Points : 1 |
Thanks, This is a good piece of information that impact the information I have been looking for sometime now.
Olufemi Nigeria
|
| Author: Bindu Bujji 12 Jul 2008 | Member Level: Gold Points : 1 |
Thanks for good information on 3-Tier Architecture.... I appreciate your time and helping others to learn this concept with good sample code.
Thanks Bindu
|
| Author: Muralee Krishnan.K 14 Jul 2008 | Member Level: Gold Points : 1 |
Hi Kumar Velu,
Thank you for sharing your knowledge with other. I is very useful for those who don't know much about 3 tier.
Thank Muraleekrishnan
|