Tuesday, 5 September 2017

Three tier architecture in asp.net using c# example

In this tutorial post we will demonstrate how to create 3 tier architecture using asp.net c#. Three tier architecture means dividing our project into three layers that is presentation layer (UI layer), Business Layer (Logic code layer) and datalayer (Layer which connects to database).
 There are lots of mails coming every day now and then asking for 3 tier architecture implementation. So this post is especially for those developers who are looking to develop a 3 tier implementation in their projects. Three tier architecture means dividing project in three layers User Interface Layer, Business Layer and Data Layer where we separate logic,data and user interface in three division. It means tomorrow if we replace sql server database with oracle database then we need to change only data layer OR if we change or user screen from windows application to website then we need to change only our user interface layer rest everything remain the same.
Kindly Note : Developer or coder need to be well versed with use of classes and objects in c-sharp.

How it works
Presentation layer is your user interface layer where you can design your interface using html web controls or windows controls or mobile controls. It can be your website or windows application or mobile application.This layer only communicates with business layer.

Application layer or Business Layer is the middle layer or bridge layer that connects database layer and presentation layer. In this layer you can write your business logic (logic written as per business requirement) or any validation code.This layer communicates with database layer and presentation layer.

Database layer which makes the connection to the database server. In this layer you can write database connection and sql queries or stored procedures. This layer only communicates with business layer.

When user post any data from from user interface (presentation page), data first goes to the business layer there data will be validated then validated data is posted to database layer to insert into the database.

When user request any data from the database then the request is first processed by the business layer validates the request and sends to database layer then database layer forwards it to the database server and fetches necessary records. Found records are loaded by the database layer and passes it back to the business layer then business layer passes those records to the presentation layer.

Advantages of using 3 tier architecture

It makes the logical separation between business layer and presentation layer and database layer.

Migration to new graphical environments is faster.

As each tier is independent it is possible to enable parallel development of each tier by using different sets of developers.

Easy to maintain and understand large project and complex project.

Since application layer is between the database layer and presentation layer so the database layer will be more secured and client will not have direct access to the database.

Posted data from presentation layer can be verified or validated at application layer before updating it to the database.

Database Security can be provided at application layer.

Application layer or middle layer or business layer can be a protection shield to the database.

New rules or new validation rules can be defined any time and changes made to middle layer will not effect presentation layer.

Define any logic once within the business layer and that logic can be shared among any number of components in the presentation layer.

We can display only necessary methods from business layer in the presentation layer.

We can hide unnecessary methods from business layer in the presentation layer.

Easy to apply object oriented concept

Easy to update data provider queries.

Disadvantage of using 3 tier architecture

Time consuming : To implement even small part of application it will consume lots of time.

Need good expertise in object oriented concept (classes and objects).

It is more complex to build.

Why to use 3 tier architecture in projects

To Manage Large Projects becomes difficult : Lets suppose you have created a 1000 screen project without using 3 tier architecture and coded all codes in the presentation layer then suppose over period of time you want to migrate to windows to web application then again you need to write code  for all 1000 screens OR lets suppose tomorrow you database provider changes from sql server to oracle server then again where ever you have written sql connection in all 1000 screens in all that you need to make changes. Frankly speaking changing code in 1000 screens or re-writing code in 1000 screens is not easy it is time and money consuming job because your client or company will not pay that much or give that much time to code every time.

Difficult to understand : If a new developer joins you for the same project then it becomes very difficult to make him/her understand about the code and project (inspite of having documentation).

No proper templatization : It becomes very difficult to find any specific part of project code.

Security Threat : Our database server is not secured because database server code is directly written in the page level.

We are human beings we cannot remember all those codes which we have written in the different pages over the years (Large projects). So in order to maintain flexibility, migration facility, data security, proper maintenance and to understand project easily even after the years it is best practices to use 3 tier architecture in projects.

For small projects with 5 to 20 screens it is not necessary to implement three tier architecture. But for large projects it is recommended to use three tier implementation.

Step by step 3 tier architecture example

In this demonstration of the code we will use website as our presentation layer and class library project for both business layer and database layer.
 .
   So friends i hope you understood this complete tutorial on Three Tier Architecture in ASP.NET using c# language. If you have any doubts or queries kindly feel free to ask me through your comments.


             Coding Part I will Upload my next Article..
                                                             



No comments:

Post a Comment