There are mainly three layers in 3 Tier Architecture But We can separate more than 3 tier as requirement.So now see as given below:-
- Presentation Layer (UI Layer)
- Business Access Layer (BAL)
- Data Access Layer (DAL)
1.) Presentation Layer(UI) :-
Presentation Layer is nothing but it is a user interface which every user see on our computer ,mobile and window screen. We can say,designing part of any application is known as Presentation Layer. The User can post input and get output on our presentation Layer only.In asp.net .axpx file is known as a presentation layer i.e mainly use for view purpose.
2.) Business Access Layer(BAL) :-
Business Access Layer is act as mediator Layer between Presentation layer and Data Access layer. or We can say business layer is communication between presentation layer and data access layer. This layer is used to transfer the data between Presentation Layer and Data Access Layer. This layer is mainly used for Validations and calculations purpose. Every validations and calculations of data are held on that layer only. I have also implemented Property layer or Entity Layer concepts in Business Access Layer. It is optional layer if we are working on a small projects.But if we are working on large projects then we have to include this layer in your 3 Tier Architecture Applications.It is used to enhance the security and prevent to brokering the application.
3.) Data Access Layer (DAL) :-
This Layer only communicate with Business Access Layer. Data Access Layer contains the methods that helps Business Access Layer.Business layer class's methods call the Data Access Layer Class methods to perform some required action with database such as insertion, deletion, updation etc. All database related connection codes are written in this layer only such as sql query ,stored procedure etc.
Note: We can also use Business Entity Layer which defined to all entities or properties.
Real Life Example :-
Suppose if we want to book tatkal ticket then what will we do ? We will hire a third party man that is called mediator or Broker man.We can easily talk with this Broker man but we can't talk with Ticket counter officers. The Broker man can easily talk with Ticket counter officers as well as users. Same things and activity followed in 3 Tier Architecture Applications.We can relate it as follows:-
User (You,I) ---> Presentation Layer
Broker man ---> Business Access Layer
Ticket Counter Officer ---> Data Access Layer
- In 3 tier application Presentation layer can't talk with Data Access Layer Directly for Security Purpose. Same for users (you,I) also, we can't talk with Ticket counter officer directly.
- Business Access Layer can talk with both layers.Here Broker man talk with both man(you,officers).
Now we can easily relate other activity of user,Broker man and Ticket Officer also with Presentation layer ,Business layer and Data Access Layer respectively That is completely similar with 3 tier architecture.
Working process of 3 Layers:-
When any user post data from our presentation layer(user Interface layer)-->Then this data first goes to Business Access Layer -->After that validation and calculation are held on this layer -->After that this data pass data to Data Access Layer -->After that data Access Layer fetch required data or insert the data in database.-->After that Data Access Layer pass the required data to the business Access Layer.-->After that Business Access Layer send the required data to the Presentation Layer,-->After that Presentation Layer is responsible to display the required data to the user's computers or mobiles or Windows.
Why does we use 3 Tier Architecture:-
- To increase the security in Application.
- To easily maintain the application.
- To easily modify or change in application.
- To Reduce the server over load.
- To reduce the loading time of application.
- All companies, which are working in software environment they always used 3 tier architecture concepts in our application whether they are working on small projects or large projects. if we are using 3 tier concepts then it will be more complex but more understandable to the users.
- Suppose we are working on a project in team 1000 members.There are many member shifted day by day due to some problems.When any new member assigned for this project,then he can easily understand the concepts of 3 Tier Architecture and involve in this project.If companies are working on 1 Tier Architecture then shifting of employee from one project to another projects are not possible.
- In 3 Tier, we can easily update any Tiers of codes easily.But in 1 Tier, it is not possible.
- Each layer always use our separate codes,so it is easy to modify the codes.
- It is helpful to easily maintain and understand the large projects.
- You can easily change your graphical Environment.
- It is more secure because any users can't access the database directly.
- We can easily change any layer codes without affecting other two layers.
- It is a more consistence application.
- Its takes more time to build.
- Many peoples face problems because they haven't good knowledge in oops concepts and other c# programming such as class,object,property etc.
- 3 Tier Architecture is more complex to build.
Step 1:- First open your visual studio --> File -->New--> website -->Select ASP.NET Empty website -->OK -->Open Solution Explorer-->Add a New Web Form (Home.aspx)-->Drag and drop Label ,Text Box ,Button ,Hyper Link ,RequiredFieldValidator,Comarevalidator and SqlDataSource On the page from Toolbox as shown below:-
Step 2:- Now open Solution Explorer -->Add a New web Form (login.aspx) -->Drag and drop Label,Text Box and Button controls on the page from toolbox as shown below:-

