I've heard of the three-storey structure long before. At that time only know that the three-tier structure is the interface of the system and database operations, and other unrelated programs separate. Originally such a simple realization, indeed the legendary three-storey structure AH.
First, let's take a look at which three floors. The presentation layer (Ui,user Interface), the Business Logic layer (BLL businesslogiclayer), the data access layer (DAL data access Layer). The division of the three layer is the physical division.
The presentation layer (UI), the easiest to understand, is the main interface that the user sees.
Data access Layer (DAL), it is not difficult to understand, mainly responsible for data deletion and modification of the search.
The Business Logic layer (BLL) is a bridge between the presentation layer and the data access layer. It mainly contains some business processes. which is logic. The main function is to get the data from the Dal and then display it on the UI.
For example, a three-storey structure can be understood in a restaurant instance.
The UI refers to the waiter, BLL is the chef, and the Dal is the buyer.
In the eyes of customers, can only see the waiter to serve them. I don't know what the backstage chefs and buyers are doing. For these three different roles, no matter which part of the problem, only need to change an employee can business as usual.
The advantages of the three-tier architecture, or separation of responsibilities, reduce coupling.
Next, look at a landing instance that uses a three-tier structure. First, it needs to be stated. There are many bugs in this instance that need to be optimized. But it's enough for the main idea of a three-tier presentation. is just an example.
Database tables:
This is the Data module diagram:
Careful readers will surely find that, in addition to the ui,bll,dal of these three, there is a moudel, this moudel does not belong to any one layer, but to better link the three-tier existence. This class only stores things that are used in conjunction with the above three classes. Play a coordinating role. Moudel class, which is the entity class.
Here are a few levels of relationships.
The next thing to look at is how they can achieve their respective division of labor.
Entity class:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Login.model
{public
class UserInfo //entity class, for saving user information
{public
int ID {get; set;}
public string UserName {get; set;}
public string Password {get; set;}
public string Email {get; set;}}}
U-Layer:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms; namespace Loginui {public partial class Form1:form {public Form1 () {initializecom
Ponent (); private void Label1_click (object sender, EventArgs e) {} private void Btnlogin_click
(object sender, EventArgs e) {try {string userName = TxtUserName.Text.Trim ();
Remove the user interface data string password = txtPassword.Text;
Login.BLL.LoginManager mgr = new Login.BLL.LoginManager (); Login.Model.UserInfo user = Mgr. Userlogin (userName, password); Use user interface data to find//If there is no problem, login successfully MessageBox.Show ("Login User:" + users).
UserName);
catch (Exception ex)//login Failure If there is an exception landing {MessageBox.Show (ex.
message);
}
}
}
}
B-Layer:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
namespace LOGIN.BLL //business logic layer
{public
class Loginmanager
{public
Login.Model.UserInfo userlogin (String userName, string Password)
{
///throw new notimplementedexception ();
Login.DAL.UserDAO Udao = new Login.DAL.UserDAO (); Create a user
Login.Model.UserInfo user= udao.selectuser (UserName, Password); Returns the corresponding data
if (user!= null) //If there is no data in the database for the first login. Add 10 points
{
Login.DAL.ScoreDAO SDAO = new Login.DAL.ScoreDAO ();
Sdao.updatescore (userName);
return user;
}
else //If the user name is not in the database, login failed
{
throw new Exception ("Login Failed")
;
}}
D-Level:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
namespace Login.dal //data Access layer
{
class dbutil //SQL statement for saving linked server
{public
static string connstring = @ "Server=zc-pc;database=login; User Id=sa; password=123456 ";
}
}
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Data;
Using System.Data.SqlClient; Namespace Login.dal {public class Userdao {public Login.Model.UserInfo selectuser (string userName, Strin
G Password)//return a user {using (SqlConnection conn = new SqlConnection (dbutil.connstring)) based on UI selection {//Create a Command object and add a command SqlCommand cmd = conn.
CreateCommand (); Cmd.
CommandText = @ "Select Id,username,password,email from USERS WHERE username= @UserName and password= @Password"; Cmd.
CommandType = CommandType.Text; Cmd.
Parameters.Add (New SqlParameter ("@userName", UserName)); Cmd.
Parameters.Add (New SqlParameter ("@Password", Password)); Conn. Open (); Open the Data link SqlDataReader reader= cmd.
ExecuteReader (); Login.model.uSerinfo User=null; Used to save the read data while reader.
Read ())//start reading data {if (user==null)//If not, regenerate one {
User=new Login.Model.UserInfo (); } user.id=reader.
GetInt32 (0); User. Username=reader.
GetString (1); User. Password=reader.
GetString (2); if (!reader. IsDBNull (3))//No need to have email, you can return {user. Email=reader.
GetString (3);
} return user;
}
}
}
}
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Data.SqlClient;
Namespace Login.dal
{public
class Scoredao //First login, add 10 points
{public
void Updatescore (string userName, int value)
{
using (SqlConnection conn = new SqlConnection (dbutil.connstring))
{
SqlCommand cmd = conn. CreateCommand (); Create a Command object
cmd.commandtext = @ "INSERT into SCORES (username,score) Values (@UserName, @Score)"; Modify score table Data
cmd. Parameters.Add (New SqlParameter ("@userName", UserName));
Cmd. Parameters.Add (New SqlParameter ("@Score", value));
Conn. Open ();
Cmd. ExecuteNonQuery ();}}}
Next, take a look at the execution results:
Success of execution:
Enter the error message:
Although this is a very small example, but it is enough to learn three layers. There is a bad place to write that can be understood.
Summary: For programs that use a three-tier architecture, what level of error changes. Greatly reduce the coupling of the system. Of course, with the level of the procedure, the maintenance must be convenient for many.