ASP.Net Multi-Tier Architecture : With Product sample
In this post I'll explain how to manage a much more better structure in your web applications with Multi-Tier architectures. In this post we are going to experience the Three-Tier architecture. Even-though we name this as a Three-Tier architecture we can divide it into more levels to manage the complexity of the project.
This is the architecture we are going to implement in this post.
Responsibilities of each layers:
1. Application Layer - This is the top level and the layer which the end user getting access to. Which simply means this is the web site interface which include all the aspx files, css files and other scripts.
2. Business Layer - This is the main layer of the application which holds the business logic. I have divide this layer in to 2 layers.
3. Database Layer - This layer use to communicate with the application database.
Lets begin...
To demonstrate this architecture we are going to create a simple web application which allow user to add new product to the system database.
Application Layer
Go to Visual Studio and create an ASP.NET Empty Web Application under Web section. Give a suitable name. I'll give as ThreeTierApp.
You are done creating the 'Application Layer'.
Lets add the connection string to the Web.config file.
Lets add the other layers. Since these layers doesn't need any interface we are going to create Windows Class Library projects. Go to Visual Studio, right click on the solution and from the pop up window, select Class Library under Windows section. Since we are going to create the Database Layer lets give this class library name as DatabaseLayer. Chang the default class name to ConnectionManager.
We are going to use this class to retrieve the connection string. Add the following code to ConnectionManager.cs to retrieve the connection string from the Web.config file.
Business Layers
In our solution business layer have 2 layers.
1. Business Domain Layer
This will hold the domain objects. Ex- Product
Select the solution and create new Class Library Project as we did when creating Database Layer.
Give name as BusinessDomainLayer.
Rename the class to 'Product' and add the bellow code
This layer will hold all the logic of the solution.
Select the solution and create new Class Library Project.
Give name as BusinessLogicLayer.
Rename the class to 'ProductManager' and add the bellow code.
Lets do the database related functions. Go to the DatabaseLayer again and create a class name as 'ProductDataAccessManager and add the following code.
When you adding the above code the compiler will complain that the Product cannot be found. To solve this add the reference to this project. Product is in the BusinessDomainLayer. So add this reference to the DatabaseLayer references.
The above code will add product details to Products table by executing a Stored Procedure in SQL Server.
This is the SP which the code is executing.
The purpose of this post to give an idea about the architecture of the Three-Tier (Multi-Tier) Architecture. So I don't going to add much business logic for this.
Go to the BusinessLogicLayer, and add the bellow code to the ProductManager.cs
Ok. Now lets add the code to ApplicationLayer. Lets create a web form in the Application Layer, lets called it Home. Design the Home.aspx to view like bellow.
And add the following to the Home.aspx.cs
This is the architecture we are going to implement in this post.
Responsibilities of each layers:
1. Application Layer - This is the top level and the layer which the end user getting access to. Which simply means this is the web site interface which include all the aspx files, css files and other scripts.
2. Business Layer - This is the main layer of the application which holds the business logic. I have divide this layer in to 2 layers.
- Business Logic Layer - This layer holds the pure business logic itself.
- Business Domain Layer - This holds the domain objects of the solution which require in the business logic. (Ex: Product, User)
3. Database Layer - This layer use to communicate with the application database.
Lets begin...
To demonstrate this architecture we are going to create a simple web application which allow user to add new product to the system database.
Application Layer
Go to Visual Studio and create an ASP.NET Empty Web Application under Web section. Give a suitable name. I'll give as ThreeTierApp.
You are done creating the 'Application Layer'.
Lets add the connection string to the Web.config file.
Database Layer (Data Layer)
Lets add the other layers. Since these layers doesn't need any interface we are going to create Windows Class Library projects. Go to Visual Studio, right click on the solution and from the pop up window, select Class Library under Windows section. Since we are going to create the Database Layer lets give this class library name as DatabaseLayer. Chang the default class name to ConnectionManager.
We are going to use this class to retrieve the connection string. Add the following code to ConnectionManager.cs to retrieve the connection string from the Web.config file.
public static SqlConnection GetConnection() { return new SqlConnection(ConfigurationManager.ConnectionStrings["ProductDBConnectionString"].ConnectionString); }
Business Layers
In our solution business layer have 2 layers.
1. Business Domain Layer
This will hold the domain objects. Ex- Product
Select the solution and create new Class Library Project as we did when creating Database Layer.
Give name as BusinessDomainLayer.
Rename the class to 'Product' and add the bellow code
public class Product { public int Id { get; set; } public String Name { get; set; } public Double Price { get; set; } }2. Business Logic Layer
This layer will hold all the logic of the solution.
Select the solution and create new Class Library Project.
Give name as BusinessLogicLayer.
Rename the class to 'ProductManager' and add the bellow code.
public string AddProduct(Product product) { try { ProductDataAccessLayer.AddProduct(product); return "Product Added !"; } catch { return "Error occured when adding product !"; } }
Lets do the database related functions. Go to the DatabaseLayer again and create a class name as 'ProductDataAccessManager and add the following code.
public static void AddProduct(Product product) { SqlParameter name = new SqlParameter("@name", product.Name); SqlParameter price = new SqlParameter("@price", product.Price); SqlConnection conn = ConnectionManager.GetConnection(); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "AddProducts"; cmd.Parameters.Add(name); cmd.Parameters.Add(price); cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close(); }
When you adding the above code the compiler will complain that the Product cannot be found. To solve this add the reference to this project. Product is in the BusinessDomainLayer. So add this reference to the DatabaseLayer references.
The above code will add product details to Products table by executing a Stored Procedure in SQL Server.
This is the SP which the code is executing.
USE [ProductDB] GO /****** Object: StoredProcedure [dbo].[AddProducts] Script Date: 02/11/2016 3:30:29 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[AddProducts] @name varchar(50), @price decimal(8,2) AS BEGIN SET NOCOUNT ON; INSERT INTO Products VALUES (@name, @price); END
The purpose of this post to give an idea about the architecture of the Three-Tier (Multi-Tier) Architecture. So I don't going to add much business logic for this.
Go to the BusinessLogicLayer, and add the bellow code to the ProductManager.cs
public string AddProduct(Product product) { try { ProductDataAccessManager.AddProduct(product); return "Product Added !"; } catch { return "Error occured when adding product !"; } }
Ok. Now lets add the code to ApplicationLayer. Lets create a web form in the Application Layer, lets called it Home. Design the Home.aspx to view like bellow.
And add the following to the Home.aspx.cs
protected void Page_Load(object sender, EventArgs e) { lblMessage.Text = ""; } protected void btnAddProduct_OnClick(object sender, EventArgs e) { var name = txtName.Text; var price = txtPrice.Text; if (name != null && price != null) { Product product = new Product() { Name = name, Price = double.Parse(price) }; ProductManager pm = new ProductManager(); lblMessage.Text = pm.AddProduct(product); } }
We are done here.
Comments
Post a Comment