Three-tier architecture 1

Source: Internet
Author: User
Tags set cookie

One or three-layer frame

Presentation layer: Also called the view layer, with HTML, CSS, JS, jquery

Business Logic Layer: Also called the control layer, which contains the implementation part of the business logic

Data management layer: the part that interacts directly with the database

Ii. formation of a three-tier framework (existing example)

1. Data Management layer

A. Introduction of Model class

After the project is created, the Model directory is established on the project's sibling directory, and the model directory is also created on the hard disk where the project is saved. The model.base is then copied to the model directory set up on the hard disk, and the existing Model.Base.csproj module of the hard disk Model.base line is introduced under the project model.

B. Create a new C # class library module under the project model, the project name is Model.testdb, and the project is saved under the model of the hard drive. The Model.testdb directory is generated in both the hard disk and the project.

* * Each class in the MODEL.TESTDB corresponds to table one by one in the database, and each property in the class corresponds to each field within the table.

2. Business Logic Layer

A, the introduction of the Dal class

Build the Dal directory on your project and hard disk, and copy the Dal.base and utility to the DAL directory on your hard disk. The DAL.Base.csproj and Utility.csproj modules in both directories are introduced under the Dal of the project.

B. Create a new C # class library module under the project's Dal, the project name is Dal.test, and the project is saved under the Dal of the hard disk. The Dal.test directory is generated in both the hard disk and the project.

3. Presentation layer (Web layer)

The project that was created at the beginning of the site project is the module that implements the logic and does not need to be created specifically.

Iv. Compiling and referencing

Before compiling, all the compiled files are stored uniformly in the hard disk directory reference with the project siblings for ease of administration.

1. Compiling model.base

A, first open the Properties page for the Model.base project, specify the name of the assembly and namespace, and then specify the reference directory that is generated with the output path as the project sibling.

B, click on the model.base above the right button to select [Generate] to compile, after the end of the compilation to confirm whether the reference directory has compiled files.

2, Model.testdb needs to need to reference Model.base, open Model.testdb Project--Reference on right--Add a reference-browse to find the hard disk to hold the reference directory of the compiled files-- Select the compilation file to be introduced--OK. You should see the Model.base assembly Com.Model.Base in the reference to the MODEL.TESTDB project.

3, compile Model.testdb, refer to "Compiling Model.base".

4, compile utility, refer to "Compiling Model.base". Utility is a feature that includes the extension of the. NET default feature, which is a base class that does not need to reference other modules.

5, DAL. Base is required to reference multiple modules.

A, 5 modules that begin with Microsfot are copied directly to the reference directory where the compiled files are saved.

B, the introduction of the module includes 5 Microsfot the beginning of the Com.Model.Base and com.utility modules together referenced into the Dal.base module.

C, compile Dal.base, refer to "Compiling Model.base".

6, DAL. Test also needs to refer to multiple modules.

A, the introduction of COM. Model.base, com. DAL. Base, Modeltserdb, and other three modules are referenced together in the Dal.base module.

B, compile the dal.test, and refer to "Compiling Model.base".

7, in the beginning of the creation of Web projects need to introduce the above said there are modules

A, the introduction of COM. DAL. Base, DAL. Test, com. Model.base, Model.tserdb, com. Utility, etc. 5 modules.

B, compile the Web project, and refer to "Compiling Model.base".

---to this three-tier architecture Foundation is complete---

V. Using three-tier architecture examples

1, append the database connection string in the Web. config: Con is the default connection string name, append the new concatenated string name to append the corresponding enumeration class data to the Enums.cs below Model.base.

such as: con = 1,//default

Sq_ruanmou= 2//New add-on connection

2, the table structure in the database is converted to the corresponding class in Model.testdb.

A, set the properties for constructors and fields

This framework supports only a single key field for a field. A federated key field consisting of multiple fields cannot be processed.

public class Rnews:basemodel//data table corresponding classes

{

Public rnews ()//constructor for this class

{

PrimaryKey = "NewsId"; //Support only the key fields of a single field

DataBaseName = Databaseenum.con; Select a database connection string

}

public int NewsId {get; set;} Set the properties for each field in the table

public string Title {get; set;}

public string Text {get; set;}

Public DateTime createdtime {get; set;}

public string Newsclass {get; set;}

public int Viewcount {get; set;}

}

3. Create the corresponding class of the generic structure with curd function in the Dal.test class using the class corresponding to the table in Model.testdb.

public class Rnewsdal//classes corresponding to data tables in the business logic layer

{

public static basedal<rnews> M_rnewsdal = new basedal<rnews> (); Create a curd instance of a table

}

* * M_rnewsdal is the object used in the presentation layer (display layer) for curd.

4. Application of Curd Example

A, Getmodel method of obtaining common query

1) can pass PrimaryKey corresponding value

2) You can pass the conditional expression section behind where

Userinfor user = UserInforDAL.m_UserInforDal.GetModel (string. Format ("Username= ' {0} ' and pwd= ' {1} '", UserName, PWD));

if (user = = null)//getmodel The returned instance, user null, indicates that the query did not return a result.

{

Response.Write ("<script>alert (' Username or password error ');</script>");

}

Else

{

if (chk. Checked)//Save cookies When check box is ticked

{

response.cookies["username"].  Expires = DateTime.Now.AddHours (1); Set Cookie retention time

}

response.cookies["username"].        Value = Username; Save cookies

Response.Redirect ("rnewsm.aspx"); Move to a new development page

}

3) SQL injection processing

A, it is common to use pass-through parameters to prevent SQL injection.

Userinfor user = UserInforDAL.m_UserInforDal.GetModel ("[email protected] and [email protected]", LISTPM);

b, the input content can be checked in advance whether there are illegal characters in the way to prevent SQL injection.

public static bool Sqlinsert (string STRs)//Check the input contents for illegal characters in the method

{

BOOL B = true;

String sql = "Exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare |drop |creat"; Key strings to check for

string[] Sql_c = sql.                 Split (' | '); Change to Group

foreach (Var sl in Sql_c)

{

if (Strs.tolower (). INDEXOF (SL) >= 0)//Find the position of the string in SL in STRs

{

b = false; We're not going to return-1, otherwise.

Break Break out once an illegal string is found

}

}

return b;

}

* * String. IndexOf (String): Specifies the zero-based index of the first occurrence of a Unicode character or string in this instance. If the character or string is not found in this instance, this method returns-1.

* * Call the instrumented statement:

if (Websafe.sqlinsert (username) = = False | | Websafe.sqlinsert (pwd) = = False) {}

False---The test result indicates that there is an illegal string.

Three-tier architecture 1

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.