Saturday, August 20, 2016

Using Three tier Architecture in ASP.NET

In earlier days, people used to write all codes in one place in code behind without
separating the code into different layers. Then started using Three tier architecture
by which code is classified or separated as 3 different layers, Presentation Layer,
Business Logic Layer and Data Layer.

Presentation Layer :

This is the user interface layer where all input and output operations takes place.
Here we can design our interface using controls. This layer communicates only
with Business Logic Layer.

Business Layer :

Most of the business logics perform in this layer. After getting data from Data Layer
operations like logics, validations and calculations related with the data can be placed
in this layer. We define classes and business entities in this layer.

Data Layer :

All database related operations like connecting to database, queries for execution
and getting the results from the database are placed in this layer. This layer 
is on top of the Business Logic Layer.


Advantages of 3 - tier Architecture :

-  Maintenance and modifications of large projects is easy as separated to different layers.
-  easy for code re usability
-  Since database operations are in separate layer, more security is there.

Note : Any number of tiers can be used , N- tiers based on how we classify the
code.

Next I will explain one sample of implementation of Three Tier Architecture in
ASP.NET.

In this example I am binding one gridview . Drag one GridView control to the
web page and set AutoGenerateColumns="true". 

C# Code (.aspx.cs) :

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class grid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            getData();
    }
    protected void getData()
    {
        UserBLL bll = new UserBLL();
        DataSet ds = bll.getUsers();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}

Next is the Business Layer Class which is placed inside App_Code. Its code is :

App_Code/UserBLL.cs :

using System;
using System.Data;

/// <summary>
/// Summary description for UserBLL
/// </summary>
public class UserBLL
{
    UserDAL DAL = new UserDAL();
    public DataSet getUsers()
    {
        DataSet ds = new DataSet();
        ds = DAL.getUsers();
        return ds;
    }

}

Code for Data Layer Class which is also inside App_Code is :

App_Code/UserDAL.cs :

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for UserDAL
/// </summary>
public class UserDAL
{

    public DataSet getUsers()
    {
        DataSet ds = new DataSet();
        string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;

        using (SqlConnection dbConnection = new SqlConnection(connectionString))
        using (SqlCommand dbCommand = new SqlCommand())
        {
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.CommandText = "getAgents";
            dbCommand.Connection = dbConnection;
            dbConnection.Open();

            SqlDataAdapter da = new SqlDataAdapter(dbCommand);
            da.Fill(ds);

        }
        return ds;
    }

}

Connection string is provided in web.config file inside configuration tag as :

<connectionStrings>
        <add name="conn" connectionString="Data Source=DHANYA-PC;Initial Catalog=test;
        User ID=sa;Password=123ewe" providerName="System.Data.SqlClient"/>
 </connectionStrings>

Connection string in web.config is accessed via ConfigurationManager as :

string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;

For using ConfigurationManager class, following namespace is required :
using System.Configuration;

In this example I have used Stored Procedure which is an efficient way of using 
SQL Query.

For this CommandText has to be provided like this :

 dbCommand.CommandType = CommandType.StoredProcedure;
 dbCommand.CommandText = "getAgents";

Output :



No comments:

Post a Comment