The Architecture Design for Web Application

On June 21, 2015, in Other Online Technology, by James Liu
3-layers

These days, I have reviewd all my web applications which were finished in the past few years. The biggest web application project is build with .Net and C#. The rest small projects are written in PHP. As .Net provide a powerful framework, the whole project is well structured. I think it will be a good example to show you how to design a web application with a standard and well designed architecture.

When we design a web application, usually for big web application, we will adope the classic three layers architecture. From the top to bottom, three layers are presentation lay, business logic lay (BLL), and data access layer (DAL). Some people also name the DAL as persistence layer in Java.

In the presentation layer, all the logic is taking the responsibility to present the data for users. It could be website html page, xml, or json. In this layer, we will get the requests from outside and response the requests with a certain formatted data. The request could be loading a web page or a RESTful API calling. After we get the request, we will decide how to response the request in the same layer.

In the business layer, there are all business logic code inside. Usually these business logic will determine how data to be created, stored, and updated. But the business lay doesn’t do the real job. It will call DAL to retrieve data from database, write data to database and update data in database.

In the data access layer, the task is quite straight forward. All code in DAL is about how to manipulate data with database. The DAL play a main role between the application and database. In the application, we are using VO (Value Object) to transfer data between layers. However, there is no object oriented thing in database. All data stored in Database are table based. Therefore, one main task in DAL is object relational mapping. The DAL gets data from database and convert the table based data into strongly typed dataset. Here is an example source code to show you a standard DAL class. Below is C# source code example.

namespace JMS.Connection
{
    public class DBConnection
    {
        public static SqlConnection GetConnectionInstance()
        {
			string connectStr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
            var connection = new SqlConnection(connectStr);
            connection.Open();
            return connection;
        }
    }
}

namespace JMS.Service
{
    public class StudentDAL
    {
        public IEnumerable<Student> GetStudentsByName(string name)
        {
            var connection = DBConnection.GetConnectionInstance();
            try
            {
                const string commandString = @"SELECT Student.* FROM Student " +
                                             @"WHERE Student.name = @name;";
				var result = connection.Query<Student>(commandString, new { @name = name });
                connection.Close();
                connection.Dispose();
				var studentVO = result.FirstOrDefault();
                return studentVo;
            }
            catch (Exception)
            {
                connection.Close();
                connection.Dispose();
                return null;
            }
        }
    }
}

What About the MVC?

Lots of people may get confused with three layers architecture and MVC framework (Model-View-Control framework). As the definition on Wiki, Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces. In big web application projects, the MVC framework will be located in the presentation layer. The controller will take the request from outside, and then ask for data from BLL (or DAL directly in some simple web applications). After the controller gets all data, it will prepare one model (or several data models in very special case) and bind it (or send it) to the corresponding view. Usually the relationship between controller and view is one to one. But this is not absolute true. Nowadays, one controller may have several functions to handle different kind of requests, and each function may have its own view.

How to Implement 3-Layers Architecture in ASP.NET, PHP and Java

The 3-Layers Architecture is an industry standard and language irrelevant. Hence, we can implement this architecture with any languages. In ASP.NET, there are lots of framework you can use to build your web application. For example, you can use Entity Framework to build DAL and use MVC 5 to build your website. In PHP, it will be more complicated. To acculturate the development speed, we can use some open source frameworks like CakePHP, Yii, Symfony, or Zend framework (not free). If you are developing Java application, there is a pair of open source frameworks you can use together, the hibernate (for DAL) and spring (for whole 3-layers, has MVC framework built-in).

(Your Comments Are Welcome!)

 

Leave a Reply

WordPress Themes

WordPress Themes