Showing posts with label How to Implement 3 Tier Architecture Concepts in asp.net. Show all posts
Showing posts with label How to Implement 3 Tier Architecture Concepts in asp.net. Show all posts

Friday, 10 October 2014

How to Implement 3 Tier Architecture Concepts in asp.net,Step by step 3 tier architecture in asp.net using c# example,3-Tier Architecture in ASP.NET with C#



 How to Implement 3 Tier Architecture Concepts in asp.net



Hai friend I have create simple registration page using three tier Architecture Concepts in asp.net..we use three tier Architecture Concepts in higher Level companies  where there is separately three layer are formed There are three layers in 3 Tier Architecture as given below:-

1.    Presentation Layer (UI Layer)
2.    Business Access Layer (BAL)
3.    Data Access Layer (DAL)

Where each layer as unique important in the role of the project .
  1.) Presentation Layer(UI) :-
Presentation Layer is nothing but it is a user interface which every user see on your computer ,mobile and window screen. You can say,designing part of any application is known as Presentation Layer. The User can post input and get output on your presentation Layer only.In asp.net .axpx file is known as a presentation layer.

   2.) Business Access Layer(BAL) :-
Business Access Layer is act as mediator Layer 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 you are working on a small projects.But if you are working on large projects then you 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.

You can easily understand the exact concepts of 3 Tier Applications as show below:-


Fig. 3 Tier Architecture

What are the working process of 3 Layers:-

When any user post data from your 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:-
There are following reasons to use 3 Tier Architecture in our asp.net application as given below:-

§  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.

Example :- 

§  All companies, which are working in software environment they always used 3 tier architecture concepts in your application whether they are working on small projects or large projects.  if you are using 3 tier concepts then it will be more complex but more understandable to the users.
§  Suppose you 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, you can easily update any Tiers of codes easily.But in 1 Tier, it is not possible.
Advantages of 3 Tier Architecture:-

§  Each layer always use your 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. 
§  You can easily change any layer codes without affecting other two layers.
§  It is a more consistence application.
Disadvantage of 3 Tier Architecture :- 

§  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.

There are some steps to implement the 3 Tier Architecture concepts in asp.net applications.In this application ,I will explain "How to build 3 tier registration and Login page in asp.net " as given below:- 

I have created this login page in 3 tier architecture.
Inside BuninessLayer class library i have created class named BuninessLayer.cs
Inside DAL class library i have created class named dataLayer.cs
Inside Registration3Tier application i have created web form RegisterData.aspx
Below i gave code for all the classes and web form.

Step:1
create database skyworld

use skyworld

create table myregisterdata(sno int identity(1,1) primary key,firstname varchar(30),lastname varchar(30),username varchar(30),password varchar(30),houseaddress varchar(30),phoneno varchar(30))

select * from myregisterdata

create procedure userdata
(@spfirstname varchar(30),@splastname varchar(30),@spusername varchar(30),@sppassword varchar(30),@sphouse varchar(30),@spphone varchar(30))
as
begin
insert into myregisterdata values (@spfirstname,@splastname,@spusername,@sppassword,@sphouse,@spphone)
end
go
Step:2


RegisterData.aspx Code
<table style="height: 304px; width: 393px">
<tr>
<td>First Name</td>
<td>:</td>
<td><asp:TextBox ID="txtfirst" runat="server" ></asp:TextBox></td>
</tr>

<tr>
<td>Last Name</td>
<td>:</td>
<td><asp:TextBox ID="txtlast" runat="server" ></asp:TextBox></td>
</tr>
<tr>
<td>User Name</td>
<td>:</td>
<td><asp:TextBox ID="txtuser" runat="server" ></asp:TextBox></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><asp:TextBox ID="txtpassword" runat="server" ></asp:TextBox></td>
</tr>
<tr>
<td>Address</td>
<td>:</td>
<td><asp:TextBox ID="txtaddress" runat="server" ></asp:TextBox></td>
</tr>
<tr>
<td>Phone No</td>
<td>:</td>
<td><asp:TextBox ID="txtphoneno" runat="server" ></asp:TextBox></td>
</tr>
<tr>
<td></td><td></td>
<td><asp:Button ID="btnsubmit" runat="server" Text="Submit"
onclick="btnsubmit_Click" /> &nbsp;&nbsp;&nbsp; <asp:Button ID="btnreset"
runat="server" Text="Reset" onclick="btnreset_Click" /></td>
</tr>

