step_by_step_3_tier_architecture
step_by_step_3_tier_architecture
Presentation layer is your user interface layer where you can design your interface using html web
controls or windows controls or mobile controls. It can be your website or windows application or
mobile application.This layer only communicates with business layer.
`Ìi`ÊÜÌ ÊvÝÊ* Ê `ÌÀÊ
ÊvÀiiÊvÀÊViÀV>ÊÕÃi°
Application layer or Business Layer is the middle layer or bridge layer that connects database layer
/ÊÀiÛiÊÌ ÃÊÌVi]ÊÛÃÌ\Ê
and presentation layer. In this layer you can write your business logic (logic written as per business ÜÜܰVi°VÉÕV° Ì
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
requirement) or any validation code.This layer communicates with database layer and presentation
layer.
Database layer which makes the connection to the database server. In this layer you can write
database connection and sql queries or stored procedures. This layer only communicates with
business layer.
When user post any data from from user interface (presentation page), data first goes to the business
layer there data will be validated then validated data is posted to database layer to insert into the
database.
When user request any data from the database then the request is first processed by the business
layer validates the request and sends to database layer then database layer forwards it to the
database server and fetches necessary records. Found records are loaded by the database layer and
passes it back to the business layer then business layer passes those records to the presentation
layer.
As each tier is independent it is possible to enable parallel development of each tier by using different
sets of developers.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Easy to maintain and understand large project and complex project.
Since application layer is between the database layer and presentation layer so the database layer
will be more secured and client will not have direct access to the database.
Posted data from presentation layer can be verified or validated at application layer before updating it
to the database.
Application layer or middle layer or business layer can be a protection shield to the database.
New rules or new validation rules can be defined any time and changes made to middle layer will not
effect presentation layer.
Define any logic once within the business layer and that logic can be shared among any number of
components in the presentation layer.
We can display only necessary methods from business layer in the presentation layer.
We can hide unnecessary methods from business layer in the presentation layer.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Disadvantage of using 3 tier architecture
Time consuming : To implement even small part of application it will consume lots of time.
Difficult to understand : If a new developer joins you for the same project then it becomes very
difficult to make him/her understand about the code and project (inspite of having documentation).
No proper templatization : It becomes very difficult to find any specific part of project code.
Security Threat : Our database server is not secured because database server code is directly
written in the page level.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
We are human beings we cannot remember all those codes which we have written in the different
pages over the years (Large projects). So in order to maintain flexibility, migration facility, data
security, proper maintenance and to understand project easily even after the years it is best practices
to use 3 tier architecture in projects.
For small projects with 5 to 20 screens it is not necessary to implement three tier architecture. But for
large projects it is recommended to use three tier implementation.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
As you see from above code that we have successfully created a website with dotnet framework 4.0
and language c#.
In our website screen we have created a customer entry screen to add a new customer to the data
base means using this screen we will see how to post data over 3 tier architecture and update it in the
database.
Then we have added a gridview to the screen to display all customers from the data base means
using gridview screen we will see how to fetch all records from the database and display it on screen
with edit and delete options.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
In the same gridview we provided two options "edit" and "delete" to edit and delete selected
customer records. Kindly Note : If you want to know more about insert, update, delete and select in
asp.net c# then click here to view my article.
"Edit" option provided to see how to fetch selected customer ID record from database over 3 layers.
"Delete" option provided to see how to delete selected customer ID record from database over 3
layers.
So this is how our presentation layer looks like now we will create our database layer and start our
coding from database layer->Business Layer->Presentation Layer.
In this step we will add a new project of class library to our existing project and name it as database
layer.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DatabaseLayer
{
public class clsDataLayer
{
}
}
As you see from our above code that we have successfully created our database layer.
In our web application we will use SQL SERVER as our data provider so lets make a connection to
sql server. For best practices we will write our sql connection string in the web config file. As we know
that web config is the configuration file for web applications so if we write sql connection in that file we
can access sql connection string from any file in the project.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="Mythreetier" connectionString="Data Source=guru\sqlexpress;Initial Catalog=ProjectBilling;Integrated Security=True" providerNa
</connectionStrings>
</configuration>
So as you see from above code that we have successfully added connection string "Mythreetier" to
web config file. Now lets call that sql connection string in database layer file.
To call connection string from web config file to database layer, manually we need to add references
of System.Configuration to database layer references folder as shown in below image file.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
We also need to import data references in the database layer file and finally calling connection string
"Mythreetier" in database layer file as shown below code.
using System.Collections.Generic;
using System.Linq;
using System.Text;
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DatabaseLayer
{
public class clsDataLayer
{
private string conn = ConfigurationManager.ConnectionStrings["Mythreetier"].ToString();
}
}
Now in this step we will create methods which will execute Insert/Update/Delete command using
SQLCOMMAND and another method which execute Select command using SQLCOMMAND and
DATASET to load all records and pass it to business layer.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
using System.Data.SqlClient;
namespace DatabaseLayer
{
public class clsDataLayer
{
private string conn = ConfigurationManager.ConnectionStrings["Mythreetier"].ToString();
}
}
As you see from our above code that we have created two following methods
"InsertUpdateDeleteSQLString()" to execute Insert/Update/Delete and "ExecuteSqlString()" to
execute Select command.
Before we create methods for insert/update/delete/select sql queries let me just show you how our
"customer" table structure in sql database looks like.
Now lets create methods for insert/update/delete/select sql queries as shown below.
using System.Collections.Generic;
using System.Linq;
using System.Text;
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DatabaseLayer
{
public class clsDataLayer
{
private string conn = ConfigurationManager.ConnectionStrings["Mythreetier"].ToString();
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
DataSet ds = new DataSet();
SqlCommand objcmd = new SqlCommand(sqlstring, objsqlconn);
SqlDataAdapter objAdp = new SqlDataAdapter(objcmd);
objAdp.Fill(ds);
return ds;
}
public void AddNewCustomerDB(string custname, string custaddr, string custcountry, string custcity, string custincode)
{
DataSet ds = new DataSet();
string sql = "INSERT into Customer (customer_name,customer_address,customer_country,customer_city,customer_pincode) VALUES (
InsertUpdateDeleteSQLString(sql);
}
public void UpdateCustomerDB(int custid, string custname, string custaddr, string custcountry, string custcity, string custincode)
{
DataSet ds = new DataSet();
string sql = "Update Customer set customer_name='" + custname + "',customer_address = '" + custaddr + "',customer_country= '" + custc
InsertUpdateDeleteSQLString(sql);
}
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
InsertUpdateDeleteSQLString(sql);
}
}
}
This is the complete code of database layer file as you see from above code that we have created
different methods for insert/update/delete/select sql queries. Now these are the methods which will
used in the business layer for customer insert, update, delete and select.
In this step we will add a new project of class library to our existing project and name it as business
layer.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BusinessLayer
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
{
public class clsBusinessLayer
{
}
}
As you see from our above code that we have successfully created our business layer.
In this step we need to import the namespace of database layer and then create the object of class
"clsDatalayer" as shown below code. To import the namespace manually we need to add database
layer project references in the business layer references folder. as shown below.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
After adding the references we need to create a database layer class object in the business layer
class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseLayer;
namespace BusinessLayer
{
public class clsBusinessLayer
{
public clsDataLayer objDataLayer = new clsDataLayer();
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
}
}
Now in further step we will create methods in business layer for select/update/delete/insert to update
from presentation layer to database layer.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseLayer;
namespace BusinessLayer
{
public class clsBusinessLayer
{
public clsDataLayer objDataLayer = new clsDataLayer();
public void AddNewCustomer(string custname, string custaddr, string custcountry, string custcity, string custincode)
{
objDataLayer.AddNewCustomerDB(custname, custaddr, custcountry, custcity, custincode);
}
public void UpdateCustomer(int custid, string custname, string custaddr, string custcountry, string custcity, string custincode)
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
{
objDataLayer.UpdateCustomerDB(custid, custname, custaddr, custcountry, custcity, custincode);
}
So as you see from above code that we have successfully created each methods for
select/insert/delete/update i.e.
So our business layer and datalayer is now ready to take input values from presentation layer.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
If you see closely look without touching user interface we have coded our business logic and database
logic so tommorow if our user interface changes from web application to mobile application then our
business logic and database logic remains the same because our business layer and database layer
are not coupled with user-interface layer.
So friends as you see this is one of the main advantage of using 3 tier architecture in projects
because it gives you more flexibility to handle projects over a time period if any user-interface comes
or any code migration happens then we can handle them easily without changing our database layer
and business layer snippet only we need to re-code our user-interface pages.
Finally we will now code our presentation layer. First we need to reference business layer project in
the website application references folder and import the namespace of business layer.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void btnaddcustomer_Click(object sender, EventArgs e)
{
}
}
As you see from above code that we have successfully imported the namespace in the presentation
layer. Now lets do select/insert/update/delete using Business layer methods.
Select Customer
Now lets load all the customers in the Gridview using LoadCustomer() method.
clsBusinessLayer objLogic;
As you see from above image we have displayed all customer records in the gridview using
LoadCustomer() method.
We will insert the customer record and to insert we will use AddNewCustomer() method in the button
click event and pass appropriate values to it.
clsBusinessLayer objLogic;
if (HiddenField1.Value == "")
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
{
objLogic.AddNewCustomer(txtcustname.Text, txtcustaddr.Text, txtcustcountry.Text, txtcustcity.Text, txtcustincode.Text);
}
Response.Redirect("Default.aspx");
}
Note : HiddenField is by default empty when the user clicks on edit link then we have set the
HiddenField value with the customer ID but by default it is empty.
Update Customer
When the user from the screen clicks on edit link which is located on gridview then on
GridView1_SelectedIndexChanging() event we have set all the values to text fields as shown
below.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
txtcustname.Text = GridView1.Rows[index].Cells[3].Text;
txtcustaddr.Text = GridView1.Rows[index].Cells[4].Text;
txtcustcountry.Text = GridView1.Rows[index].Cells[5].Text;
txtcustcity.Text = GridView1.Rows[index].Cells[6].Text;
txtcustincode.Text = GridView1.Rows[index].Cells[7].Text;
HiddenField1.Value = GridView1.Rows[index].Cells[2].Text;
If the hiddenfield contains the value and user clicks on update button then we used
UpdateCustomer() method to update the records to the database as shown in below code.
clsBusinessLayer objLogic;
Delete Customer
When the user clicks on delete link located on gridview then on GridView1_RowDeleting() click
event we have called DeleteCustomer() method to delete a record as shown in below code.
clsBusinessLayer objLogic;
objLogic.DeleteCustomer(custid);
}
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Complete Code of Presentation Layer is shown below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
int index = Convert.ToInt32(e.NewSelectedIndex);
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
txtcustname.Text = GridView1.Rows[index].Cells[3].Text;
txtcustaddr.Text = GridView1.Rows[index].Cells[4].Text;
txtcustcountry.Text = GridView1.Rows[index].Cells[5].Text;
txtcustcity.Text = GridView1.Rows[index].Cells[6].Text;
txtcustincode.Text = GridView1.Rows[index].Cells[7].Text;
HiddenField1.Value = GridView1.Rows[index].Cells[2].Text;
objLogic.DeleteCustomer(custid);
}
protected void btnaddcustomer_Click(object sender, EventArgs e)
{
if (HiddenField1.Value == "")
{
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
else
{
objLogic.UpdateCustomer(Convert.ToInt16(HiddenField1.Value), txtcustname.Text, txtcustaddr.Text, txtcustcountry.Text, txtcustcity.Text, tx
}
ClearAll();
Response.Redirect("Default.aspx");
}
So friends i hope you understood this complete tutorial on Three Tier Architecture in ASP.NET using
c# language. If you have any doubts or queries kindly feel free to ask me through your comments. If any
part of code is wrong or missed something kindly let me know through comments. If you like this article
or if you found this article useful just share it with your friends on social media networks like Facebook,
twitter and Google+.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com