- Back to Home »
- Windows Applications »
- 3 Layered Architecture
Posted by :
Sudhir Chekuri
Wednesday, 9 March 2016
If an application is divided into 3 layers for better maintenance of project then it is known as 3 layered architecture.
There
are 3 layers in 3 layered architecture. They are:
1. Presentation layer
2. Business logic layer
3. Data access layer
1. Presentation layer
2. Business logic layer
3. Data access layer
Presentation layer
It
is the User interface through which user will communicate with the application.
It is the GUI part of the application which contains UI controls.
Business logic layer
Programming
logic will be present in business logic layer. It acts as interface between
presentation and data access layer.
Data Access layer
It
contains code to interact with the database and it gives information of
database to business logic layer.
Example:
create table
tbl_layers(sno int, name varchar(20),course varchar(20))
insert into
tbl_layers values(1,'sudhir','JAVA')
create procedure
Usp_Insert
@id int,
@name
varchar(20),
@course
varchar(20)
as begin
insert into
tbl_layers values(@id,@name,@course)
end
Presentation layer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _3_layer_architecture
{
public partial class Form1 : Form
{
DAL dobj = null;
public Form1()
{
dobj
= new DAL();
InitializeComponent();
}
private void BtnInsert_Click(object sender, EventArgs e)
{
BAL balObj = new BAL();
balObj.Id = Convert.ToInt32(TxtId.Text);
balObj.Name=TxtName.Text;
balObj.Course = TxtCourse.Text;
int i=
dobj.insert(balObj);
}
}
}
Bussiness logic layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _3_layer_architecture
{
class BAL
{
public int Id { get; set; }
public string Name { get; set; }
public string Course { get; set; }
}
}
Data access layer
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace _3_layer_architecture
{
class DAL
{
SqlConnection con = new SqlConnection("Data Source=WINBC250150-JUL\\SQLEXPRESS;Initial
Catalog=master;Integrated Security=True");
public int insert(BAL obj)
{
con.Open();
SqlCommand cmd = new SqlCommand("Usp_Insert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", obj.Id);
cmd.Parameters.AddWithValue("@name", obj.Name);
cmd.Parameters.AddWithValue("@course", obj.Course);
int i= cmd.ExecuteNonQuery();
con.Close();
return i;
}
}
}