4-Tier Architecture in ASP.NET with C# - Pdf 66

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

{
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

{
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

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();
}
}

/// 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
{


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status