Simple Web three-tier architecture system "Third Edition"

Source: Internet
Author: User

Today is the third edition, and the same day as the previous day is still to optimize the code, three-tier architecture is a kind of thought, concrete can make the whole system security and high performance, or to see whether the code is written reasonable, logical or not rigorous.

I saw someone write it yesterday. In a three-tier architecture, instead of passing a single argument in a method, it directly declares the whole of an object as a parameter. Finally on-line search, found that the original is in the system added a layer, called the model layer, is used to transfer data between the various layers of the system, so as to avoid the transfer of multiple parameters for a method phenomenon.

Specific in-depth model layer use is still in the study, today, with a little bit of learning a simple model layer of knowledge, the code is once again optimized.

The Prime Minister first establishes a model layer in which to create an entity class (person):

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceModel8 {9      Public class PersonTen     { One         //Employee Number A          Public stringID; -  -          Public stringId the         { -             Get{returnID;} -             Set{id =value;} -         } +  -         //Employee Name +          Public stringname; A  at          Public stringName -         { -             Get{returnname;} -             Set{name =value;} -         } -  in         //Employee Gender -          Public stringsex; to  +          Public stringSex -         { the             Get{returnsex;} *             Set{sex =value;} $         }Panax Notoginseng  -         //Employee Wages the          Public stringsalary; +  A          Public stringSalary the         { +             Get{returnsalary;} -             Set{salary =value;} $         } $  -         //constructor, no parameter -          PublicPerson () the         { -  Wuyi         } the  -         //constructor, passing in a parameter ID Wu          PublicPersonstringID) -         { About              This. ID =ID; $         } -  -         //constructor, passing in three parameters name, sex, salary -          PublicPersonstringNamestringSexstringsalary) A         { +              This. Name =name; the  -              This. Sex =sex; $  the              This. Salary =salary; the         } the  the         //constructor, passing in four parameter IDs, name, sex, salary -          PublicPersonstringIdstringNamestringSexstringsalary) in         { the              This. ID =ID; the  About              This. Name =name; the  the              This. Sex =sex; the  +              This. Salary =salary; -         } the     }Bayi}

Then make a simple change to the previous code, here does not emit all the method changes, the other methods are similar:

First, the model layer is introduced:

using Model;

Default.aspx.cs Code:

1     //Change Employee Information2     protected voidUpdate_click (Objectsender, EventArgs e)3     {4         stringID = This. update_id. Text.trim ();5 6         stringName = This. Update_name. Text.trim ();7 8         stringSex = This. Update_sex. Text.trim ();9 Ten         stringSalary = This. Update_salary. Text.trim (); One  APerson p =NewPerson (ID, name, sex, salary); -  -         if(Pd.update (p)) the         { -              This. Lbl_3.text ="* Change Success! "; -         } -}

Persondao class:

1          Public BOOLUpdate (person p)2         {3             BOOLFlag =false;4 5Sqlparameter[] Paras =NewSqlparameter[]//creating a parameter array6             {7                 NewSqlParameter ("@id", P.id),8                 NewSqlParameter ("@name", p.name),9                 NewSqlParameter ("@sex", P.sex),Ten                 NewSqlParameter ("@salary", P.salary) One             }; A  -             //using the values in the parameter array -             stringsql ="Update person Set [name] = @id, sex = @name, salary = @sex where id = salary"; the  -             if(Sq. ExecuteNonQuery (SQL, paras) >0) -             { -Flag =true; +             } -  +             returnFlag; A}

SqlHelper class (Does not change):

1         /// <summary>2         ///execute the SQL statement with parameter additions and deletions3         /// </summary>4         /// <param name= "SQL" >the SQL statement to execute</param>5         /// <param Name= "Paras" >parameters passed in</param>6         /// <returns>returns the number of rows affected</returns>7          Public intExecuteNonQuery (stringSQL, sqlparameter[] paras)8         {9             intRes;Ten  Onecmd =NewSqlCommand (SQL, Getcon ()); A  -Cmd. Parameters.addrange (paras); -  -res =cmd. ExecuteNonQuery (); -  +             returnRes; -}

* The use of entity classes in small projects, the effect is not obvious, so there are many people say that the entity class is dispensable, but really to the large projects inside, the use of entity classes make the whole system become more fluent, close, logical and better.

Simple Web three-tier architecture system "Third Edition"

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.