4-Tier Architecture in ASP.NET with C#
I am using 3-Tier architecture in my different projects, but adding a 4th tier is a novelty for me.
After reading this article, I can see the benefits to add a Business Access Layer in some complex scenarios.
From the author:
Almost all of us must have heard about 3-Tier architecture but what is this 4-Tier architecture? What are the
benefits and how it is different from other architectures?
Well, the architecture I am going to demonstrate here is just enhancement of 3-Tier archicture. In this architecture;
you no need of writing long function parameters throughout the layers (as in traditionally 3-Tier archicture has to)
and the actual objects of the application will be in a separate tier so that in future you can separately use these
objects for enhancements. Change in the object definition can be done without touching the entire Business Access
Layers ............
Let me explain you step-wise process of creatioin of 4-Tier architecture application.
In this application, I am going to take example of a Person that will have 3 properties: FirstName, LastName, Age.
We will create a separate pages to insert these records (default.aspx) into database and list,update,delete records
(list.aspx) from database.
In this application we will have following 4-Tiers
1. Business Object [BO]
2. Business Access Layer [BAL]
3. Data Access Layer [DAL]
4. UI (4-Tier) folder [UI]
Well, the architecture I am going to demonstrate here is just enhancement of 3-Tier archicture. In this architecture;
you no need of writing long function parameters throughout the layers (as in traditionally 3-Tier archicture has to)
and the actual objects of the application will be in a separate tier so that in future you can separately use these
objects for enhancements. Change in the object definition can be done without touching the entire Business Access
Layers ............
Let me explain you step-wise process of creatioin of 4-Tier architecture application.
In this application, I am going to take example of a Person that will have 3 properties: FirstName, LastName, Age.
We will create a separate pages to insert these records (default.aspx) into database and list,update,delete records
(list.aspx) from database.
In this application we will have following 4-Tiers
1. Business Object [BO]
2. Business Access Layer [BAL]
3. Data Access Layer [DAL]
4. UI (4-Tier) folder [UI]
Picture - 1 (Solution Explorer)
For simplicity reason, I have created separate folders for first 3-tiers into App_Code folder. You can create a
separate projects for these tiers and add into forth tier (UI) solution.
Lets create above tiers one by one.
Business Object [BO - Person.cs]
Create a separate folder by right-clicking App_Code folder and name it as BO. Right click this folder and create a
new .cs (Class) file named Person.cs. Write following code inside it.
- Hide Code
int m_PersonID = 0;
string m_FirstName = string.Empty;
string m_LastName = string.Empty;
int m_Age = 0;
#region Propertiers
public int PersonID
{
get { return m_PersonID; }
set { m_PersonID = value; }
}
public string FirstName
{
get { return m_FirstName; }
set { m_FirstName = value; }
}
public string LastName
{
get { return m_LastName; }
set { m_LastName = value; }
}
public int Age
{
get { return m_Age; }
set { m_Age = value; }
}
#endregion Properties
Here, we are first declaring 4 variables for corresponding properites and defining properties for them.This is your
Business Object with all its properties/attributes to work with. Next step is to create Data Access Layer.
Data Access Layer [DAL - PersonDAL.cs]
The way you created BO folder inside App_Code folder, create another folder named DAL. Create a .cs file inside it
and name it as PersonDAL.cs
Write following code inside it (You can copy-paste).
- Hide Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Summary description for PersonDAL
/// </summary>
public class PersonDAL
{
string connStr = ConfigurationManager.ConnectionStrings["TutTestConn"].ToString();
public PersonDAL()
{
}
/// <summary>
/// Used to insert records into database
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public int Insert(Person person)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand dCmd = new SqlCommand("InsertData", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@firstName", person.FirstName);
dCmd.Parameters.AddWithValue("@lastName", person.LastName);
dCmd.Parameters.AddWithValue("@age", person.Age);
return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// Update record into database
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public int Update(Person person)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand dCmd = new SqlCommand("UpdateData", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@firstName", person.FirstName);
dCmd.Parameters.AddWithValue("@lastName", person.LastName);
dCmd.Parameters.AddWithValue("@age", person.Age);
dCmd.Parameters.AddWithValue("@personID", person.PersonID);
return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// Load all records from database
/// </summary>
/// <returns></returns>
public DataTable Load()
{
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter dAd = new SqlDataAdapter("LoadAll", conn);
dAd.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet dSet = new DataSet();
try
{
dAd.Fill(dSet, "PersonTable");
return dSet.Tables["PersonTable"];
}
catch
{
throw;
}
finally
{
dSet.Dispose();
dAd.Dispose();
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// Delete record from database
/// </summary>
/// <param name="person"></param>
/// <returns></returns>
public int Delete(Person person)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand dCmd = new SqlCommand("DeleteData", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@personID", person.PersonID);
return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
}
In this class file, we have Insert, Update, Delete, Load methods. In this class file, first I am getting the connection
string from the web.config file in a class level variable called connStr and using the same string in all my methods to
open the connection.
For simplicity reason, I have not shown the code for Stored Procedure, however you can get the
complete database and code by downloading the Source Code files. This was your Data Access Layer. Till
now you have your Business Object and Data Access Layer ready. Now lets go to the third layer and create Business
Access Layer.
Business Access Layer [BAL - PersonBAL.cs]
Again, right click App_Code folder and add a new folder named BAL. Create a new class file inside it and name it as
PersonBAL.cs. Write following code inside it.
- Hide Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for PersonBAL
/// </summary>
public class PersonBAL
{
public PersonBAL()
{
}
/// <summary>
/// insert records into database
/// </summary>
/// <param name="person"></param>
/// <returns></returns>
public int Insert(Person person)
{
PersonDAL pDAL = new PersonDAL();
try
{
return pDAL.Insert(person);
}
catch
{
throw;
}
finally
{
pDAL = null;
}
}
/// <summary>
/// Update records into database
/// </summary>
/// <param name="person"></param>
/// <returns></returns>
public int Update(Person person)
{
PersonDAL pDAL = new PersonDAL();
try
{
return pDAL.Update(person);
}
catch
{
throw;
}
finally
{
pDAL = null;
}
}
/// <summary>
/// Load records from database
/// </summary>
/// <returns></returns>
public DataTable Load()