Saturday, 5 September 2009

Fun With 3 - Tier Architecture

Hello Friends,

In this post, i am going to show how to implement the three tier architecture into your web application.The three tier architecture consists of three different layers :

  1. Presentation Layer
  2. Business Layer
  3. Data Access Layer
Here we are going to implement this three tier arch in our web application in its simplest form.First of all lets create a simple web for that consists of four textboxes for inserting data and a gridview for showing the data in the following manner :



Now lets first prepare our data access layer.We have to create two simple procedure for showing and inserting the records.We create the Procedure this way :

set ANSI_NULLS ON [For Showing Records]
set QUOTED_IDENTIFIER ON
go


CREATE PROCEDURE [dbo].[ShowRecords]
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM Profile
END


set ANSI_NULLS ON [For Inserting Records]
set QUOTED_IDENTIFIER ON
go

CREATE PROCEDURE [dbo].[InsertRecord]
(
@name varchar(50),
@designation varchar(50),
@company varchar(50),
@experience varchar(10)
)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO PROFILE (Name,Designation,Company,Experience) VALUES(@name,@designation,@company,@experience)
END


Now its time to design our data access layer.For this purpose we create a new clas and name it DataLayer.cs.In this class we create two methods ,one for showing records and the other one for inserting the records in the following manner :

public DataSet ShowRecords()
{
SqlConnection con = new SqlConnection("Data Source=kartik;Initial Catalog=MyNetwork;Integrated Security=true");
con.Open();
SqlCommand com = new SqlCommand ();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "ShowRecords"; // name of stored procedure
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
return ds;
}

public void InsertRecord(string stpName, SqlParameter[] Param)
{
SqlConnection con = new SqlConnection("Data Source=kartik;Initial Catalog=MyNetwork;Integrated Security=true");
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = stpName;
com.Parameters.Add(Param[0]); // values of parameters, returned from Business layer
com.Parameters.Add(Param[1]);
com.Parameters.Add(Param[2]);
com.Parameters.Add(Param[3]);

com.ExecuteNonQuery();
con.Close();
}

Here in the above InsertRecords function the stpName parameter is used for the name of stored procedure used and parameter Param is array of sqlparameter type that contains all the parameter values used in stored procedure.

Now we design our Business Layer.Now again create a class and name it BussLayer.cs.In the class add the following properties :

string Name;
string Desig;
string Company;
string Exep;
public string _Name
{
get
{ return Name; }
set
{ Name = value; }
}
public string _Desig
{
get
{ return Desig; }
set
{ Desig = value; }
}
public string _Company
{
get
{ return Company; }
set
{ Company = value; }
}

public string _Exep
{
get
{ return Exep; }
set
{ Exep = value; }
}


And add following two methods for Inserting and showing records :

public DataSet showData()
{
DataLayer Obj = new DataLayer(); // Create object of our DataLayer class created above
DataSet ds = new DataSet();
ds = Obj.ShowRecords();
return ds;
}

public void insertRecord()
{
DataLayer Obj = new DataLayer();
SqlParameter[] objParameter = new SqlParameter[4];

objParameter[0] = new SqlParameter();
objParameter[0].ParameterName = "@name";
objParameter[0].SqlDbType = SqlDbType.VarChar;
objParameter[0].Value = _Name;

objParameter[1] = new SqlParameter();
objParameter[1].ParameterName = "@designation";
objParameter[1].SqlDbType = SqlDbType.VarChar;
objParameter[1].Value = _Desig;

objParameter[2] = new SqlParameter();
objParameter[2].ParameterName = "@company";
objParameter[2].SqlDbType = SqlDbType.VarChar;
objParameter[2].Value = _Company;

objParameter[3] = new SqlParameter();
objParameter[3].ParameterName = "@experience";
objParameter[3].SqlDbType = SqlDbType.VarChar;
objParameter[3].Value = _Exep;

Obj.InsertRecord("InsertRecord",objParameter); // Passing proc name and sql parameters to dataLayer
}


This Business layer will work as a bridge between our data access layer and the presentation layer.Now finally lets prpare our presentation layer.In the code behind of our aspx page we add the following method for binding data to our grid :

public void BindGrid()
{
BussLayer Obj = new BussLayer(); // Creating object of business layer class, created above
DataSet ds = new DataSet();
ds = Obj.showData();
GridView1.DataSource = ds;
GridView1.DataBind();
}


Now do the following coding on the click event of Show Record button :

BindGrid();

Now do the following coding on the click event of Insert Record button :

BussLayer Obj = new BussLayer();
Obj._Name = TextBox1.Text;
Obj._Desig = TextBox2.Text;
Obj._Company = TextBox3.Text;
Obj._Exep = TextBox4.Text;
Obj.insertRecord();


Now we are ready to run our application for the first time.Now run the application and click the Show Record button.The records will be shown in the grid.

Now add some data to the text boxes and click the Add Record button.The record will be inserted in to database.



You can see the inserted record by again clicking the Show Record button.

So now we are done with our basic three tier architecture.We have utilized all the three layers in our application.Now you can Utilize this architecture in the greater scenario by creating the class library and using the DLL's of those class library into your applications.

Download Full Source Code Here [C#/VB.Net]


3tierArch
3tierArch.rar
Hosted by eSnips

No comments: