Well, the architecture I am going to demonstrate here is just enhancement of 3-Tier archicture. In this architecture; you no need of writing long function parameters throughout the layers (as in traditionally 3-Tier archicture has to) and the actual objects of the application will be in a separate tier so that in future you can separately use these objects for enhancements. Change in the object definition can be done without touching the entire Business Access Layers ............

In this application, I am going to take example of a Person that will have 3 properties: FirstName, LastName, Age. We will create a separate pages to insert these records (default.aspx) into database and list,update,delete records (list.aspx) from database.

Here, we are first declaring 4 variables for corresponding properites and defining properties for them.This is your Business Object with all its properties/attributes to work with. Next step is to create Data Access Layer.

Data Access Layer [DAL - PersonDAL.cs]

The way you created BO folder inside App_Code folder, create another folder named DAL. Create a .cs file inside it and name it as PersonDAL.cs

Write following code inside it (You can copy-paste).

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

In this class file, we have Insert, Update, Delete, Load methods. In this class file, first I am getting the connection string from the web.config file in a class level variable called connStr and using the same string in all my methods to open the connection.

For simplicity reason, I have not shown the code for Stored Procedure, however you can get the complete database and code by downloading the Source Code files. This was your Data Access Layer. Till now you have your Business Object and Data Access Layer ready. Now lets go to the third layer and create Business Access Layer.

Business Access Layer [BAL - PersonBAL.cs]

Again, right click App_Code folder and add a new folder named BAL. Create a new class file inside it and name it as PersonBAL.cs. Write following code inside it.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

Here, we are creating separate methods each for respective PersonDAL.cs methods here. As in our case we don't have any business logic, so we are just instantiating the Data Access Layer objects, using its methods and and returning to UI (fourth layer, described later on).

You must have noticed here that in the try catch block, I am just writing throw; statement. This is because when any error will occur it will be send to the calling layers (in our case UI) and there we will handle it.

Till now, we have BO, BAL and DAL ready. Now we are left with our application face, I mean UI. Lets first create default.aspx file that will contain one form and textboxs that will be used to enter records.

User Interface - [UI]-Default.aspx

Create a separate folder in your UI solution named 4-Tier and add one .aspx page called Default.aspx (Picture - 2). In this page, we will write ASP.NET code to render textboxes and buttons. OnClick event of the button we will calll AddRecords method that will ultimately insert the records into database. Below is the code to render the asp.net form.

In the above method, I am doing following things mainly:
1. Instantiating BAL object
2. Instantiating BO object
3. Settinng properties of BO object by the textbox values
4. Calling Insert method of the BAL object and passing BO object as parameter [pBAL.Insert(person)] in try block
5. Checking for number of records affected, If the number is more than zero, I am writing Success message otherwise Duplicate records found.
6. If any layer will throw any error, I am catching it and displaying to the user in throw block.
7. Whatever objects I had instantiated, I am specifying their values to null to let the GC know that I am no more going to use them.

User Interface - [UI]-List.aspx

In this page, I am going to use a GridView to List, Modify, Sort and Delete records from the database. Create an .aspx page in the same 4-Tier folder named List.aspx (Picture - 3). Following is the code for the GridView that will do data manipulation for us.

On the OnRowEditing, OnRowUpdating, OnRowCancelEdit, OnSorting and OnRowDeleting events we are calling respective methods to do data manipulation. Following are codes to bind the GridView and methods that will fire on GridView events.

Now we have done all our home work, Its time to enjoy now. Just build the application by pressing Ctrl+Shift+B, once it shows Build Succeeded in the status bar at the bottom, you are ready to enjoy your 4-Tier architecture application.

Browse it as http://localhost/4-Tier (Provided you have created "4-Tier" virtual directory in your IIS or you can choose your own virtual directory name), you should see your screens as shown above (default.aspx and list.aspx pages).

Try manipulating data and see how effectively its happening. This was a simple application, based on this architecture, you can create larger application by adding separate objects and their corresponding tiers.

In order to use namespace in different tiers, you should define your namespace in respective tiers objects and use those namespace by using statement whereever you need.

I have also attached a ready to use sample code of this example, Download it and use it!!!

Your BO "tier" is what I call a set of DTOs (Data Transfer Objects), it's neither new nor a "tier": DTOs are used to pass data from one tier to the other: they have no embedded "business logic", they're just data "containers".

Author's response: Please see the 2nd paragraph of this article. It explains the purpose and its benefits. Thanks.

This is normal, everyday 3-tier: Presentation, BLL, and DAL. The object you're passing back and forth is not a tier any more than a String or Integer would be. But, it's a fine article for someone who wants a simple explanation of DTOs.

Author's response: Please see the 2nd paragraph of this article. It explains the purpose and its benefits. Thanks.

