creating asp.net applications with n-tier architecture - codeproject
DESCRIPTION
Creating ASP.net Applications With N-Tier Architecture - CodeProjectTRANSCRIPT
-
home quick answers discussions features community
helpSearch for articles, questions, tips
Articles Web Development ASP.NET General
Article
Browse Code
Stats
Revisions (5)
Alternatives
Comments &
Discussions (48)
About Article
This article describes how
to build ASP.NET
applications using n-tier
architecture.
Type Article
Licence CPOL
First Posted 13 Aug 2012
Views 48,115
Downloads 4,845
Bookmarked 79 times
C# ASP.NET Beginner
N-Tier
Prev Next
Creating ASP.NET Applications with N-Tier
ArchitectureBy Rahul Rajat Singh, 20 Aug 2012
Download demo - 760.7 KB
Introduction
This article describes how to build ASP.NET applications using n-tier architecture. The benefits of
having n-tier architecture is that all the modules having dedicated functionality will be
independent of each other. Changing one tier will not effect other tiers and there is no single
point of failure even if some tier is not working.
Background
In a typical n-tier application there will be 4 Layers. The bottom most layer is the Data layer which
contains the tables and stored procedures, scaler function, table values function. This Data layer
is typically the database engine itself. We will be using as the data layer in our
example.
On top of Data Layer, we have a Data Access Layer (DAL). This layer is responsible for handling
Database related tasks i.e. only data access. This is created as a separate
solution so that the changes in only need the recompilation of DAL and not the complete
website. The benefit of having this layer as a separate solution is that in case the database engine
is changes we only need to change the and the other areas of the website need not be
changed and recompiled. Also the changes in other areas outside this solution will not demand
for recompilation.
On top of DAL, we have our . contains all the calculations
and Business Rule validations that are required in the application. It is also in a separate solution
for the reason that if the Business rules change or the calculations change we only need to
recompile the and the other layers of the application will remain unaffected.
Finally on top of we have our Presentation Layer. The Presentation layer for an ASP.NET web
forms application is all the Forms (pages and their code behinds) and the classes
contained in the App_Code folder. The Presentation layer is responsible for taking the user input,
showing the data to the user and mainly performing input data validation.
Note: Input data filtration and validation is typically done at the Presentation Layer(Both client
side and server side). The business Rule validation will be done at the .
So to visualize the above mentioned architecture:
4.76 (33 votes)
Sign up for our free weekly Web Developer Newsletter.
articles
Creating ASP.NET Applications with N-Tier Architecture - CodeProject http://www.codeproject.com/Articles/439688/Creating-ASP-NET-appli...
1 of 12 12-02-2013 09:17
-
Top News
How Newegg crushed the
shopping cart patent and
saved online retail
Get the Insider News free each
morning.
Related Articles
N-Tier Architecture and Tips
Walkthrough: Creating an
N-tier Data Application with a
ASP.NET Presentation Tier
A N-Tier Architecture Sample
with ASP.NET MVC3, WCF, and
Entity Framework
Architecture Guide: ASP.NET
MVC Framework + N-tier +
Entity Framework and Many
More
Back to the Basics: Exception
Management Design Guideline
for N-tier ASP.NET Applications
N-Tier application using
Managed C++ and ASP.NET - A
Tutorial
Open Source Extensible
Enterprise n-tier Application
Framework with Multilayered
Architecture
Using LINQ to SQL in N-Tier
Architectures
Implementing a CLR Stored
Procedure in a LINQ based
n-tier architecture
A Tool to create N-Layer
Architecture Classes
N-Tier: Begginer's guide in
designing their application
Fast, Flexible and Easy to Use
N-tier Software Architecture
ASP.NET Internals: Request
Architecture
N-Tier development with
ASP.NET MVC, WCF, and LINQ
Zombie Explorer : A N-Tier
application from top to bottom
Creating Scalable Architecture
Building an N-Tier Application
in VB.NET, in 8 Steps
Creating a WIX Installer for
ASP.NET Web Applications
Enterprise Application
Architecture with LINQ to SQL
Application architecture
research
Note: The Data Access Layer in this article was written using classic , due to which the
amount of code in is little too much. Nowadays using ORMs like
!" to
generate the is recommended. The code will be generated by ORM itself.
Using the code
Let us develop a small Toy # application that will use n-tier architecture. We will develop
a small Employee Management application for the $%&Database. (For simplicity, I have
removed all other tables from the DB and some columns from the Employee table). This
application should be able to perform the basic CRUD operations on the DB.
The solution for this application will contain separate projects for and . The Data Layer
will be . The Presentation Layer is an ASP.NET website running on top of these
projects.
The Data Layer
The data layer in this example contain only one table called Employee. The data layer also
contains the stored procedures for all the basic operations on the Employee table. So let us look
at the table and all the stored Procedures we have in our Data Layer.
Creating ASP.NET Applications with N-Tier Architecture - CodeProject http://www.codeproject.com/Articles/439688/Creating-ASP-NET-appli...
2 of 12 12-02-2013 09:17
-
Now we will create a set of stored procedures to perform the operations on the Employees Table.
Collapse | Copy Code
!"#$%& '"#$& ("#$ )"#$& '*#$+& '*#$ '"#$, '*#$-& .#
#/
&&$%&$&$$&$+&$$,$
-,$%&$&$$&$+&$$
,$-
0
/1 020
3&0
//41 020
3&/
/41
0 &$ !"#$%& '"#$& ("#$ )"#$& '*#$+& '*#$ '"#$, '*#$-& .#
#/
,
2$%&2%&$
Creating ASP.NET Applications with N-Tier Architecture - CodeProject http://www.codeproject.com/Articles/439688/Creating-ASP-NET-appli...
3 of 12 12-02-2013 09:17
-
&2&$2$&2&$+&2+&$2$,2,$-&2-&
020
Now we have our Data Layer ready.
The Data Access Layer
Now we will go ahead and create a Data Access Layer for our application. The data access layer
will contain 2 main type of classes. A set of classes that will represent the Table entities. And
classes to perform the '()operations on the database.
The class in the above diagram is the Entity that will represent the table.
This class has been created so that the Layers above the will use this class to perform
operations in Employee table and they need not worry about the table schema related details.
Collapse | Copy Code
,&5
&06&+6 &+1&6 &+&6 &+6 ! &+&6 &++&6 &+6 &+,6 &+-&6
,&&05
+5
,0675
02,67
7
,&&+5
+5
,67
Creating ASP.NET Applications with N-Tier Architecture - CodeProject http://www.codeproject.com/Articles/439688/Creating-ASP-NET-appli...
4 of 12 12-02-2013 09:17
-
52,6
77
,&&+%&5
+5
,1&675
1&2,67
7
,&&+&5
+5
,&675
&2,67
7
,&&+5
+5
,675
2,67
7
,&&+&5
+5
,&675
&2,67
7
,&&++&5
+5
,+&675
+&2,67
7
,&&+5
+5
,675
2,67
7
,&&+,5
+5
,,675
,2,67
7
,&&+-&5
+
Creating ASP.NET Applications with N-Tier Architecture - CodeProject http://www.codeproject.com/Articles/439688/Creating-ASP-NET-appli...
5 of 12 12-02-2013 09:17
-
5,-&6
75
-&2,67
77
The class expose the methods to perform the '()operations on the
Employee table.
Collapse | Copy Code
,&85
,&
#5
/9:;2/9:;5
/9
- 2:
-
52#6%
77 5
67
77
,67
'#%()(*+ #,
&&-,&?/&+$$/9:;#
52#6
,&+/9&2/9&0C/03##5
,&+/92##5
26-26+#6
5
&1/I2&/#5
#67
,&+/92/9##
5%
77 5
67
77
,67
'#%)*+)(*+ #,
&&
-,>,&+$$/9:;#
5&,2"6
,&+/9&2/9&0C/03##5
,&+/92##5
26-26+#6
5
&1/I2&/#5
#67
,2-,>,#67 5
67
77
,,A"#67
7
Note: If we use any (,(Object Relation Mapper) then DAL need not be written. The (,will
generate all the DAL code.
!"is one of the best (,available. This DAL can
simply be replaced with a class library containing the
- !" generated Entities
Creating ASP.NET Applications with N-Tier Architecture - CodeProject http://www.codeproject.com/Articles/439688/Creating-ASP-NET-appli...
8 of 12 12-02-2013 09:17
-
and Contexts.
The Business Logic Layer
The business logic layer will have a reference to the DAL and will mainly perform Business rule
validation and business logic specific calculations. In out example, I will write a simple that
will govern the IO between the and Presentation layer. In real applications the will
contain more logic and code.
Collapse | Copy Code
,&=5
-.+/0
82,6
,&=#5
28#67
'#1
#1#
,&&@A3
,3C
'#1
#1#
,&
#5
,#67
'#1
#1#
,&3&&0#5
,3&0#67
'#1
#1#
,&
&0#5
,0#67
'#1
#1#
,&
#5
,#67
7
The Presentation Layer
The presentation layer now contains only a set of pages and code behinds and it will use the
and the the Employee class to perform all the operations. The add Operation can be seen as an
example how the is being used to perform an operation.
Collapse | Copy Code
2#6
2--6%&2-%-62--6&2-&-6,2-,-6
Creating ASP.NET Applications with N-Tier Architecture - CodeProject http://www.codeproject.com/Articles/439688/Creating-ASP-NET-appli...
9 of 12 12-02-2013 09:17
-
+&2-+&-62--6-&2--&-6&2-&-6
==2=#6
&1=#22,#5
2#
&