Simple three-tier Program Design

Source: Internet
Author: User


UnderstandingASP. N etLayer-3 structure in 

We use a three-tier structure to make the project structure clearer and the division of labor clearer, which is conducive to later maintenance and upgrade.

Three-tier structure inclusion:Presentation Layer(USL ),Business logic layer(BLL ),Data access layer(DAL)

1: Data access layer: it is the operation layer for raw data (in the form of database or text files that store data), instead

It refers to raw data, that is, operations on data, rather than databases, providing data services for the business logic layer or presentation layer.

2: business logic layer: This layer is mainly used to address specific problems. It can also be understood as a pair of operations on the data layer.

If the data layer is a building block, the logic layer is the construction of these building blocks.

3: Presentation Layer: It mainly indicates the Web mode or winform mode,

If the logic layer is powerful and complete, the logic layer can provide complete services regardless of how the presentation layer is defined and changed.

Specific differentiation methods

1: Data access layer: it mainly depends on whether your data layer contains logic processing. In fact, its functions are mainly completed.

Operations on data files. You do not have to worry about other operations.

2: business logic layer: mainly responsible for operations on the data layer. That is to say, some data layer operations are combined.

3: Presentation Layer: mainly accept user requests and return data to provide applications for clientsProgram.

Layer-3 Structure Description

The perfect three-tier structure requires that you modify the presentation layer instead of the logic layer, instead of the data layer.

Otherwise, it is hard to say whether your application has a multi-layer structure, or whether there is a problem with the Division and organization of the layer structure.

Different applications have different understandings, which is a conceptual issue.

Flowchart

Deploy a three-tier structure

1: Create a blank solution

2: In this solution, add> new project> class library named dbentity (Database entity)

3: In this solution, add> new project> class library named Dal (data access layer)

4: add on the next solution> Create a project> name the class library BLL (business logic layer)

5: add on the next solution> New website> Asp. Net website name website (presentation layer, add a window application for the winform Project)

6: Dal, BLL, and website Add references to dbentity, respectively.

7. Add a reference to Dal for Bll, and add a reference to BLL for website.

 

 

Log on to the demo project as a user.

Dbentity adds userinfo. CS to indicate the database entity, which is generally one-to-one correspondence with the database

Using system; using system. collections. generic; using system. text; namespace dbentity {public class userinfo {private int _ id; private string _ username; private string _ password; Public int ID {get {return _ id ;} set {_ id = value ;}} Public String username {get {return _ username ;}set {_ username = value ;}} Public String password {get {return _ password ;} set {_ password = value ;}}}}

Add userdal. CS in dal

Using system; using system. data; using system. data. sqlclient; using system. configuration; using system. collections. generic; using dbentity; namespace Dal {public class userdal {private string connectionstring = configurationmanager. appsettings ["connectionstring"]. tostring (); Public userinfo login (string username, string password) {userinfo info = new userinfo (); string strsql = "select ID, username, password from users where username = @ username and password = @ password "; sqlconnection conn = new sqlconnection (connectionstring); Conn. open (); sqlcommand COM = new sqlcommand (); COM. commandtype = commandtype. text; COM. commandtext = strsql; COM. connection = conn; COM. parameters. addwithvalue ("@ username", username); COM. parameters. addwithvalue ("@ password", password); sqldatareader DR = com. executereader (commandbehavior. closeconnection); If (dr. read () {info. id = convert. toint32 (Dr ["ID"]); info. username = Dr ["username"]. tostring (); info. password = Dr ["password"]. tostring (); Return Info;} else {return NULL ;}}}}

Add userbll. CS to BLL

 
Using system; using system. collections. generic; using system. text; using dbentity; using Dal; namespace BLL {public class userbll {userdal = new userdal (); Public userinfo login (string username, string password) {return Dal. login (username, password );}}}

The background corresponding to login. aspx in the WebCode

Using system; using BLL; using dbentity; public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {} protected void button#click (Object sender, eventargs e) {userbll DATA = new userbll (); userinfo info = new userinfo (); Info = data. login (textbox1.text, textbox2.text); If (info! = NULL) {// response. Write ("<SCRIPT> alert (OK !) </SCRIPT> ");} else {// logon Failure response. Write (" <SCRIPT> alert (error !) </SCRIPT> ");}}}

Now, a simple three-tier architecture user login is complete!

The level is limited. If you cannot write well, let's give you some advice!

Next, write down the three-tier architecture Extension: The factory model.

Download with source code: threemodelsolution.rar

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.