Simple web layer-3 architecture system (3rd edition), web layer-3 Architecture

Source: Internet
Author: User

Simple web layer-3 architecture system (3rd edition), web layer-3 Architecture

 

Today is the third edition. As we did a few days ago, we still need to optimize the code. The three-tier architecture is an idea. Whether the overall system security and high performance can be ensured depends on whether the code is properly written, whether the logic is rigorous.

 

Yesterday, I accidentally saw that in the layer-3 architecture written by others, I did not pass a single parameter in the method, but directly declared the overall parameter passing for an object. Finally, I checked through the Internet and found that I had added a layer in the system called the model layer, which is used to transmit data between the layers of the system, this avoids passing multiple parameters for a method.

The detailed in-depth use of the model layer is still being studied. Today, we will use a simple model layer knowledge to optimize the code again.

 

The Prime Minister first sets up a Model to create an entity class (person) in it ):

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace Model 8 {9 public class person10 {11 // employee id 12 public string id; 13 14 public string Id15 {16 get {return id ;} 17 set {id = value;} 18} 19 20 // employee name 21 public string name; 22 23 public string Name24 {25 get {return name ;} 26 set {name = value;} 27} 28 29 // employee gender 30 public string sex; 31 32 public string Sex33 {34 get {return sex ;} 35 set {sex = value;} 36} 37 38 // employee salary 39 public string salary; 40 41 public string Salary42 {43 get {return salary ;} 44 set {salary = value;} 45} 46 47 // constructor, no parameter 48 public person () 49 {50 51} 52 53 // constructor, input a parameter id54 public person (string id) 55 {56 this. id = id; 57} 58 59 // constructor, input three parameters: name, sex, salary60 public person (string name, string sex, string salary) 61 {62 this. name = name; 63 64 this. sex = sex; 65 66 this. salary = salary; 67} 68 69 // constructor, input four parameters: id, name, sex, salary70 public person (string id, string name, string sex, string salary) 71 {72 this. id = id; 73 74 this. name = name; 75 76 this. sex = sex; 77 78 this. salary = salary; 79} 80} 81}

 

 

Then, make a simple modification to the previous code, and do not change all methods here. Other methods are similar:

 

First introduce the Model ):

using Model;

 

Default. aspx. cs code:

1 // modify employee information 2 protected void update_Click (object sender, EventArgs e) 3 {4 string id = this. update_id.Text.Trim (); 5 6 string name = this. update_name.Text.Trim (); 7 8 string sex = this. update_sex.Text.Trim (); 9 10 string salary = this. update_salary.Text.Trim (); 11 12 person p = new person (id, name, sex, salary); 13 14 if (pd. update (p) 15 {16 this. lbl_3.Text = "* changed successfully! "; 17} 18}

 

PersonDAO class:

1 public bool update (person p) 2 {3 bool flag = false; 4 5 SqlParameter [] paras = new SqlParameter [] // create a parameter array 6 {7 new SqlParameter ("@ id", p. id), 8 new SqlParameter ("@ name", p. name), 9 new SqlParameter ("@ sex", p. sex), 10 new SqlParameter ("@ salary", p. salary) 11}; 12 13 // use the 14 string SQL = "update person set [name] = @ id, sex = @ name, salary = @ sex where id = salary "; 15 16 if (sq. executeNonQuery (SQL, paras)> 0) 17 {18 flag = true; 19} 20 21 return flag; 22}

 

SQLHelper class (unchanged ):

1 /// <summary> 2 // execute the add, delete, modify, and delete SQL statement with parameters 3 /// </summary> 4 /// <param name = "SQL"> Yes the executed SQL statement </param> 5 // <param name = "paras"> input parameter </param> 6 /// <returns> returns the affected number of rows </param name = "paras">/ returns> 7 public int ExecuteNonQuery (string SQL, sqlParameter [] paras) 8 {9 int res; 10 11 cmd = new SqlCommand (SQL, getcon (); 12 13 cmd. parameters. addRange (paras); 16 17 res = cmd. executeNonQuery (); 18 19 return res; 20}

 

* The use of entity classes has little effect in small projects. Therefore, many people say that entity classes are dispensable, but they are actually in large projects, the use of entity classes makes the entire system smoother, closer, and more logical.

 

Related Article

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.