Saturday, October 13, 2012

THREE TIRE ARCHITECTURE

Click Here to download sample project

A three tire architecture application is divided into three platforms namely Data Access Layer, Business Layer and Presentation Layer.
  • Data Access Layer (DAL):This layer performs the operations related to data transfer between business layer and database.
  • Business Layer (BL):This layer performs business logics if necessary or else it just transfers data between the presentation layer and data access layer.
  • Presentation Layer:This layer is used to display the related data to the users.  This layer gets or supplies necessary data to the business layer and presents it to the user with the respective presentation technology.

Advantages of Three tire Architecture:

  • Code Reusability – After writing DAL or BL these logics can be used for any kind of technologies in dotnet.  For example, if you are developing a social networking site in three tire architecture and if you choose asp.net as your front end technology then the same DAL and BL codes can be used to develop the same application for mobile apps and windows applications.
  • Better Exception Handling – Exceptions can be handled easily by identifying them easily due to layered coding.  For example if data is not retrieved then we can check DAL for exceptions.
  • Better Exception Reporting – If we use layers we can report the exceptions to the respective department about the exception.



Different models of three tire architecture (Code level / Development level):

  • Using App_Code – By grouping class of different layers using namespaces we can create three tire architecture.
  • Using Assembly – By creating each assembly for each layer we can create three tire architecture.

Let’s see an example of creating a three tire architecture using App_Code model

Sample Database:



Objects:
namespace ObjectsLayer
{
    interface ICategory
    {
        int CategoryId { get; set; }
        string CategoryName { get; set; }
    }
    interface IProducts
    {
        int ProductId { get; set; }
        string ProductName { get; set; }
        decimal Cost { get; set; }
    }
    public class Category : ICategory
    {
        private int _CategoryId = 0;
        private string _CategoryName;
        public int CategoryId
        {
            get { return _CategoryId; }
            set { _CategoryId = value; }
        }
            
        public string CategoryName
        {
            get { return _CategoryName; }
            set { _CategoryName = value; }
        }

    }
    public class Products : Category, IProducts
    {
        private int _ProductId;
        private string _ProductName;
        private decimal _cost;

        public int ProductId
        {
            get { return _ProductId; }
            set { _ProductId = value; }
        }
    
        public string ProductName
        {
            get { return _ProductName; }
            set { _ProductName = value; }
        }
      
        public decimal Cost
        {
            get { return _cost; }
            set { _cost = value; }
        }
    }
}

Data Access Layer:
namespace DAL
{
    interface ICategory
    {
        DataSet GetCategory() { return null; }
        void InsertCategory(ObjectsLayer.Category ObjCategory) { }
        void UpdateCategory(ObjectsLayer.Category ObjCategory) { }
        void DeleteCategory(ObjectsLayer.Category ObjCategory) { }
    }
    interface IProduct
    {
        DataSet GetProducts() { return null; }
        void InsertProduct(ObjectsLayer.Products ObjProducts) { }
        void UpdateProduct(ObjectsLayer.Products ObjProducts) { }
        void DeleteProduct(ObjectsLayer.Products ObjProducts) { }
    }
    public class Category:ICategory
    {
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        SqlDataReader dr;
        DataSet ds;
        string query = string.Empty;
        public DataSet GetCategory()
        {
            query = "select CategoryId,CategoryName from Category ";
            da = new SqlDataAdapter(query, con);
            ds = new DataSet();
            da.Fill(ds);
            return ds;
        }

        public void InsertCategory(ObjectsLayer.Category ObjCategory)
        {
            query = "Insert into Category(CategoryName) Values('" + ObjCategory.CategoryName + "')";
            con.Open();
            cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }

        public void UpdateCategory(ObjectsLayer.Category ObjCategory)
        {
            query = "Update Category Set CategoryName='" + ObjCategory.CategoryName + "' where CategoryId='" + ObjCategory.CategoryId + "'";
            con.Open();
            cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }

        public void DeleteCategory(ObjectsLayer.Category ObjCategory)
        {
            query = "delete from Category where CategoryId='" + ObjCategory.CategoryId + "'";
            con.Open();
            cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    public class Products:Category,IProduct
    {
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        SqlDataReader dr;
        DataSet ds;
        string query = string.Empty;
        public DataSet GetProducts()
        {
            query = @"SELECT Products.ProductId,Products.CategoryId,Products.ProductName,Products.Cost,Category.CategoryName FROM
                        Products INNER JOIN Category
                        ON Products.CategoryId=Category.CategoryId";
            da = new SqlDataAdapter(query, con);
            ds = new DataSet();
            da.Fill(ds);
            return ds;
        }

        public void InsertProduct(ObjectsLayer.Products ObjProducts)
        {
            query = "insert into Products(ProductName,Cost,CategoryId)values('" + ObjProducts.ProductName + "','" + ObjProducts.Cost + "','" + ObjProducts.CategoryId + "')";
            con.Open();
            cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }

        public void UpdateProduct(ObjectsLayer.Products ObjProducts)
        {
            query = "Update Products Set ProductName='" + ObjProducts.ProductName + "',Cost='" + ObjProducts.Cost.ToString() + "',CategoryId='" + ObjProducts.CategoryId + "' where ProductId='" + ObjProducts.ProductId + "'";
            con.Open();
            cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }

        public void DeleteProduct(ObjectsLayer.Products ObjProducts)
        {
            query = "delete from Products where ProductId='" + ObjProducts.ProductId + "'";
            con.Open();
            cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

Business layer:
namespace BL
{
       public class Category
       {
       
        DAL.Category dalCat = new DAL.Category();
        public DataSet GetCategory()
        {
            return dalCat.GetCategory();
        }
        public void InsertCategory(ObjectsLayer.Category ObjCategory)
        {
            dalCat.InsertCategory(ObjCategory);           
        }
        public void UpdateCategory(ObjectsLayer.Category ObjCategory)
        {
            dalCat.UpdateCategory(ObjCategory);
        }
        public void DeleteCateogry(ObjectsLayer.Category ObjCategory)
        {
            dalCat.DeleteCategory(ObjCategory);
        }
       }
    public class Products
    {
        DAL.Products dalpro = new DAL.Products();
        public DataSet GetProduct()
        {
            return dalpro.GetProducts();
        }
        public void InsertProduct(ObjectsLayer.Products ObjPro)
        {
            dalpro.InsertProduct(ObjPro);
        }
        public void UpdateProduct(ObjectsLayer.Products ObjPro)
        {
            dalpro.UpdateProduct(ObjPro);
        }
        public void DeleteProduct(ObjectsLayer.Products ObjPro)
        {
            dalpro.DeleteProduct(ObjPro);
        }
    }
}

Presentation Layer
In this layer you can use the business layer as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ObjectsLayer;
using BL;

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

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        ObjectsLayer.Category ObjCategory = new ObjectsLayer.Category();
        ObjCategory.CategoryName = txtname.Text.Trim();
        blcat.InsertCategory(ObjCategory);
    }
}

Conclusion:
Using this three tier architecture as your code model will be easy but while developing it will be some what lengthy process.