This sample suffers from both SQL injection and HTML injection making it a very insecure. Get rid of building those SQL strings and use parameterised queries and ensure that output added to the HTML is correctly HttpUtility.Encoded.

Author's response: Thanks for your feedback Damieng. Sql statement has been used here to make the tutorial simple, it is informed in the DAL description. Regards

Basically, it's all about components. If the UI (presentation) logic is mixed up with the business (task) logic, this is going to be a bear to do. You'll have to slog through all that code and filter out the business code from the presentation code. But if you put these into separate components (n tiers), you can easily plug your business code into any type of user interface or API.

Think of each tier as a black box that only has functionality that is related to what it needs to do. The tiers can just be logical or physical. In the example above it's move a physical tiers.When it comes to performance this would be very very fast and scalable too.

There's more to it, of course, but that's the basic principle Software evolves. Build it for evolution, extensibility, scalability.......and I am very sure this 4 tier architecture will helps us a great deal in improving the future of application.

Hello,
This is very good article. I have some questions.
1. Where to maintain transaction.
2. If I am using inheritance. Then their is need to create a diffent DAL classes base class and each derived cass.

Hi, this is good, thanks. But i have a concern. I am speaking from a Windows App point of view but have yet to try this on a web app.

Supposedly, you have created separate project for each of the Layers. Let's just say the BO and BAL are in the same project. To use your objects you would have to add reference to it. However this would result in a circular dependency which is not allowed. If you had reference the dll file instead, this would also result in a error that you have made more than a file reference to a assembly when you reference the assembly in your presentation layer.

I am referring to your statement when you mentioned "You can create a separate projects for these tiers and add into forth tier (UI) solution."

Thanks for your response. If you are creating separate project for all these layers, you will need following:

1. You will need to create separate projects for all layers BO, BAL, DAL, UI.
2. You will need to add reference of BO into all UI, BAL and DAL. If you will have BO and BAL into same project then you will get circular reference error.
3. Your BO is nothing but a your object Entities layer that will work as a passing objects from one layer to another layer (Mostly similar to DTO but represent all entities of your application)

Any real world application has Business Logic. This does not fit into the above 4 tiers, necessitating another tier - commonly known as the Business Logic Layer or BLL. Am I wrong or does this meam an implementation of the above would really be a 5 tier architecture when BLL is included. Also, the BAL does seem a bit redundant. Could there just be a DataBaseController class that takes the BOL object as a parameter and uses the DAL object to perform operations.

I think I won't be able to explain these things in details right now, but you can visit http://www.developer.com/design/article.php/10925_1502691_4 to know about Strategy Design pattern that might help you to design yourself.

@Ko_min_min:
I do not think its by default possible to keep different tiers in separate machine, however you can use remoting (an old concept), WCF Serivces, Web Service to spread the load of different tiers on different machines.

In general this is not done instead several servers are hosted with same application using load balancer.

Hi Sheo , I have worked in 3 tire architecture but this is new to me. Thanks 4 this.
But I have some confusion about the use of BO and BAL tires. In three tire we use 1 business object layer which is and in your code you divided it into 2 tires.
how this step modify our application and which respect?

I wonder if you are still active on this post. Nevertheless, it is a good post capturing the basics of 4-tier. I had a query here -- though not directly related to n-tier.

Usually the .config file of the DAL project will be containing the connection-string and other such details. After deployment of application, now a person having higher-privilege rights can just go to appropriate path -- right click on the config file -- open in notepad and thus come to know sensitive data as username/password of DB from connection string and other such details.

What is the best-practice you follow to overcom such a limitation?
---
Regards,
Mukesh

Dear All,
I am very new in ASP.Net, do you think I should make a class or what you called BO, DAL, BAL and UI for each Table?
Let say, I have tables for my Contact Database and they are Employee , Customer and Profile , Should I make a Class or what you called BO, DAL, BAL and UI for each table in my database?
Could you please answer and I would be very grateful if you answer my questions immediately.

Hi Sheo,
if you are still active in this article, can u plz explain me.

you have used Person object to insert,update and delete and why have you used datatable for listing and binding to the Grid? why not use List of Person objects to bind to Grid, so we will be using BO in all situtation. is it bcoz data table is convinient in sorting and paging?
Thanks

You are correct, just to quickly put together I have used DataTable. Ideally we can use generic list objects to bring the collection of Person from DAL through BAL and populate it to the Grid.

This article was written way back in 2007 and I guess at that time we were started talking about Generic list (if I remember correctly) Now generic list support sorting and paging as well through LINQ to it would be much easier.

hi sheo,
Can you please send me the code for search button in list.aspx to retrive one row from database.this will help me for my project.
Thanks in Advance.
Mail address: sivarajan.kanakasabai@gmail.com

Hi i used this example to creating another application but it is giving error:while updating and deleting
updating:
Server Error in '/' Application.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index