In this post, I will show how to create 3 -tier architecture in asp.net.Before going to technical details, I will describe some details about 3 tier architecture and the main components of 3 tier architecture.
The three-tier architecture was comes into existence to improve management of code and contents and to improve the performance of the web based applications.There are mainly three layers in three-tier architecture.{alertInfo}
Why Layered architecture?
Having a layered architecture in place will make it easier to add new features or change existing ones. It is much more difficult to add a new use case to the system or extend the business rules on a specific domain object if the process or business logic is spread throughout the code. {alertInfo}
ASP.NET already provide the functionality to separate code from view, but it's not enough to create an extendable software.
These are defined as follows…
-
UI(InsertCustomer.aspx): First layer Presentation contains mainly the interface code, and this is shown to the user. This code could contain any technology that can be used on the client-side like HTML, JavaScript or VBScript etc.
-
Business Logic(CustomerBL.cs): The second layer is Business Logic which contains all the server-side code. This layer has code to interact with the database and query, manipulate, pass data to the user interface, and handle any input from the UI.
-
DataAccess Layer(CustomerDAL.cs): Third layer Data represents the data store like MS Access, SQL Server, an XML file, an Excel file or even a text file containing data also, some additional database is also added to that layers.
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;
}