</table>
RegisterData.aspx.cs Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuninessLayer;
using dataLayer;

namespace Registration3Tier
{
public partial class RegisterData : System.Web.UI.Page
{
objectdataBus dataregister = new objectdataBus();


studentlist studentdata = new studentlist();




protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnreset_Click(object sender, EventArgs e)
{
txtfirst.Text = "";
txtlast.Text = "";
txtuser.Text = "";
txtpassword.Text = "";
txtaddress.Text = "";
txtphoneno.Text = "";
}

protected void btnsubmit_Click(object sender, EventArgs e)
{
studentdata.FirstName = txtfirst.Text;
studentdata.LastName = txtlast.Text;
studentdata.UserName = txtuser.Text;
studentdata.password = txtpassword.Text;
studentdata.Address = txtaddress.Text;
studentdata.PhoneNumber = txtphoneno.Text;

try
{
string result = dataregister.record_insert(studentdata);
if (result != null)
{
Label1.Text = "inserted Successfully";
}
else
{
Label1.Text = "Not registerd Successfully";
}


}
//catch (Exception info)
//{
//    throw info;
//}
finally
{
studentdata = null;
}
}
}
}

Step 3

Modify the BL layer BuninessLayer.cs as below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using dataLayer;

namespace BuninessLayer
{
    public class objectdataBus
    {
        dataLayer.datainfo datanew = new dataLayer.datainfo();
     
        public string  record_insert(studentlist abc)
        {
            try
            {
                return datanew.registration_details(abc);
            }
            catch (Exception e)
            {
                throw e;

            }
            finally
            {
            datanew = null;
            }
        }

    }
}

Step 4

Modify the DL Layer DataLayer.cs code as below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace dataLayer
{
   
    public class datainfo
    {
      
          string dataconfig = ConfigurationManager.ConnectionStrings["configdata"].ToString();

        SqlConnection sqlcon;
        SqlCommand sqlcmd;
        SqlDataAdapter sqldap;
        DataSet ds;
        public string registration_details(studentlist user_details)
        {
            sqlcon = new SqlConnection(dataconfig);
            sqlcmd = new SqlCommand();
            sqlcmd.Connection=sqlcon;
            sqlcmd.CommandType=CommandType.StoredProcedure;
            sqlcmd.CommandText="userdata";
            sqlcon.Open();
            try
            {
               sqlcmd.Parameters.AddWithValue("@spfirstname",user_details.FirstName);
                sqlcmd.Parameters.AddWithValue("@splastname",user_details.LastName);
                 sqlcmd.Parameters.AddWithValue("@spusername",user_details.UserName);
                 sqlcmd.Parameters.AddWithValue("@sppassword",user_details.password);
                 sqlcmd.Parameters.AddWithValue("@sphouse",user_details.Address);
                 sqlcmd.Parameters.AddWithValue("@spphone",user_details.PhoneNumber);
                return sqlcmd.ExecuteNonQuery().ToString();
            }
            catch (Exception showerror)
            {
                throw showerror;

            }
            finally
            {
                sqlcmd.Dispose();
               
                sqlcon.Dispose();

            }

                
            }

        }

      


    }

write the c# property codes for get and set the data  as given below:- 
  public class studentlist
    {


        public string FirstName { set; get; }
        public string LastName { set; get; }
        public string UserName { set; get; }
        public string password { set; get; }
        public string Address { get; set; }
        public string PhoneNumber { set; get; }
    }
}

Output