Surendra Sharma

Surendra Sharma

Search This Blog

Monday, July 8, 2013

How to create the 3 tier / layer architecture application in .NET

Most of the projects in .NET are developed using tier / layer architecture. Though tier and layer have different meaning, but for all .NET beginners I am using same term.
Below is typical diagram of 3 tier architecture.


  1. Presentation Layer: It’s a User Interface Layer (UIL) where users are interacting with an application. It also contains additional application logic. This layer may be ASP.NET, windows or mobile application. This layer interacts with middle layer. It never directly access data layer.

2.     Business Layer: It’s a Business Logic Layer (BLL) which communicates both to Presentation layer or Data Layer.

3.     Data Layer: It’s a Data Access Logic Layer (DAL) which interacting with database.

Steps to create in Visual Studio using C#
  1. Create one window / web project.
  2. Right click on your solution, Add one new project as shown below


  1. Select one class library project, specify its name as “DataAccessLayer”, as shown below


  1. Similarly create another class library name as “BusinessLogicLayer”
  2. Create classes in DataAccessLayer for StudentInfo table as shown below
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

namespace DataAccessLayer
{
    class StudentInfo
    {
        public DataTable LoadAll()
        {
            //Write code to load all the records of studentinfo Table
            DataTable objDataTable = new DataTable();
            return objDataTable;
        }

        //Write rest of the code for each SP - Insert, Update, delete
    }
}

  1. Build DataAccessLayer Project.
  2. Add the reference of DataAccessLayer in BusinessLogicLayer project as shown below

 



  1. Try to access the DataAccessLayer’s StudentInfoDAL class in BusinessLogicLayer as shown below
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class StudentInfoBLL
    {
        /// <summary>
        /// Call the StudentInfoDAL class of DataAccessLayer
        /// </summary>
        /// <returns></returns>
        public DataTable LoadAll()
        {
            StudentInfoDAL objStudentInfoDAL = new StudentInfoDAL();
            return objStudentInfoDAL.LoadAll();
        }

        //Write rest of the code to call the Insert, Update, delete method of StudentInfoDAL
    }
}

  1. Build BusinessLogicLayer Project.
  2. Similarly Add the reference of BusinessLogicLayer in your window / web project.
  3. Try to access the BusinessLogicLayer’s StudentInfoBLL class.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BusinessLogicLayer;

namespace _3TierDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindStudentInfoData();
            }
        }

        public void BindStudentInfoData()
        {
            StudentInfoBLL objStudentInfoBLL = new StudentInfoBLL();
            DataTable objDataTable = objStudentInfoBLL.LoadAll();
            GridView1.DataSource = objDataTable;
            GridView1.DataBind();
        }
    }
}


Finally your 3-Tier application look like.




 

Hope this is helpful. Please leave your comments for any help / suggestion.

No comments:

Post a Comment