Building Web Based N-Tier Applications using C#
This article explores the N-tier and Distributed Application Architecture. Over the last decade, the way applications are designed has evolved and come a long way. We have stand-alone applications, server based applications, client server applications, web based applications, n-tier applications, distributed applications, Peer-to-peer apps, service-oriented Architectures, component based Development and the list goes on. This article discusses the advantages, disadvantages of n-tier applications and methods to implement web based n-tier Applications using C#, .NET
Why N-Tier Applications?
N-Tier Applications can easily implement the concepts of Distributed Application Design and Architecture. The N-Tier Applications provide strategic benefits to Enterprise Solutions.
While 2-tier, client-server can help us create quick and easy solutions and may be used for Rapid Prototyping, they can easily become a maintenance and security night mare.
The N-tier Applications provide specific advantages that are vital to the business continuity of the enterprise. Typical features of a real life n-tier may include the following:
1) Security: Application has Appropriate Authentication, logging and monitoring mechanisms
2) Availability and Scalability: Application should be reliable and should have sufficient fail-over mechanisms ( redundancy) such as fail-over clusters
3) Manageability: Application should be designed to have the capability to Deploy, Monitor and troubleshoot. Methodology to handle Errors, log Errors and provide useful information for problem resolution
4) Easy Maintenance: This is generally achieved by adopting coding standards, deployment standards, modular application design, ‘data abstraction’ and application frameworks.
5) Data Abstraction: Easily make changes to the functionality, business rules with the least amount of impact to the entire applications
The above mentioned points are some of the Key design goals of a successful n-tier application that intends to provide a good Business Solution.
What does it take to build the n-tier Application?
The single most important factor that decides the success of the n-tier application is a through business or domain knowledge.
The second most important access is the technical and design know how. N-tier applications require us to put in a sufficient amount of thought to successfully distribute the overall functionality of the application in the appropriate ‘tiers’.
When n-tier Applications should not be used?
Building and implementing a successful n-tier application requires a lot of Effort, Skill, experience, commitment and Organizational Maturity.
It also implies cost.
Hence a favorable Cost-Benefit Ratio is necessary before you decide to go ahead with the n-tier Application.
What is a n-tier Application?
Simply stated, an n-tier application helps us distribute the overall functionality into various tiers or layers.
For example in a typical implementation you can have one or more of the following layers
1) Presentation Layer
2) Business Rules Layer
3) Data Access Layer
4) Database/Data store
In certain scenarios some of the layers mentioned above may be split further into one or more sub layers.
Each Layer can be developed independently of the other provided that it adheres to the standards and communicates with the other layers as per the specifications.
This is one of the biggest advantages of the n-tier application.
Each layer can potentially treat the other layers as ‘black-box’
In other words, each layer does not care how the other layer processes the data as long as it sends the right data in a correct format.
Building them with C#, .NET:
C#.NET provides us an excellent, robust feature rich platform.
C# being Object Oriented Programming Language it helps in practically laying down the standards. For instance you could create a base class with standard functions and require that all new classes should be derived from this base class. Please see the article on “Inheritance” in this series.
.NET provides type safety, automatic Garbage Collection which is very important in implementing good n-tier apps.
The Logical Building Blocks
The above diagram describes the logical building blocks of the Application.
1) The Presentation Layer: Also called as the client layer comprises of components that are dedicated to presenting the data to the user. For example: Windows/Web Forms and buttons, edit boxes, Text boxes, labels, grids, etc.
2) The Business Rules Layer: This layer encapsulates the Business rules or the business logic of the encapsulations. To have a separate layer for business logic is of a great advantage. This is because any changes in Business Rules can be easily handled in this layer. As long as the interface between the layers remains the same, any changes to the functionality/processing logic in this layer can be made without impacting the others. A lot of client-server apps failed to implement successfully as changing the business logic was a painful process.
3) The Data Access Layer: This layer comprises of components that help in accessing the Database. If used in the right way, this layer provides a level of abstraction for the database structures. Simply put changes made to the database, tables, etc do not effect the rest of the application because of the Data Access layer. The different application layers send the data requests to this layer and receive the response from this layer.
The database is not accessed directly from any other layer/component. Hence the table names, field names are not hard coded anywhere else. This layer may also access any other services that may provide it with data, for instance Active Directory, Services etc. Having this layer also provides an additional layer of security for the database. As the other layers do not need to know the database credentials, connect strings and so on.
4) The Database Layer: This layer comprises of the Database Components such as DB Files, Tables, Views, etc. The Actual database could be created using SQL Server, Oracle, Flat files, etc.
In an n-tier application, the entire application can be implemented in such a way that it is independent of the actual Database. For instance, you could change the Database Location with minimal changes to Data Access Layer. The rest of the Application should remain unaffected.
Many packaged n-tier Applications are created so that they can work the same with SQL Server, Oracle, UDB and so on. In the above pages we have seen the background and the logical design of the n-tier application. Large enterprise apps are typically designed as n-tier applications and large portion of them are web based applications. Therefore they can be viewed in a secure manner, from any PC with a browser. This is a good combination of ease of use and security.
How do the Layers communicate with each other?
Each Layer comprises of one or more components. Each component being a part of the app may communicate with one or more component. The component may “speak” to the other components using one of the many protocols, HTTP, FTP, TCP/IP and mechanisms such as XML/RPC, SOAP, REMOTING etc. The data as such may be “passed” across in many formats such as binary, string , XML. For the purpose of this article we will use XML format for passing data between the components.
Communication Techniques in n-tier ‘.NET’ Apps :
The different layers of the n-tier applications can be located on physically different machines.
Various techniques can be used to communicate between the various layers and components. The most common include
XML Web Services
.NET Remoting
A Web Based N-Tier Application
In the diagram above we observe that the Presentation Layer is made of Web Pages, Web Components and Web Server such as IIS. The end user sees the web pages on a Browser such as IE.
To create a web based n-tier Application using C#, .NET the following steps need to be followed:
1) Define what the Application should do. In other words the functionality of the application.
2) Segregate the application logic. The logic related to the User Interface or presentation layer will be part of the presentation layer, The logic related to the Business Rules goes to the Business Layer and so on.
3) Design the Database Structures, such as Tables, Views and so on. The Database design is a very crucial step and it effects the overall application performance, reliability and usability
4) After this you can design the Data Access Layer. This Layer comprises of code to access the database. The Data Access Layer components may typically be called by the Business Layer Components. The Design also defines how this call should be made. This includes the interface definition, Inputs, Outputs, Data Structure Definition and so on.
5) Similarly Business Application Layer design will define the Components that are part of this layer. Specifications for each component should include the Interface Definition, Inputs, outputs and Data structures (example XML DTD).
6) The Presentation Layer is designed keeping in mind the ease of Use. The User Interface should be intuitive and pleasant. The Presentation Layer may comprise of Web Pages such as .aspx,HTML, and compiled components.
Each of the layers can typically be located on physically separate machines for various reasons such as reliability, security and scalability.
To understand the actual coding aspects for building an n-tier application or a distributed application architecture refer to our articles on Creating Components in C#.
Summary:
In the above article we discussed the evolution of various architectures and specifically n-tier applications and distributed application Architecure.