Note:- This is also called presentation layer.
Step 3:- Now open Solution Explorer --> Add Database.mdf file on our project folder -->Create student table in sql server for given fields with a specified column as shown below:-
Step 4:- Now open Server Explorer --> Right click of Data Connection->Add Connection
Fill the server name,user name,id,and select database,Then click ok
Step 5:- Now go Home.aspx page -->and Configure Sql Data Source.. with student table by following the Wizard steps (i have create a 3TireConnectionString string in this wizard) as shown below:-
Note:-
Click OK
Step 7:- Similarly follow step 6 and Add two other classes als0 such as
Step 8:-Now Open Business_Object.cs file -->and write the c# property codes for get and set the data as given below:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Business_object
/// </summary>
public class Business_object
{
//Declared Registration varialbles
private string UserName;
private string Password;
private string Qualification;
private int Age;
private string Mobile;
// use properties concept in c#, to get and set the value in variables
public string username
{
get {return UserName;}
set { UserName = value; }
}
public string password
{
get { return Password; }
set { Password = value; }
}
public string qualification
{
get { return Qualification; }
set { Qualification = value; }
}
public int age
{
get { return Age; }
set { Age = value; }
}
public string mobile
{
get { return Mobile; }
set { Mobile = value; }
}
}
}
Note:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for Data_Access_Layer
/// </summary>
public class Data_Access_Layer
{
//Insert our regstration values in student table
public string Registration_Details(Business_object user_details)
{SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["3TireConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Insert into student values(@name,@password,@qualification,@age,@mobile)",con);
try
{ cmd.Parameters.AddWithValue("@name", user_details.username);
cmd.Parameters.AddWithValue("@password", user_details.password);
cmd.Parameters.AddWithValue("@qualification", user_details.qualification);
cmd.Parameters.AddWithValue("@age", user_details.age);
cmd.Parameters.AddWithValue("@mobile", user_details.mobile);
return cmd.ExecuteNonQuery().ToString();
}
catch (Exception show_Error)
{
throw show_Error;
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}
public string login_details(Business_object userlogin_details)
{
SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["3TireConnectionString"].ConnectionString);
con1.Open();
try
{
SqlCommand cmd = new SqlCommand("select count(*) from student where Nmae='" + userlogin_details.username + "' and password='" + userlogin_details.password + "' ", con1);
string str = cmd.ExecuteScalar().ToString();
return str;
}
catch (Exception Info)
{
throw Info;
}
finally {
con1.Close();
con1.Dispose();
}}}
Note:- This is the 3 Layer of this application
Step 10:-Now open Business_Layer.cs file -->write the following c# codes as shown below:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Business_Layer
/// </summary>
public class Business_Layer
{ public string record_insert(Business_object mks)
{Data_Access_Layer dal = new Data_Access_Layer();
try
{
return dal.Registration_Details(mks);
}
catch (Exception info)
{ throw info;
}
finally
{
dal = null;
}
} public string db_Login(Business_object mylogin)
{ Data_Access_Layer dal1 = new Data_Access_Layer();
try
{
return dal1.login_details(mylogin);
}
catch (Exception ex)
{
throw ex;
}
finally
{
dal1 = null;
}}}
Note:- This is called 2 Layer of this application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//Load or call Business object class in memory and get set the value in variables
Business_object bo = new Business_object();
bo.username = txtUName.Text;
bo.password = txtPassword.Text;
bo.qualification = txtQualification.Text;
bo.age = int.Parse(txtAge.Text);
bo.mobile = txtMobile.Text;
//call business layer class by creating object of business layer class
Business_Layer bl = new Business_Layer();
try
{
string result = bl.record_insert(bo);
if (result != null)
{
lbl1.Text = "<b style='color:green'>Record Inserted sucessfully</b>";
txtUName.Text = string.Empty;
txtPassword.Text = string.Empty;
txtQualification.Text = string.Empty;
txtAge.Text = string.Empty;
txtMobile.Text = string.Empty;
}
else
{
lbl1.Text = "<b style='color:green'>Record Inserted faild</b>";
}
}
catch (Exception info)
{
throw info;
} finally
{
bo = null;
bl = null;
}}}
Note:- This is called 1 Layer of this application.
Descriptions:-
Here i have created object(ob) of Business_Object Class.So that we can call Business_Object class methods for assigning the respective Text Box values to the variables.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
Business_object bo1 = new Business_object();
bo1.username = txtUName.Text;
bo1.password = txtPassword.Text;
Business_Layer bl1 = new Business_Layer();
try
{
string login = bl1.db_Login(bo1);
int i = int.Parse(login);
if (i > 0)
{
Response.Redirect("sucess.aspx");
}
else
{
lblStatus.Text = "<b style='color:red'>Wrong User Name or Password</b>";
}
}
catch (Exception ex)
{throw ex;
}
finally
{
bl1 = null;
}}}
Note:- This is also called 1 Layer of this application.
Step 13:- Now ,You have completed the 3 Tier Application -->Run the application (press F5) --> Enter the Registration Fields Details -->Press Register Now Button -->You will see following output as shown below:- without value
After fill the value
Step 14:- If we have completed the registration Field details --> Press Click here Button as given below -->login.aspx page will be opened --> Now enter login details -->and press Login Button -->You will see following output as shown below:-
and see the result your database
Attention:-
Step 3:- Now open Solution Explorer --> Add Database.mdf file on our project folder -->Create student table in sql server for given fields with a specified column as shown below:-
Step 4:- Now open Server Explorer --> Right click of Data Connection->Add Connection
Fill the server name,user name,id,and select database,Then click ok
Step 5:- Now go Home.aspx page -->and Configure Sql Data Source.. with student table by following the Wizard steps (i have create a 3TireConnectionString string in this wizard) as shown below:-
Note:-
- It is very easy process to connect a database.
- When we will follow whole configuration steps then you will see that connection string codes will be automatically inserted in web.config file.If we don't use Sql Data Source then you will have to add connection string(sql connection,stored procedure etc.) codes in web.config file manually
- <configuration>
-
<connectionStrings>
<add name="3TireConnectionString" connectionString="Data Source=mithilesh;Initial Catalog=3Tire;Persist Security Info=True;User ID=sa;Password=12345"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"></add>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
</system.web>
</configuration> -
.Descriptions:-- Here we can see all connection strings codes are inserted within <connectionStrings> ...</connectionStrings>
- If We want to insert other connection string codes such stored procedure , sql server connections then we can put under the configuration section of our web.config file as shown in above codes.
Click OK
Step 7:- Similarly follow step 6 and Add two other classes als0 such as
- Business _Layer.cs
- Data Access Layer.cs
Step 8:-Now Open Business_Object.cs file -->and write the c# property codes for get and set the data as given below:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Business_object
/// </summary>
public class Business_object
{
//Declared Registration varialbles
private string UserName;
private string Password;
private string Qualification;
private int Age;
private string Mobile;
// use properties concept in c#, to get and set the value in variables
public string username
{
get {return UserName;}
set { UserName = value; }
}
public string password
{
get { return Password; }
set { Password = value; }
}
public string qualification
{
get { return Qualification; }
set { Qualification = value; }
}
public int age
{
get { return Age; }
set { Age = value; }
}
public string mobile
{
get { return Mobile; }
set { Mobile = value; }
}
}
}
Note:-
- This Layer is the sub group of Business Access Layer that is called property layer or entity layer.
- Here i have declared all registrations variables Privately .
- Create different methods for each variables to get and set the respective values.Here we can see, each method is returning string values.
- After set and get this method send the respective values to other method from where its called. When we will read each steps carefully then we will see that each layer class methods will call property layer class 's methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for Data_Access_Layer
/// </summary>
public class Data_Access_Layer
{
//Insert our regstration values in student table
public string Registration_Details(Business_object user_details)
{SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["3TireConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Insert into student values(@name,@password,@qualification,@age,@mobile)",con);
try
{ cmd.Parameters.AddWithValue("@name", user_details.username);
cmd.Parameters.AddWithValue("@password", user_details.password);
cmd.Parameters.AddWithValue("@qualification", user_details.qualification);
cmd.Parameters.AddWithValue("@age", user_details.age);
cmd.Parameters.AddWithValue("@mobile", user_details.mobile);
return cmd.ExecuteNonQuery().ToString();
}
catch (Exception show_Error)
{
throw show_Error;
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}
public string login_details(Business_object userlogin_details)
{
SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["3TireConnectionString"].ConnectionString);
con1.Open();
try
{
SqlCommand cmd = new SqlCommand("select count(*) from student where Nmae='" + userlogin_details.username + "' and password='" + userlogin_details.password + "' ", con1);
string str = cmd.ExecuteScalar().ToString();
return str;
}
catch (Exception Info)
{
throw Info;
}
finally {
con1.Close();
con1.Dispose();
}}}
Note:- This is the 3 Layer of this application
Step 10:-Now open Business_Layer.cs file -->write the following c# codes as shown below:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Business_Layer
/// </summary>
public class Business_Layer
{ public string record_insert(Business_object mks)
{Data_Access_Layer dal = new Data_Access_Layer();
try
{
return dal.Registration_Details(mks);
}
catch (Exception info)
{ throw info;
}
finally
{
dal = null;
}
} public string db_Login(Business_object mylogin)
{ Data_Access_Layer dal1 = new Data_Access_Layer();
try
{
return dal1.login_details(mylogin);
}
catch (Exception ex)
{
throw ex;
}
finally
{
dal1 = null;
}}}
Note:- This is called 2 Layer of this application.
- Here I have created the object of Data Access Layer class,so that all members of this class loaded in memory.
- After that call the registration_details method from data access layer with the help of object of that class and pass the value to Data Access Layer or Presentation Layer ,it depends who is calling this class's methods. We already know that this layer easily talk with both layers.
- Here i have also created the object of Data Access Layer class as above.
- Here I have called login_details ( ) method of Data Access layer Class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//Load or call Business object class in memory and get set the value in variables
Business_object bo = new Business_object();
bo.username = txtUName.Text;
bo.password = txtPassword.Text;
bo.qualification = txtQualification.Text;
bo.age = int.Parse(txtAge.Text);
bo.mobile = txtMobile.Text;
//call business layer class by creating object of business layer class
Business_Layer bl = new Business_Layer();
try
{
string result = bl.record_insert(bo);
if (result != null)
{
lbl1.Text = "<b style='color:green'>Record Inserted sucessfully</b>";
txtUName.Text = string.Empty;
txtPassword.Text = string.Empty;
txtQualification.Text = string.Empty;
txtAge.Text = string.Empty;
txtMobile.Text = string.Empty;
}
else
{
lbl1.Text = "<b style='color:green'>Record Inserted faild</b>";
}
}
catch (Exception info)
{
throw info;
} finally
{
bo = null;
bl = null;
}}}
Note:- This is called 1 Layer of this application.
Descriptions:-
Here i have created object(ob) of Business_Object Class.So that we can call Business_Object class methods for assigning the respective Text Box values to the variables.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
Business_object bo1 = new Business_object();
bo1.username = txtUName.Text;
bo1.password = txtPassword.Text;
Business_Layer bl1 = new Business_Layer();
try
{
string login = bl1.db_Login(bo1);
int i = int.Parse(login);
if (i > 0)
{
Response.Redirect("sucess.aspx");
}
else
{
lblStatus.Text = "<b style='color:red'>Wrong User Name or Password</b>";
}
}
catch (Exception ex)
{throw ex;
}
finally
{
bl1 = null;
}}}
Note:- This is also called 1 Layer of this application.
Step 13:- Now ,You have completed the 3 Tier Application -->Run the application (press F5) --> Enter the Registration Fields Details -->Press Register Now Button -->You will see following output as shown below:- without value
After fill the value

and see the result your database
Attention:-
- Here Business_Object class is optional for small project,we can use it or not. But if we are working Large project then we have to use it in our project.
- In Above application ,i have used SqlDataSource connection string. We can also use stored procedure for our database connections if required.
- You can add all class file(.cs) from out side of App_Code folder but we have to set the References to each classes.
----------------------------------------X---------------------------------------X------------------------
No comments:
Post a Comment