I have also checked the three-layer concept and read it. Below is a simple example of the three-layer architecture. Let's take a look at how it is implemented.
Let's take a look at the Entity class-model essence: the entity class is to complete the function corresponding to the database and the entity class. One class is a table and one attribute is a field!
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace model { public class User { public User() { } private string _name; public string name { set { _name = value; } get { return _name; } } } }
The configuration file is applied to the instance to access the database. Of course, we can use sqlhelper or directly store the database connection code on layer D.
<add name="ConnectionString" connectionString="Data Source=localhost;Initial Catalog=tester;User ID=sa;Password=123456" providerName="System.Data.SqlClient" />
The bottom layer of the layer-data access layer (DAL) must reference the entity class and reference the configuration in essence: adding, deleting, modifying, and querying the content in the database
using System; using System.Collections.Generic; using System.Linq; using System.Text; using model; using System.Data; using System.Configuration; using System.Data.SqlClient; namespace DAL { public class UserDB { public bool User_add(model.User model) { string setting = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); SqlConnection myconn=new SqlConnection(setting); myconn.Open(); SqlCommand cmd=new SqlCommand("insert into dbo.[user]([name]) values(@name)",myconn); cmd.Parameters.AddWithValue("@name", model.name); if (cmd.ExecuteNonQuery()>0) { return true; } else { return false; } } } }
The layer-3 Bridge-business logic layer BLL needs to reference the entity class and data access layer substance: responsible for handling the U layer issues (this example mainly involves operations on the data layer)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DAL; namespace BLL { public class userBLL { DAL.UserDB db = new UserDB(); public bool addUser(model.User model) { return db.User_add(model); } } }
The top layer in Layer 3-the presentation layer UI should reference the entity class and business logic layer essence: The specific solution to the problem
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using BLL; using model; namespace login {public partial class form1: FORM {public form1 () {initializecomponent ();} private void button#click (Object sender, eventargs E) {model. user thisuser = new user (); thisuser. name = tb_username.text.tostring (); BLL. userbll UB = new userbll (); If (ub. adduser (thisuser) {MessageBox. show ("true");} else {MessageBox. show ("false ");}}}}
Relationship between three layers: explanation: the Dal in the above Code is mainly used to operate on the content of the database. Here we add users to the database. Bll mainly calls the operations on the Dal layer and returns the result (true or false) of Adding users to the Dal layer ). In this way, an intermediate layer is added to the client and database to reduce the dependence between the two layers. The UI Layer mainly responds to user needs and calls the adduser method implemented by The bll layer. The Dal layer is actually doing this operation.
C # Three-tier architecture instance