Friday, 19 April 2013

3 - Tier architecture Asp.net C#

The 3- Tier architecture consists of 3 layers.They are
Application Layer or Presentation Layer
Buisness Logic Layer(BLL) or Buisness Access Layer(BAL)
Data Access Layer(DAL)

Application Layer or Presentation Layer

This layer is for contains the UI part.Through this layer only we get the input. FOR EXAMPLE:  In the aspx page textbox , button,etc... are used for getting the input and to display or present the output.

Here i will show an example to insert the username and password using 2 text boxes and a button.Write the following in the "userinsert.aspx" page.

<asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
<asp:TextBox ID="txtpassword" runat="server"></asp:TextBox>
<asp:Button ID="btninsert" runat="server">Insert</asp:Button>

Buisness Logic Layer(BLL) or Buisness Access Layer(BAL) 

This layer acts as a interface between Application layer and Data Access Layer.Here we use this layer to assign the parameters i.e, get and set and other calculations related with the data like insert data, retrieve data and validating the data.

Right click on your project web application---> select add new item ----> select class file in wizard --->give name as userBLL.CS


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Data;

public class userBLL

{

    userDAL DAL;
public userBLL()
{
        DAL = new userDAL(this);
}

    private string _UserName;
    private string _Password;
    

    private string _Mode;
    private string _Procedure;
  


    public string UserName    {
        get
        {
            return _UserName;
        }
        set
        {
            _UserName = value;
        }
    }

    public string Password

    {
        get
        {
            return _Password;
        }
        set
        {
           _Password = value;
        }
    }

   public string Mode

    {

        get
        {
            return _Mode;
        }
        set
        {
           _Mode = value;
        }
    }

    public string Procedure
    {
        get
        {
            return _Procedure;
        }
        set
        {
            _Procedure = value;
        }
    
    }


   public void Insert(string Connect)
    {
        DAL.Insert(Connect);
    }
  }

Data Access Layer(DAL)
This Layer contains methods or procedures to connect with database and to perform insert,update,delete,get data from database based on our input data.
Right click on your project web application---> select add new item ----> select class file in wizard --->give name as userDAL.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

public class userDAL
{

    userBAL BAL;
    SqlConnection Con;
    SqlCommand cmd;

     public userDAL(userBAL BAL1)
    {
     BAL=BAL1;
    }
      public void Connection(string Conn)
    {
        Con = new SqlConnection(Conn);
        if (Con.State == ConnectionState.Closed)
        {
            Con.Open();
        }
    }
    public void Insert(string Connect)
    {
        Connection(Connect);
        cmd = new SqlCommand(BAL.Procedure, Con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = BAL.UserName;
        cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = BAL.Password;     
        cmd.Parameters.Add("@Mode", SqlDbType.VarChar).Value = BAL.Mode;
        cmd.ExecuteNonQuery();
        Con.Close();
    }
}

Now BLL and DAL are ready.Now once again return to the application layer and write the following in the button click event of the "userinsert.aspx.cs" page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
string Conne = ConfigurationManager.ConnectionStrings["Connects"].ConnectionString.ToString();
    userBAL BAL = new userBAL();

  protected void Button1_Click(object sender, EventArgs e)
    {
        BAL.UserName =txtusername.Text;
        BAL.Password = txtpassword;
        BAL.Mode = "Insert"; //Stored procedure for insert
        BAL.Procedure = "SP_Tbl_userinsesrt"; //Stored procedure name 

        BAL.Insert(Conne);
    }

Thats it..3 Layers are completed.Now its ready to run....


No comments:

Post a Comment

back to top