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:
int CategoryId { get;
set; }
string CategoryName { get;
set; }
int ProductId { get; set; }
string ProductName { get;
set; }
decimal Cost { get; set; }
public class Category : ICategory
private int
_CategoryId = 0;
private string
_CategoryName;
get { return
_CategoryId; }
set { _CategoryId = value;
}
public string
CategoryName
get { return
_CategoryName; }
set { _CategoryName = value;
}
public class Products : Category,
IProducts
private string
_ProductName;
get { return
_ProductId; }
set { _ProductId = value;
}
public string
ProductName
get { return
_ProductName; }
set { _ProductName = value;
}
Data Access Layer:
DataSet GetCategory() { return
null; }
void InsertCategory(ObjectsLayer.Category ObjCategory) { }
void UpdateCategory(ObjectsLayer.Category ObjCategory) { }
void DeleteCategory(ObjectsLayer.Category ObjCategory) { }
DataSet GetProducts() { return
null; }
void InsertProduct(ObjectsLayer.Products ObjProducts) { }
void UpdateProduct(ObjectsLayer.Products ObjProducts) { }
void DeleteProduct(ObjectsLayer.Products ObjProducts) { }
public class Category:ICategory
string query = string.Empty;
public DataSet
GetCategory()
query = "select CategoryId,CategoryName
from Category ";
da = new SqlDataAdapter(query,
con);
public void
InsertCategory(ObjectsLayer.Category
ObjCategory)
query = "Insert into
Category(CategoryName) Values('" + ObjCategory.CategoryName + "')";
cmd = new SqlCommand(query,
con);
public void
UpdateCategory(ObjectsLayer.Category
ObjCategory)
query = "Update Category Set
CategoryName='" + ObjCategory.CategoryName + "' where CategoryId='" +
ObjCategory.CategoryId + "'";
cmd = new SqlCommand(query,
con);
public void
DeleteCategory(ObjectsLayer.Category
ObjCategory)
query = "delete from Category where
CategoryId='" + ObjCategory.CategoryId + "'";
cmd = new SqlCommand(query,
con);
public class Products:Category,IProduct
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);
public void
InsertProduct(ObjectsLayer.Products
ObjProducts)
query = "insert into
Products(ProductName,Cost,CategoryId)values('" +
ObjProducts.ProductName + "','" +
ObjProducts.Cost + "','" +
ObjProducts.CategoryId + "')";
cmd = new SqlCommand(query,
con);
public void
UpdateProduct(ObjectsLayer.Products
ObjProducts)
query = "Update Products Set
ProductName='" + ObjProducts.ProductName + "',Cost='"
+ ObjProducts.Cost.ToString() + "',CategoryId='"
+ ObjProducts.CategoryId + "' where
ProductId='" + ObjProducts.ProductId + "'";
cmd = new SqlCommand(query,
con);
public void
DeleteProduct(ObjectsLayer.Products
ObjProducts)
query = "delete from Products where
ProductId='" + ObjProducts.ProductId + "'";
cmd = new SqlCommand(query, con);
Business layer:
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);
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.Collections.Generic;
using System.Web.UI.WebControls;
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.