Very early on, I heard about the three-storey structure. At that time only knew three layer structure is the system interface and the database operation and so on irrelevant program separate. Originally so simple implementation, indeed the legendary three-storey structure AH.
First, let's look at what the three tiers are. The presentation layer (Ui,user Interface), the Business Logic layer (BLL businesslogiclayer), the data access layer (DAL data access layers). The division of three layers is a physical division.
The presentation layer (UI), which is 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.
The Business Logic layer (BLL) is a bridge between the presentation layer and the data access layer. It mainly stores some business processes. That's logic. The main function is to get the data from the Dal and then display it to the UI.
For example, a three-storey structure can be understood in the case of a restaurant.
UI refers to the waiter, theBLL is the chef, theDAL is the buyer.
In the eyes of the customers, only to see the waiter to serve them. Do not know how the background cooks and buyers do. For the above three different roles, no matter what the problem, just need to change an employee can be open as usual.
The advantages of the three-tier architecture, or separation of duties, reduce coupling.
Next, look at a landing instance that uses a three-tier structure. First, you need to declare it. There are many bugs in this example that need to be optimized. But the main idea of showing three layers is enough. is just an example.
Database tables:
Here is the Data module diagram:
The attentive reader will surely find that there is a moudel in addition to the three ui,bll,dal, This Moudel does not belong to any layer, just to better link three layers and exist. This class only stores things that are used in conjunction with the above three classes. Play a coordinating role. the Moudel class, which is the entity class.
Here are some of these levels of relationships.
The attentive reader will surely find that there is a moudel in addition to the three ui,bll,dal, This Moudel does not belong to any layer, just to better link three layers and exist. This class only stores things that are used in conjunction with the above three classes. Play a coordinating role. the Moudel class, which is the entity class.
Here are some of these levels of relationships.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacelogin.model{ Public classUserInfo//entity class for saving user information { Public intID {Get;Set; } Public stringUserName {Get;Set; } Public stringPassword {Get;Set; } Public stringEmail {Get;Set; } }}
U-Layer:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceloginui{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidLabel1_click (Objectsender, EventArgs e) { } Private voidBtnlogin_click (Objectsender, EventArgs e) { Try { stringUserName = TxtUserName.Text.Trim ();//removing data from the user interface stringPassword =txtPassword.Text; Login.BLL.LoginManager Mgr=NewLogin.BLL.LoginManager (); Login.Model.UserInfo User= Mgr. Userlogin (userName, password);//using user interface data to find//If there is no problem, the login is successfulMessageBox.Show ("Login User:"+user. UserName); } Catch(Exception ex)//If the login has an exception, the login fails .{MessageBox.Show (ex. Message); } } }}
B Layer:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceLogin.bll//Business Logic Layer{ Public classLoginmanager { PublicLogin.Model.UserInfo Userlogin (stringUserName,stringPassword) { ///throw new NotImplementedException ();Login.DAL.UserDAO Udao =NewLogin.DAL.UserDAO ();//Create a userLogin.Model.UserInfo user= Udao.selectuser (UserName, Password);//return the corresponding data through the content that is filled in the UI if(user!=NULL)//If there is no data in the database, it is first logged in. 10 points added{Login.DAL.ScoreDAO SDAO=NewLogin.DAL.ScoreDAO (); Sdao.updatescore (UserName,Ten); returnuser; } Else //If the user name is not in the database, the login fails { Throw NewException ("Login Failed"); } } }}
Layer D:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceLogin.dal//Data Access Layer{ classDbutil//SQL statement to save the linked server { Public Static stringConnString =@"Server=zc-pc;database=login; User Id=sa; password=123456"; }}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Data;usingSystem.Data.SqlClient;namespacelogin.dal{ Public classUserdao { PublicLogin.Model.UserInfo Selectuser (stringUserName,stringPassword)//returns a user based on the UI selection { using(SqlConnection conn =NewSqlConnection (dbutil.connstring)) { //Create a Command object and add a commandSqlCommand cmd =Conn. CreateCommand (); Cmd.commandtext=@"SELECT id,username,password,email from USERS WHERE [email protected] and [email protected]"; Cmd.commandtype=CommandType.Text; Cmd. Parameters.Add (NewSqlParameter ("@userName", UserName)); Cmd. Parameters.Add (NewSqlParameter ("@Password", Password)); Conn. Open (); //Open Data LinkSqlDataReader 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 a{User=NewLogin.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 an email, you can also return{User. Email=reader. GetString (3); } } returnuser; } } }}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Data.SqlClient;namespacelogin.dal{ Public classScoredao//10 points added for first time landing { Public voidUpdatescore (stringUserName,intvalue) { using(SqlConnection conn =NewSqlConnection (dbutil.connstring)) {SqlCommand cmd= Conn. CreateCommand ();//Create a Command objectCmd.commandtext =@"INSERT into SCORES (username,score) Values (@UserName, @Score)";//Modifying score table DataCmd. Parameters.Add (NewSqlParameter ("@userName", UserName)); Cmd. Parameters.Add (NewSqlParameter ("@Score", value)); Conn. Open (); Cmd. ExecuteNonQuery (); } } }}
Next, take a look at the results:
The success of the implementation of the situation:
Enter the error message:
Although this is a very small example, 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, which layer of error is changed. Greatly reduces the coupling of the system. Of course, with the level of procedures, maintenance must be much more convenient.
Original link: C # three-tier architecture landing instance
Go C # Three-tier architecture landing instance