Microsoft.NET

……………………………………………….Expertise in .NET Technologies

N-Tier Architecture in ASP.NET

Posted by Ravi Varma Thumati on May 5, 2009

Introduction

Creating N-tier applications is not a new concept now. Since VB4 days Developers are building such applications. .NET provides great support for building such applications. This article is intended to give you an overview of n-tier architecture as applicable to .NET and how various technology choices offered by .NET fit in the picture.

What is N-Tier Application?

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. 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 Architecture

At first glance this division of tasks may seem to be unnecessary. However, it gives following benefits:

  • The applications get 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.

Presentation layer options

There are two main options for creating presentation layers in .NET. They are – Windows Forms or ASP.NET Web Forms.

Using windows forms you can create traditional forms based desktop applications. Windows forms applications can offer rich user interface elements and response times to the user. However, they suffer from the drawback that they need to be installed on each and every machine. Though they can be used in “smart client” mode they are more or less the same as traditional VB forms.

The most appealing option to develop presentation layer is ASP.NET web forms. Traditionally web pages suffered from the drawback of limited user interface elements. ASP.NET web server controls bridge the gap to a great extent. Controls such as DataGrid, DataList and Calendar provide rich UI in very few lines of code.

Above presentation layer options can be coded in variety of languages such as C# or VB.NET. Here, again support for multiple development languages comes handy. You can use your choice of language to develop the UI.

Business logic layer options

Business logic layer primarily consists of components that perform the task of business validation, business workflow and other similar things.

.NET components form this layer. In case you have invested a lot in COM components then you can also use them in .NET via interoperability. However, this will degrade the performance.

ASP.NET Web Service can also serve as business logic layer. However, they should not be used as replacement to components in all the situations. They should be used only if the business validation happens at some “external” place than your network.

The components you develop need not always reside on the same machine. Using .NET Remoting you can create distribute them on multiple physical machines.

Data access layer options

This layer deals with data manipulation actions such as inserts, updates, deletes and selects. The data mentioned above can reside in RDBMS or XML files or even flat files. You should design data access layer in such a way that other layers need not have any knowledge about underlying data store.

ADO.NET is the data access technology under .NET. Though ADO.NET allows connected data access via DataReader classes more focus is on disconnected data access. DataSet plays a key in this mode. In some rare cases you can also use ADO for data access but its use should have valid reasons. Do not use ADO just because you like Recordset!

Again .NET components form this layer. As stated earlier you may also use classic COM components.

Web services can also form data access layer. This is especially true if your database do not have a data provider. In such cases you can write some custom code to connect with the data and populate DataSet with it and then return DataSet to the caller.

In addition to ADO.NET you can also make use of built-in RDBMS capabilities such as stored procedures and functions.

Passing data from one layer to another

Passing data from one layer to another is required in almost all the cases. Traditionally developers used comma separated strings, arrays and disconnected recordsets to achieve this. In .NET, DataSet provides an excellent way to pass your data across layers. You can even create the DataSet programmatically and populate it with your data. If you like objects a lot then you can make use of “Typed DataSets”. Typed DataSet is nothing but a class derived from DataSet that exposes the tables and rows as objects.

The overall picture

Following figure shows how all these parts fit in:

n-tier

Advertisements

One Response to “N-Tier Architecture in ASP.NET”

  1. This really essentially a lovely stuff. An doc something like this provides precisely how deep the concept is actually realized by publisher.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: