It is the most common and most important structure to recognize the hierarchical structure.
Three-tier architecture (3-tier architecture)
The interface layer (user Interface layer) is primarily for the user's request acceptance, as well as the return of the data, providing the client with access to the application.
The business logic layer is primarily a matter of action for a specific problem, and can also be understood as a manipulation of the data layer
The data access layer is primarily an operational layer of non-raw data (in the form of data such as databases or text files)
The data access layer includes entity classes
Data access Classes
Advantages
1, the developer can only focus on the entire structure of one of the layers;
2, can easily use the new implementation to replace the original level of implementation;
3. Can reduce the dependence between layer and layer;
4, in favor of standardization;
5, facilitate the reuse of the logic of each layer.
6, the structure more clear
7, in the late maintenance, greatly reduced maintenance costs and maintenance time
Disadvantages
1, reduce the performance of the system. This is self-evident. Without a tiered structure, many businesses can access the database directly to get the data, and now they have to do it through the middle tier.
2. Sometimes cascade changes are caused. This kind of modification is especially reflected in the top-down direction. If you need to add a feature in the presentation layer to ensure that the design conforms to the layered structure, you may need to add code to the appropriate business logic layer and the data access layer.
3, increase the development cost.
Contract to create a folder under the project App_Code the entity class file to be created, and the Operation class files are placed under the folder change.
Entity class file name = indicates that the action class file name = Table name +data
entity class--The structure object mapped out by the database, the simplest package.
Change the table name of the database to the class name of the class
Turn each column of the database into a member variable and a property in the entity class
Column names are consistent with property names
Example: encapsulation of the Table users entity class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceconsoleapplication2.app_code{ Public classUsers {Private int_ids; Public intIds {Get{return_ids;} Set{_ids =value;} } Private string_username; Public stringUserName {Get{return_username;} Set{_username =value;} } Private string_password; Public stringPassWord {Get{return_password;} Set{_password =value;} } Private string_nickname; Public stringNickname {Get{return_nickname;} Set{_nickname =value;} } Private BOOL_sex; Public BOOLSex {Get{return_sex;} Set{_sex =value;} } PrivateDateTime _birthday; PublicDateTime Birthday {Get{return_birthday;} Set{_birthday =value;} } Private string_nation; Public stringNation {Get{return_nation;} Set{_nation =value;} } }}
View Code
data access classes (Operation classes)--Writes a table's database operations into a single method, which is put into this class for external invocation
Example: Creating a query on a table users, adding an action class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;namespaceconsoleapplication2.app_code{ Public classUsersdata {SqlConnection conn=NULL;//"High seas" promote the scope of conn cmdSqlCommand cmd =NULL; PublicUsersdata ()//constructor, put each method to use, and run each instantiation { stringsql ="server=.; Database=data0216;user=sa;pwd=123"; Conn=NewSqlConnection (SQL); CMD=Conn. CreateCommand (); } //Querying all Methods PublicList<users> SelectAll ()//There is a return value list<users> returns a collection,{List<Users> list =NewList<users> ();//Instantiate a collectionCmd.commandtext="Select *from Users";//Query StatementsConn. Open (); SqlDataReader Dr=cmd. ExecuteReader (); while(Dr. Read ())//There is no no line judgment to read the records directly . { Users u=NewUsers ();//instantiate a Users object, provided that the users entity class has been establishedU.ids= Convert.ToInt32 (dr["IDs"]); U.username= dr["UserName"]. ToString (); U.password= dr["PassWord"]. ToString (); U.nickname= dr["Nickname"]. ToString (); U.sex= Convert.toboolean (dr["Sex"]); U.birthday= Convert.todatetime (dr["Birthday"]); U.nation= dr["Nation"]. ToString (); List. ADD (U); } conn. Close (); returnList//Back to List } //Add a record method Public voidInsert (Users u)//you need to enter a variable of type users{Cmd.commandtext="INSERT into Users values (@username, @password, @nickname, @sex, @birthday, @nation)"; Cmd. Parameters.clear (); Cmd. Parameters.addwithvalue ("@username", U.username); Cmd. Parameters.addwithvalue ("@password", U.password); Cmd. Parameters.addwithvalue ("@nickname", U.nickname); Cmd. Parameters.addwithvalue ("@sex", U.sex); Cmd. Parameters.addwithvalue ("@birthday", U.birthday); Cmd. Parameters.addwithvalue ("@nation", u.nation); Conn. Open (); Cmd. ExecuteNonQuery (); Conn. Close (); } }}
View Code
Exercises
Re-order the student table. such as when S002 deleted after the remaining reorder
S001 S001
S003 S002
S004 S003
ADO "entity class" "Data Access Class"