In n-tier architecture the entire application is divided in several
pieces. These pieces can be logical or physical. Each piece performs a
specific task such as displaying user interface or data access. There
can be any number of tiers or layers of such pieces. Hence, the name
n-tier (Note that many times the terms tier and layer are used
interchangeably). However, most commonly applications have 3 distinct
tiers or layers. They are:
- Presentation Layer
- Business Logic Layer
- Data Access Layer
As you can guess, presentation layer is nothing but a piece
of software that deals with user interface of your application.
Displaying data to the end user and allow them to interface with it is
the core functionality of this layer.
In most of the cases the data entered by the end user needs some
kind of validation or further processing. This is the responsibility of
Business Logic Layer.
Finally, your application data needs to be stored and retrieved
in some data store (RDBMS, XML etc.). This task is handled by Data
Access Layer.
In short, the process works like this:
- User requests for some application data.
- The data access layer retrieves the data and is forwarded to
the presentation layer via business logic layer. Sometimes data access
layer gives this data directly to presentation layer.
- Presentation layer receives the data to be displayed via business logic layer.
- The user changes the data and initiates the appropriate action (such as insert, or update).
- The business logic layer validates the data submitted by the user.
- If the data is valid it is handed over to data access layer for updating into the database.
Advantages of N-Tier ArchitectureAt first glance this division of tasks may seem to be unnecessary. However, it gives following benefits:
- The applications gets divided in logically isolated pieces reducing
tight coupling between the UI, business processes and database.
- Change in the underlying database and data access methods do
not have any effect on the presentation layer or client application.
- Client application no longer has SQL statements embedded in it. This makes it de-coupled from rest of the application.
- Table and column names can be effectively eliminated from the client-side code.
- The client application is unaware from where data comes (location transparency).
- It becomes easier to modify or extend your application, without breaking or recompiling the client-side code.
The downside of n-tier architecture is that you need to create many
isolated classes and pieces of software. However, benefits of n-tier
applications will far outweigh its disadvantage.