Tuesday, January 26, 2010

What is n-Tier Architecture?

n-Tier Architecture
We will talk generally about what n-Tier architecture is, and then we will have a look at different n-Tier architectures we can use to develop ASP.NET applications and issues that arise relating to performance, scalability and future development issues for each one.

What is n-Tier architecture?
N-Tier architecture refers to the architecture of an application that has at least 3 "logical" layers -- or parts -- that are separate. Each layer interacts with only the layer directly below, and has specific function that it is responsible for.
N-Tier the word means Multiple Tier.
N-Tier Architecture is nothing but splitting your solution into different project based on the business requirement.
Why use n-Tier architecture?
Because each layer can be located on physically different servers with only minor code changes, hence they scale out and handle more server load. Also, what each layer does internally is completely hidden to other layers and this makes it possible to change or update one layer without recompiling or modifying other layers.
This is a very powerful feature of n-Tier architecture, as additional features or change to a layer can be done without redeploying the whole application. For example, by separating data access code from the business logic code, when the database servers change you only needs to change the data access code. Because business logic code stays the same, the business logic code does not need to be modified or recompiled.

Advantage:
  1. Reduce the business and the programming complexity.
  2. Easy to process

Note: tier and layer mean the same thing

An n-Tier application usually has three tiers, and they are called the presentation tier, the business tier and the data tier. Let's have a look at what each tier is responsible for.

Presentation Layer
Presentation Layer is the layer responsible for displaying user interface and "driving" that interface using business tier classes and objects. In ASP.NET it includes ASPX pages, user controls, server controls and sometimes security related classes and objects.

Business Tier
Business Tier is the layer responsible for accessing the data tier to retrieve, modify and delete data to and from the data tier and send the results to the presentation tier. This layer is also responsible for processing the data retrieved and sent to the presentation layer.

In ASP.NET it includes using SqlClient or OleDb objects to retrieve, update and delete data from SQL Server or Access databases, and also passing the data retrieved to the presentation layer in a DataReader or DataSet object, or a custom collection object. It might also include the sending of just an integer, but the integer would have been calculated using the data in the data tier such as the number of records a table has.

Often this layer is divided into two sub layers
BLL and DAL
The Business Logic Layer (BLL), and the Data Access Layers (DAL). Business Logic Layers are above Data Access Layers, meaning BLL uses DAL classes and objects. DAL is responsible for accessing data and forwarding it to BLL.

In ASP.NET it might be using SqlClient or OleDb to retrieve the data and sending it to BLL in the form of a DataSet or DataReader. BLL is responsible for preparing or processing the data retrieved and sends it to the presentation layer. In ASP.NET it might be using the DataSet and DataReader objects to fill up a custom collection or process it to come up with a value, and then sending it to Presentation Layer. BLL sometimes works as just transparent layer. For example, if you want to pass a DataSet or DataReader object directly to the presentation layer.

Data Tier
Data tier is the database or the source of the data itself. Often in .NET it's an SQL Server or Access database, however it's not limited to just those. It could also be Oracle, mySQL or even XML. In this article we will focus on SQL Server, as it has been proven to be the fastest database within a .NET Application.

Some reasons for data tier is ;
  • Security
  • Code Maintenance and Reuse
  • Scalability

How to Implement N-Tier Architecture in Web Application?
To  work with n-Tier system
  1. Create a solution.
  2. Include a web application project in it and name it as "PRESENTATION LAYER"
  3. Include a Class Library Project and name it as "DATALAYER"




  4. Include a Class Library Project and name it as "BUSINESSLAYER"
  5. Now we have 3 project in single solution
  6. Build the DataLayer.
  7. Go to Business layer and right click on reference and in reference go to Project's. Here we can see the Datalayer. Click on that and include as reference.
  8. Now in the Business Layer reference we can see the DataLayer assembly
  9. Go to Presentationlayer and right click on reference and in reference go to Project's. Here we can see the BusinessLayer. Click on that and include as reference.
  10. Now we can see the BusinessLayer Assembly in the Presentation Layer.
  11. Now we can access all the methods
    DataLayer in Business Layer
    BusinessLayer in PresentationLayer



  12. This is the way we build our N-Tier Application. For example, this is a 3 Tier System.

1 comment:

  1. Thanks!! for you comment Takeshi Okada, I have other post too it would be helpful to you.

    ReplyDelete