Saturday, December 6, 2014

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.

3-tier-1
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.

PageViewProject1


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.

PageViewProject2


PageViewProject3


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

PageViewProject4


Step 5: We have to give reference of these projects

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

PageViewProject5


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
Download






Output
PageViewProject6










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

11 comments:

  1. Good Explanation : Let me show you one example which generally people wants to try on their own but they can't make it...

    Login Code with 3 Tier Architecture
    ==========================

    Create Login Page with 3 Tier Architecture

    ReplyDelete
  2. Thanks for sharing such informative guide on .Net technology. This post gives me detailed information about the .net technology. I am working as trainer in leading IT training academy offering Dot Net Training in Chennai and i use your guide to educate my students. .Net Training in Chennai

    ReplyDelete
  3. I have read your blog its very attractive and impressive. I like it your blog.

    Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai Dot Net Training in Chennai


    Dot Net Online Training Dot Net Online Training LINQ Online Training LINQ Online Training ASP.NET Online Training ASP.NET Online Training

    ReplyDelete
  4. Microsoft develops software called DOTNET. It is a software framework. DOTNET mainly runs on Microsoft windows. It is similar to Java. .Net is most preferred programming language among software developers all over the world. It is also considered as most trusted and effective platform to build high performing desktop or enterprise application.
    Thanks,
    DOTNET Training in Chennai | DOTNET course in Chennai | DOTNET Training Institute in Chennai

    ReplyDelete
  5. Nice post. Very interesting to read. Thank you for Sharing.
    erp software in chennai

    ReplyDelete
  6. Thanks for sharing this post with us it is a worth read.. Thanks for the tutorial, I been searching for this for a long time, At last reached to the got post. Thank you giving a information about Android Development
    Graphic design course in chennai

    ReplyDelete
  7. This software has a set of predefined tools which first organize the problems on the basis of their difficulty and then it searches for the resources used for solving the problems. cheap Revit 2018

    ReplyDelete
  8. Great blog., I got some data about asp.net really its so nice to read.. Thanks for sharing, kindly keep updating such a nice blog.. Software Testing Training in Chennai | Six Sigma Certification Training in Chennai | Selenium Training in Chennai

    ReplyDelete
  9. thank you for such a great article with us. hope it will be much useful for us. please keep on updating..Web Designing Training Institute in Chennai | Web Designing Training Institute in Velachery.

    ReplyDelete

Followers