Three-tier architecture

In the software architecture design, the layered structure is the most common and the most important one. The layering here does not refer to physical layering, but logical layering.

upper level. The purpose of layering is to achieve the idea of ​​"high cohesion, low coupling". It is beneficial to the maintenance, update or transplantation of the system later. usual meaning

The three-tier architecture above is to divide the entire business application into: interface layer, business logic layer, and data access layer. Each layer has clear responsibilities.





Pictures from the Internet

Concept introduction

1. Presentation layer (UI ): In layman's terms, it is the interface displayed to the user, that is, what the user sees and gets when using a system.

2. Business Logic Layer ( BLL ): The operation for specific problems can also be said to be the operation of the data layer and the processing of data business logic.

3. Data Access Layer ( DAL ): The transactions made by this layer directly operate the database , for data addition, deletion, modification, update, search, etc.

presentation layer

Located in the outermost layer (topmost layer), closest to the user. It is used to display data and receive data input by users, and provide users with an interactive operation interface.

business logic layer

The Business Logic Layer is undoubtedly the part that reflects the core value of the system architecture. Its focus is mainly on business rules

System design related to business requirements, such as formulation and implementation of business processes, that is to say, it is related to the domain ( Domain ) logic that the system deals with.

Often times, the business logic layer is also referred to as the domain layer. For example Martin Fowler in

In the book " Patterns of Enterprise Application Architecture ", the entire architecture is divided into three main layers: presentation layer, domain

layer and data source layer. As a pioneer of domain-driven design, Eric Evans has made a more detailed division of the business logic layer, subdivided into the application layer and the domain layer,

The domain logic is further separated from the solution of the domain logic by layering. The position of the business logic layer in the architecture is critical, it is in the digital

Between the data access layer and the presentation layer, it plays a linking role in data exchange. Since layers are a weakly coupled structure, the dependencies between layers are downward

Yes, the bottom layer is " ignorant " to the top layer, and changing the design of the top layer has no effect on the bottom layer it calls. If in hierarchical design,

Following the idea of ​​interface-oriented design, this downward dependency should also be a weak dependency. Therefore, on the premise of not changing the interface definition, the rationale

The desired layered architecture should be a “ drawer ” architecture that supports extraction and replacement . Because of this, the design of the business logic layer is very important for a support

Maintaining a scalable architecture is especially critical because it serves two distinct roles. For the data access layer, it is the caller; for the presentation layer, it is

It is the callee. The relationship between dependencies and dependents is entangled in the business logic layer. How to realize the decoupling of dependencies is in addition to implementing business logic.

Outside the task left to the designer. 

data layer

Data access layer: sometimes called the persistence layer, its function is mainly responsible for database access, and can access database systems, binary files, text

document or XML document.

简单的说法就是实现对数据表的SelectInsertUpdateDelete的操作。如果要加入ORM的元素,那么就会包括对象和数据表之间的mapping,以及对象实体的持久化。

DAL层:

        /// <summary>
        /// 数据库连接字段
        /// </summary>
        static string sqlstr = ConfigurationManager.ConnectionStrings[""].ConnectionString;
        /// <summary>
        /// 执行查询语句
        /// </summary>
        /// <param name="command">查询语句</param>
        /// <returns></returns>
        public static int happ(string command)
        {
            SqlConnection conn = new SqlConnection(sqlstr);
            SqlCommand comm = new SqlCommand(command,conn);
            int result = 0;
            try
            {
                conn.Open();
                result=comm.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                if (conn.State==ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            return result;
        }
        /// <summary>
        /// 执行非查询语句
        /// </summary>
        /// <param name="select">增删改语句</param>
        /// <returns></returns>
        public static DataTable sda(string select)
        {
            SqlDataAdapter sda = new SqlDataAdapter(select,sqlstr);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }

BLL层:

        /// <summary>
        /// 获取指定学生的成绩
        /// </summary>
        /// <param name="id">学号</param>
        /// <returns></returns>
        public static List<Marks> getMarks(int id)
        {
            return MarksService.getMarks(id);
        }

        /// <summary>
        /// 获取全部成绩
        /// </summary>
        /// <returns></returns>
        public static List<Marks> getMarks()
        {
            return MarksService.getMarks();
        }

        /// <summary>
        /// 添加成绩
        /// </summary>
        /// <param name="mark">成绩</param>
        /// <returns></returns>
        public static bool addMarks(Marks mark)
        {
            return MarksService.addMarks(mark);
        }

        /// <summary>
        /// 修改成绩
        /// </summary>
        /// <param name="mark">成绩</param>
        /// <returns></returns>
        public static bool updateMarks(Marks mark)
        {
            return MarksService.updateMarks(mark);
        }

        /// <summary>
        /// 删除成绩
        /// </summary>
        /// <param name="mark"></param>
        /// <returns></returns>
        public static bool deleteMarks(Marks mark)
        {
            return MarksService.deleteMarks(mark);
        }

MODEL层:

 private int sid;

        /// <summary>
        /// 学号
        /// </summary>
        public int Sid
        {
            get { return sid; }
            set { sid = value; }
        }

        private string name;

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string sex;

        /// <summary>
        /// 性别
        /// </summary>
        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }

优点

1、开发人员可以只关注整个结构中的其中某一层;  

2、可以很容易的用新的实现来替换原有层次的实现;   

3、可以降低层与层之间的依赖;   

4、有利于标准化;   

5、利于各层逻辑的复用。 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325521714&siteId=291194637