Layer-3 analysis and Example Analysis

Source: Internet
Author: User

What is a three-tier structure? The so-called layer-3 structure is not a physical layer-3 division, nor a simple module division, but a logical layer-3 structure. It adds an intermediate layer between client and database access, form a three-tier logical structure.

What are the three layers? What are their functions? Three-tier structure includes: Presentation Layer UI, business logic layer BLL, and data access layer DAL. 1. The display layer is the display part of the software, mainly the client, which usually appears as a WEB or form. Main functions: accept user input information, display system output information, and provide users with an interactive interface. 2. At the business logic layer, the main functions of the system are to process the business logic of software and process data. 3. The data access layer is used for database operations, but not for databases.

Why use Layer 3? The design pattern tells us that software design and development should follow the open-closed principle, that is, a software can add functions or code, but cannot be modified. Before using the layer-3 structure, the display layer, business logic layer, and data access layer are all coupled. If we need to change the client content, the business logic layer must be changed, this is not safe, so we need to process the business logic layer separately to reduce coupling between them.

A simple logon example is used to introduce the implementation of Layer 3. Take a look at the relationship diagram between the three layers:

Three layers are linked by passing parameters. Too many parameters will complicate the program. Here we introduce entities for parameter transmission, and the database should exist independently of the data access layer, the relationship diagram needs to be improved. The improved relationship diagram is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + o6hzcWxoZWxwZXLTw9Pat + LXsMr9vt2/fingerprint = "http://www.2cto.com/uploadfile/Collfiles/20140616/20140616091140209.jpg" alt = "\">

Solution:


Let's take a look at how the code at each layer implements their respective functions. U-Layer Code:

Namespace LoginUI {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void btnOK_Click (object sender, EventArgs e) // double-click the event {Login. model. userInfo user1 = new Login. model. userInfo (); // use the entity to pass data user1.UserName = txtUserName. text. trim (); user1.Password = txtPassword. text. trim (); Login. BLL. loginManager mgr = new Login. BLL. loginManager (); // defines a business logic class Login. model. userInfo user4 = mgr. userLogin (user1); // defines the entity to store the logon result (logical layer comparison result) if (user4 = null) {MessageBox. show ("incorrect information"); // incorrect account or password} else {MessageBox. show ("Login User" + user1.UserName); // login }}}}
The U-Layer Code implements two functions to obtain user input information and display system output information. We use user1 to obtain user input information and user4 to obtain lower-Layer Feedback Information of layer B, the system logon information is displayed. The U layer does not have any system logic transaction processing, nor is it directly associated with the database, reducing the system coupling.

Layer D code:

Namespace Login. DAO {public class UserDAO // user data operation class {public Login. model. userInfo SelectUser (Login. model. userInfo user1) // use user1 as the parameter to connect to the Database {string ConnString = @ "Server = GE-PC; Database = Login; User = sa; Password = 123456 ;"; // database connection field using (SqlConnection conn = new SqlConnection (ConnString) // connect to the database {SqlCommand cmd = conn. createCommand (); // cmd. commandText = @ "SELECT UserName, PassWord from users where UserName = @ UserName AND Password = @ Password"; cmd. commandType = CommandType. text; // cmd. parameters. add (new SqlParameter ("@ UserName", user1.UserName); // user name cmd. parameters. add (new SqlParameter ("@ Password", user1.Password); // Password conn. open (); SqlDataReader reader = cmd. executeReader (); // Login. model. userInfo user2 = new Login. model. userInfo (); // connect to the database to get data and put it in user2 while (reader. read () // {user2.UserName = reader. getString (0 ). toString (). trim (); // user2.Password = reader. getString (1 ). toString (). trim () ;}return user2; // return user2 }}}}
The main function of layer D code is to connect to the database and operate the database to return information based on the needs of the upper layer. At this layer, we use user2 to store database information and return the data to the previous layer. There is no system business logic to process this layer, and only database operations are required.

Finally, let's take a look at how layer B implements system functions:

Namespace Login. BLL {public class LoginManager {public UserInfo UserLogin (Login. model. userInfo user1) {Login. DAO. userDAO uDao = new Login. DAO. userDAO (); // instantiate the user data operation class Login. model. userInfo user3 = uDao. selectUser (user1); // if (user3.UserName! = User1.UserName | user3.Password! = User1.Password) // determines whether the account password is correct {return null; // The information is incorrect} else {return user3; // returns the database content }}}}
Layer B is mainly used to implement the system's logical functions. Here, user1 at the U layer is used to store the login username and password, user3 is used to store the username and password in the database, and then the same is determined, implement the login function, and finally return the judgment structure to the U layer for display.

Running result:


Layer 3 solves the coupling problem between data display and database, and enhances the maintainability and flexibility of the software. However, the use of layer-3 is more complex, and the effectiveness of the software is put forward. The three layers are not omnipotent and must be used with caution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.