Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

3-Tier Architecture in ASP.Net C# with step by step example

Introduction

In this post we will learn how to implement the three tier architecture in our application using Asp.Net c#. My previous post Highlight Asp.net gridview row color on mouseover using javascript and css

Explanation

Many interviews we faced the question whether you used 3-tier architecture in your application.Because it is mandatory to know the 3-tier architecture for developing a project. Three tier architecture is a separation of our application into multiple portion which is called as a layer.So we have to understand about the layer in 3-tier architecture.

Layer architecture vs Tier architecture

Layer architecture : Logical Separation of our application will be stored in the same server.

Tier architecture : Physical Separation of our application will be stored in multiple servers which will be communicated via web service or WCF services.
We may split our application into many layers which is called 'N' layered architecture. So the base for multiple layer architecture is 3-tier architecture. So before 3-tier architecture we have used two-tier architecture, which is having client - server request and response. We will keep the coding and design in same page and it is having lot of source code and we have confusion for analysing the coding and maintenance is very hard.

As mentioned in the above picture representation, While accessing a website through browser the first hit will be in the Presentation/ Application Layer. The request from Presentation/ Application Layer will be traversed to the Business Logic Layer. Then Business logic layer request traverse the request to Data Access Layer. Finally the Data Access Layer will do the communication with our SQL database and the transaction completion of the given request. The response from SQL database will reach the Data Access Layer , the Data access Layer traverse the response to Business Logic Layer. Finally the response will reach the presentation Layer. This is a vice versa request and response in 3-tier architecture.

Types of Layers in Project

1. Presentation/Application Layer.
2. Business Logic Layer.
3. Data Access Layer.

Presentation/Application Layer

Presentation Layer or Application Layer is the user interaction layer which is available for getting the inputs from the user. Asp.Net extention page (.aspx) which contains the web controls with design is called as presentation layer. This is a very important layer in a project, because even if you have complicated coding in your project but it won't be visible for an user. Attractive design is the highlight of a project to retain the user. Sometimes we search a topic in internet and a list of link will appear, we will be interest to watch only the pleasant and attractive design web sites.

Business Logic Layer

Business Logic Layer is the pillar of the project which will interact with the Presentation layer and Data access layer. Any business requirement coding will be handled in Business Logic Layer. Before traverse a request into Data access layer, this layer will validate, If validation is not through it will revert the request into presentation layer. Once the request is clear then it will traverse the request into data access layer and wait for the response of the given request.

Data Access Layer

Data Access Layer is mainly used to communicate with our database and proceed the request raised through business logic layer and revert the response to the Business logic layer. We will invoke the connection settings, SQL procedure and sql transaction queries in this layer.

3-tier architecture Advantages in Asp.Net Project

By practice keep the application and SQL in different server, because transaction request will hit in both places if it is hosted in a single server the response may get too slow if concurrent users access your website.
Project is splitted into multiple layers, we are easily to track the methods which are available in which layer pages.
Strong Object oriented programming knowledge will be easier for working on 3-tier architecture.
Without affecting presentation layer we could do the validation methods in Business layer. In Data layer stored procedure names modifications can be easily done.

Creating step by step three tier architecture example

Show the page view count in Asp.Net c# Application using 3-tier architecture.

Step 1: Create the SQL tables and stored procedures


-- Create a PageViewCount Table with count ID and Last visited date.

CREATE TABLE [dbo].[PageViewCount] (
[pvCountID] [int] NOT NULL,
[pvLastVisitedDate] [datetime] NULL
) ON [PRIMARY]
GO

--Create a Stored Procedure for Insert and update the PageViewCount Table

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE PROCEDURE dbo.Proc_Insert_Update_PageViewCount
AS
BEGIN
SET NOCOUNT ON
DECLARE @PageViewCounts INT=1
IF EXISTS(select 'X' from PageViewCount)
BEGIN
UPDATE PageViewCount SET pvCountID +=@PageViewCounts,pvLastVisitedDate=GETDATE()
END
ELSE
BEGIN
INSERT INTO PageViewCount(pvCountID,pvLastVisitedDate) values(@PageViewCounts,GETDATE())
END
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

--Create a Stored Procedure for fetch the PageViewCount Table

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE PROCEDURE dbo.Proc_Select_PageViewCount
AS
BEGIN
SET NOCOUNT ON
DECLARE @PageViewCounts INT=1
IF EXISTS(select 'X' from PageViewCount)
BEGIN
SELECT pvCountID,pvLastVisitedDate from PageViewCount
END
ELSE
BEGIN
SELECT 0,GETDATE()
END
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



Step 2: Create a sample application in Visual studio 2010 as shown below.



Step 3: Design the Page view count screen, with lables pageviewcount and lastvisited datetime

<div>
<table cellpadding="5" cellspacing="5" align="center" border="1">
<tr>
<td>
Total Page Views :
</td>
<td>
<asp:Label ID="lblPageViewCount" ForeColor="Red" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
Last Visited Time :
</td>
<td>
<asp:Label ID="lblLatVisitedDateTime" ForeColor="Red" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>


Step 4: Add a new project as a class library through solution explorer as mentioned below.





Name the class library as BOFactory and DAEngine as shown below.



Step 5: We have to give reference of these projects

DAEngine-> reference in BOFactory-> reference in Application project



Step 6: Connection strings need to be added in web config file.

<connectionStrings>
<add name="Connection" connectionString="Data Source=XXXXX;Initial Catalog=DBName;User ID=UID;Password=XXXX" providerName="System.Data.SqlClient"/>
</connectionStrings>



Download the sample project






Output










My previous post Highlight Asp.net gridview row color on mouseover using javascript and css


This post first appeared on Dotnet, please read the originial post: here

Share the post

3-Tier Architecture in ASP.Net C# with step by step example

×

Subscribe to Dotnet

Get updates delivered right to your inbox!

Thank you for your subscription

×