Understanding the three-tier structure in ASP. NET

Source: Internet
Author: User

Understanding the three-tier structure in ASP. NET

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 layers: 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.
Fliggy book-http://www.freeeim.com/
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: This layer mainly accepts user requests and returns data to provide client access to applications.

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

View sourceprint? 01 using system;

02 using system. Collections. Generic;

03 using system. text;

04

05 namespace dbentity

06 {

07 Public class userinfo

08 {

09 private int _ id;

10 private string _ username;

11 private string _ password;

12

13 public int ID

14 {

15 get {return _ id ;}

16 set {_ id = value ;}

17}

18

19 Public String Username

20 {

21 get {return _ username ;}

22 set {_ username = value ;}

23}

24

25 Public String Password

26 {

27 get {return _ password ;}

28 set {_ password = value ;}

29}

30}

31}

Add userdal. CS in dal

View sourceprint? 01 using system;

02 using system. Data;

03 using system. Data. sqlclient;

04 using system. configuration;

05 using system. Collections. Generic;

06 using dbentity;

07

08 namespace dal

09 {

10 public class userdal

11 {

12 private string connectionstring = configurationmanager. etettings ["connectionstring"]. tostring ();

13 public userinfo login (string username, string password)

14 {

15 userinfo info = new userinfo ();

16 string strsql = "select ID, username, password from users where username = @ username and password = @ password ";

17 sqlconnection conn = new sqlconnection (connectionstring );

18 conn. open ();

19 sqlcommand COM = new sqlcommand ();

20 com. commandtype = commandtype. text;

21 com. commandtext = strsql;

22 com. Connection = conn;

23 com. Parameters. addwithvalue ("@ username", username );

24 com. Parameters. addwithvalue ("@ password", password );

25 sqldatareader DR = com. executereader (commandbehavior. closeconnection );

26 if (dr. Read ())

27 {

28 info. ID = convert. toint32 (Dr ["ID"]);

29 info. Username = Dr ["username"]. tostring ();

30 info. Password = Dr ["password"]. tostring ();

31 return Info;

32}

33 else

34 {

35 return NULL;

36}

37}

38}

39}

Add userbll. CS to BLL

View sourceprint? 01 using system;

02 using system. Collections. Generic;

03 using system. text;

04 using dbentity;

05 using Dal;

06

07 namespace BLL

08 {

09 public class userbll

10 {

11 userdal Dal = new userdal ();

12 public userinfo login (string username, string password)

13 {

14 return Dal. login (username, password );

15}

16}

17}

Background code corresponding to login. aspx in the Web

View sourceprint? 01 using system;

02 using BLL;

03 using dbentity;

04

05 public partial class _ default: system. Web. UI. Page

06 {

07 protected void page_load (Object sender, eventargs E)

08 {

09

10}

11 protected void button#click (Object sender, eventargs E)

12 {

13 userbll DATA = new userbll ();

14 userinfo info = new userinfo ();

15 info = data. login (textbox1.text, textbox2.text );

16 if (info! = NULL)

17 {

18 // login successful

19 response. Write ("<SCRIPT> alert (OK !) </SCRIPT> ");

20}

21 else

22 {

23 // Login Failed

24 response. Write ("<SCRIPT> alert (error !) </SCRIPT> ");

25}

26}

27}

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

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.