The client/server architecture(2-tier) is little slower while we are dealing with a huge database .The coding gets more complicated and also while adding new queries to the application software we design.The earlier advancement in two tier architecture is that a host of thin client was developed and thus the development of 3-tier architecture gradually increased.
Besides from having an accurate result of every query , the owner of a system with huge database wants there queries to be called quickly besides from being accurate. This brings up the concept of using another layer i.e. concept of 3-tier architecture.
The 3-tier here are basically includes :
- Presentation Layer
- Business Layer
- Data Access Layer
Presentation Layer :
Presentation layer is what the end user can see. In an desktop application the user interface is called the presentation layer.We may present the data in a mobile device in a tablet or in a computer in a different formats we want and these are all the examples of presentation layer.This layer with the help of business layer and data access layer display the results by passing the data and verifying the credentials executing queries and displaying the result.Business Layer:
Most of the logic are placed in the business layer. The business layer only has access to the data layer.All the business application logic are stored in the business logic layer. Business logic includes all the rules that are required to be implemented. The logic may change and when we do so with this n-architecture its very easy to adding and deleting rules and logic.
examples:
- getting all the user birthday.
- getting the users who lives in kathmandu.
- getting the name of the users whose age is 21.
- getting the products above the price 20k.
Data Access Layer:
This layer is right below the business and presentation layer.This layer only has the access to the system database. Based on the query passed by business layer it retrieves the data from database and sends back to business layer for processing and eventually comes out in a presentation layer.Example code :
Three-tier based application software usingVisual studio 2012 can be found here
Presentation layer code
namespace threetire_secondtry
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
BLLUser bll = new BLLUser();
private void btnAdd_Click(object sender, EventArgs e)
{
int i = bll.add(txtFirstname.Text, txtLastname.Text, Convert.ToString(txtPhoneno.Text));
if (i > 1)
{
MessageBox.Show("Contact successfully Added");
}
}
private void btnView_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = bll.getallusers();
if (dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
}
}
}
}
Business Layer code
namespace BusinessLibrary
{
public class BLLUser
{
public int add(string firstname, string lastname, string phoneno)
{
SqlParamater[] param = new SqlParamater[3];
param[0] = new SqlParamater("@firstname", firstname);
param[1] = new SqlParamater("@lastname", lastname);
param[2] = new SqlParamater("@phoneno", phoneno);
return DAO.add("sp_add",param);
}
public System.Data.DataTable getallusers()
{
return DAO.getallusers("sp_getallusers",null);
}
}
}
Data Access Layer code
namespace DataAccessLayer
{
public static class DAO
{
public static string ConnectionString
{
get
{
return ConfigurationSettings.AppSettings["myconnection"]; // gets the connection string only
}
}
public static SqlConnection GetConnection() //gets a sql connection
{
SqlConnection con = new SqlConnection(ConnectionString);
if (con.State != ConnectionState.Open)
{
con.Open();
}
return con; // returns con ie sqlconnection
}
public static DataTable getallusers(string Storeprocedurename, SqlParameter param)
{
DataTable dt = null;
using (SqlConnection con = DAO.GetConnection())
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = Storeprocedurename;
cmd.CommandType = CommandType.StoredProcedure;
if (param != null)
{
cmd.Parameters.AddRange(param);
}
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
dt = new DataTable();
da.Fill(dt);
}
return dt;
}
}
}
}
}
Configuration file
/*
*/
No comments:
Post a Comment