The three-tier architecture has evolved to enhance code management, content organization, and overall performance in web-based applications. This architectural pattern divides the application into three distinct layers: Presentation (UI), Business Logic, and Data Access. This separation allows for better scalability, maintainability, and ease of adding new features.
Why Layered Architecture?
Layered architecture simplifies the process of adding or modifying features. It avoids scattering the process or business logic throughout the codebase, making it more challenging to extend functionalities. ASP.NET provides the means to separate code from the view, but for creating truly extensible software, a more structured approach is needed.
Main Components of 3-Tier Architecture:
-
UI (InsertCustomer.aspx):
- The Presentation layer primarily contains interface code visible to users.
- This code can utilize client-side technologies like HTML, JavaScript, or VBScript.
-
Business Logic (CustomerBL.cs):
- The Business Logic layer resides on the server and handles interactions with the database.
- It manages data manipulation, queries, and communication with the UI.
-
Data Access Layer (CustomerDAL.cs):
- The Data Access layer represents the data store, such as databases (e.g., SQL Server, MS Access), XML, Excel, or text files.
- Additional database-related operations are performed in this layer.
These are defined as follows.
Step-by-Step Guide:
Let’s start creating the project step by step.
-
Create a new website in visual studio and add two folders inside App_Code named BL and DAL (In the real scenario, you can create separate projects for BAL, DAL and UI )
-
Now, Create a class named CustomerDAL into App_Code->DAL by right-clicking it and write the following code into it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// Summary description for CustomerDAL
/// </summary>
public class CustomerDAL
{
public static void AddCustomer(CustomerBL cs)
{
try
{
using (SqlConnection con = new SqlConnection(SQLHelper.GetConnectionString()))
{
SqlParameter[] par = new SqlParameter[7];
par[0] = new SqlParameter("@ID", cs.ID);
par[0].Direction = ParameterDirection.Output;
par[1] = new SqlParameter("@name", cs.Name);
par[2] = new SqlParameter("@address", cs.Address);
par[3] = new SqlParameter("@phoneNumber", cs.PhoneNumber);
par[5] = new SqlParameter("@email", cs.Email);
int rowNo = SQLHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "proc_InsertCustomer", par);
cs.ID = Convert.ToInt32(par[0].Value);
}
}
catch (Exception ex)
{
throw;
}
}
public static void DeleteCustomer(CustomerBL customer)
{
try
{
using (SqlConnection con = new SqlConnection(SQLHelper.GetConnectionString()))
{
SqlParameter[] par = new SqlParameter[1];
par[0] = new SqlParameter("@ID", customer.ID);
int rowNo = SQLHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "proc_DeleteCustomer", par);
}
}
catch (Exception ex)
{
throw;
}
}
public static void GetCustomer(CustomerBL customer)
{
try
{
using (SqlConnection con = new SqlConnection(SQLHelper.GetConnectionString()))
{
SqlParameter[] par = new SqlParameter[1];
par[0] = new SqlParameter("@ID", customer.ID);
using (SqlDataReader dr = SQLHelper.ExecuteReader(con, CommandType.StoredProcedure, "OMS_Customer_SelectByCustomerID", par))
{
while (dr.Read())
{
customer.Name = SQLHelper.CheckStringNull(dr["Name"]);
customer.PhoneNumber = SQLHelper.CheckStringNull(dr["PhoneNo"]);
customer.Address = SQLHelper.CheckStringNull(dr["Address"]);
customer.ID = SQLHelper.CheckIntNull(dr["ID"]);
customer.Email = SQLHelper.CheckStringNull(dr["Email"]);
}
}
}
}
catch (Exception ex)
{
throw;
}
}
public static List<CustomerBL> GetAllCustomers()
{
try
{
List<CustomerBL> cuList = new List<CustomerBL>();
using (SqlConnection con = new SqlConnection(SQLHelper.GetConnectionString()))
{
using (SqlDataReader dr = SQLHelper.ExecuteReader(con, CommandType.StoredProcedure, "proc_SelectAllCustomer"))
{
while (dr.Read())
{
CustomerBL customer = new CustomerBL();
customer.Name = SQLHelper.CheckStringNull(dr["Name"]);
customer.PhoneNumber = SQLHelper.CheckStringNull(dr["Phone"]);
customer.Address = SQLHelper.CheckStringNull(dr["Address"]);
customer.ID = SQLHelper.CheckIntNull(dr["ID"]);
cuList.Add(customer);
}
}
}
return cuList;
}
catch (Exception ex)
{
throw;
}
}
}
- Now, Create a class named CustomerBL into App_Code->BL by right-clicking it and write the following code into it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for CustomerBL
/// </summary>
public class CustomerBL
{
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
public List<CustomerBL> Customers { get; set; }
public void Add()
{
CustomerDAL.AddCustomer(this);
}
public void Delete()
{
CustomerDAL.DeleteCustomer(this);
}
public void GetAll()
{
this.Customers = CustomerDAL.GetAllCustomers();
}
}
- Now, Create a Page named InsertCustomer.aspx and add the following code to it.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InsertCustomer.aspx.cs" Inherits="InsertCustomer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<table class="style1">
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Address
</td>
<td>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Phone
</td>
<td>
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Button" />
</td>
</tr>
</table>
</form>
</body>
</html>
And add the following code inside the click event of the button.
protected void btnInsert_Click(object sender, EventArgs e)
{
try
{
CustomerBL customer = new CustomerBL();
customer.Name = txtName.Text;
customer.PhoneNumber = txtAddress.Text;
customer.Email = txtPhone.Text;
customer.Address = txtEmail.Text;
customer.Add();
}
catch (Exception)
{
throw;
}
Conclusion:
This step-by-step guide demonstrates how to implement a 3-tier architecture in ASP.NET, promoting code organization and scalability. By following these principles, you can create more maintainable and extensible web applications.
Your articles always generate interesting database. That means your writing is interesting to people.thanks for sharing .please visit our website you got more information about diploma courses Diploma Courses in Ambernath
ردحذف