28 April 2014

Real-Time 3-Tier architecture Application Example using ASP.NET

In 3-Tier programming architecture, the application development is broken into three main parts.
These are:
1.Application/Presentation layer
2.Business Acess Layer(BAL) and
3.Data Access Layer(DAL).
These separations ensure independent development of those layers such that a change in one does not affect the other layers. For instance a change in the logic of the DAL layer does not affect the the BAL nor the presentation layer. It also makes testing easier in that a whole layer can be replaced with a stub object.

For example instead of testing the whole application with an actual connection to database, the DAL can be replaced with a stub DAL for testing purposes. The DAL deals with actual database operations. These operations are inserting, updating, deleting and viewing. The BAL deals with the business logic for a particular operation. The PL has the user controls for interacting with the application.

3-Tier programming architecture also enables re-use of code and provides a better way for multiple developers to work on the same project simultaneously.
1.Start a new website project
2.Design you page as shown below,
                           

3.Add sub-folders and class objects within the App_code folder as shown below,
                       
 /*
    1.First Add sub-folders and class objects within the App_code folder and then
     Create Database(RegDB.mdf) and create a table (Registration) with field names as shown below(regBEL.cs file).

    2.Set the connection string on the web.config file:

   <connectionStrings>
<add name="dbconstring"
            connectionString="DataSource=.\SQLEXPRESS;
            AttachDbFilename=G:\Practice\3-Tier Example2\App_Data\RegDB.mdf;Integrated Security=True;
            User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
*/

4.Code for regBEL.cs as shown below:

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

/// <summary>
/// Summary description for regBEL
/// </summary>
public class regBEL
{
public regBEL()
{
//
// TODO: Add constructor logic here
//
}
    public string FirstName { set; get; }
    public string LastName { set; get; }
    public string UserName { set; get; }
    public string MobileNo { set; get; }
    public string Address { get; set; }
    public string Password { get; set; }

}

5.See the Code for the Submit button (btnsubmit_Click event) in the .aspx.cs file is shown below,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    regBEL regbel = new regBEL();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        regbel.FirstName = txtfirstname.Text;
        regbel.LastName = txtlastname.Text;
        regbel.UserName = txtusername.Text;
        regbel.Password = txtpwd.Text;
        regbel.Address = txtaddress.Text;
        regbel.MobileNo = txtmobile.Text;

        regbel = regBAL.insertStudentdetails(regbel);
        lblmessage.Text = " registration successfully completed";
    }
    protected void btnreset_Click(object sender, EventArgs e)
    {
        txtaddress.Text = ""; txtfirstname.Text = ""; txtlastname.Text = "";
        txtmobile.Text = "";  txtusername.Text = "";
        lblmessage.Text = "";
        //txtpwd.Text = "";
    }
}

6.Code for regBAL.cs as shown below:

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

/// <summary>
/// Summary description for regBAL
/// </summary>
public class regBAL
{
public regBAL()
{
//
// TODO: Add constructor logic here
//
}
    public static regBEL insertStudentdetails(regBEL regbel)
    {
        return regDAL.insertStudentdetails(regbel);
    }
}

7.Code for regDAL.cs as shown below:

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

/// <summary>
/// Summary description for regDAL
/// </summary>
public class regDAL
{
public regDAL()
{
//
// TODO: Add constructor logic here
//
}
    private static string strcon = ConfigurationManager.ConnectionStrings["dbconstring"].ToString();
    private static SqlConnection con = null;
    private static SqlCommand cmd = null;
    private static SqlDataAdapter da = null;
    private static DataSet ds = null;

    internal static regBEL insertStudentdetails(regBEL regbel)
    {
        con = new SqlConnection(strcon);
        string strsql = "insert into Registration(FirstName, LastName, UserName, Password, Address, MobileNo)" + " values(@FirstName,@LastName,@UserName,@Password,@Address,@MobileNo)";
        cmd = new SqlCommand(strsql, con);
        cmd.CommandType = CommandType.Text;

        try
        {
            cmd.Parameters.AddWithValue("@FirstName", regbel.FirstName);
            cmd.Parameters.AddWithValue("@LastName", regbel.LastName);
            cmd.Parameters.AddWithValue("@UserName", regbel.UserName);
            cmd.Parameters.AddWithValue("@Password", regbel.Password);
            cmd.Parameters.AddWithValue("@Address", regbel.Address);
            cmd.Parameters.AddWithValue("@MobileNo", regbel.MobileNo);

            if (con.State == ConnectionState.Closed)
            {
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
            return regbel;
        }
        catch
        {
            throw;
        }
    }
}

8.Run the project(F5) and Displays the page as shown below:
     

No comments:

Post a Comment