. NET resolution Web site three-tier architecture __.net

Source: Internet
Author: User

The so-called three-tier architecture refers to the data Access layer (Layer), the Business Logic layer (Business Logic Layer), the interface presentation layer (English forgot ...)

In Ms.net, ASP.net has a technology called code behind, which has already provided two layers by default. For example, create a Web page file. aspx will automatically have a. cs file to match, here. aspx is the interface presentation layer and. CS is the business logic layer.

So the three-tier architecture is nothing more than an abstraction of a layer of data access, which encapsulates all methods of dealing with the database into the abstract class of the data access layer. Let me give a simple example to describe what a three-tier architecture is, and the interface layer is not said.

One, simple data access layer

public class Admin

{

public static bool Insert (String username,string userpass)

{

Database connection string

SqlConnection conn=new SqlConnection ("...");

SQL statement Operations

SqlCommand cmd=new SqlCommand ("INSERT into admin ...", conn);

SQL statement Parameters

SqlParameter parm1=new SqlParameter ("@User", sqldbtype.varchar,50);

Parm1. Value=username;

...

Conn. Open ();

Executing SQL statement Operations

Cmd. ExecuteNonQuery ();

Conn. Close ();

}

public static bool Update (...)

}

The above abstract an admin class, the class has a static method Insert, the business logic layer if you want to call the layer of method, as long as Admin.insert (This.tbuser.text,this.tbpass.text);

Second, simple business logic Layer

public class WebForm1:System.Web.UI.Page

{

protected System.Web.UI.WebControls.TextBox TextBox1;

protected System.Web.UI.WebControls.TextBox TextBox2;

protected System.Web.UI.WebControls.Button Button1;

private void Button1_Click (object sender, System.EventArgs e)

{

String Username=this. TextBox1.Text;

String Userpass=this. TextBox2.Text;

Admin.insert (Username,userpass);

}

}

Third, improved data access layer

Forestall readers will find that the above abstract class admin Each method must write SqlConnection, SqlCommand such efficiency is not high, then how to improve it. The answer is more than one processing class, the day-to-day operation of the database encapsulated in. For example, a SqlHelper class provided by PetShop3.0, then the method in the Admin class can be written like this

public class Admin

{

public static bool Insert (String username,string userpass)

{

String sql= "insert into admin ...";

Sqlparameter[] Parm=new sqlparameter[2];

parm[0]= New SqlParameter ("@User", sqldbtype.varchar,50);

Parm[0]. Value=username;

...

Sqlhelper.executenonquery realize the ExecuteNonQuery function of SqlCommand

Sqlhelper.conn_string is a database connection string

Sqlhelper.executenonquery (Sqlhelper.conn_string,sql,...);

}

public static bool Update (...)

}

Iv. Improved data access layer

If there is such a situation, if the above Admin class insert method, a lot of parameters, greater than 5, then the business logic layer to pass parameters to a pass slightly. This is obviously very bad. But it's good to do. To abstract a class to encapsulate all the parameters (like a struct in C), the method of the admin class becomes

public class Admin

{

public static bool Insert (Admininfo admininfo)

{

String sql= "insert into admin ...";

Sqlparameter[] Parm=new sqlparameter[2];

parm[0]= New SqlParameter ("@User", sqldbtype.varchar,50);

Parm[0]. Value=admininfo.username;

Sqlhelper.executenonquery (Sqlhelper.conn_string,sql,...);

}

public static bool Update (...)

}

With the simplest data access layer and a slightly improved data access layer, a three-tier architecture is implemented. For example, a user wants to insert a new admin information to the database, and its general execution process is:

(1) User input user name and password point in interface submission

(2) At this point in the business logic layer, it accepts and processes the business. Specifically, the interface layer passed over the parameters packaged and passed to the data access layer, and then called its data access layer method

(3) The data access layer completes the user's operation through the interaction of the database, and returns the operation result to the previous layer

At present, there are some projects are four, five, n-tier. can also encapsulate a data layer interface layer and interface factory class, you can realize completely transparent to the database, that is, whether the database using SQL Server or Oracle, as long as the web.config write a string to identify the automatic transfer. Microsoft's famous PetShop3.0 is a completely typical three-tier architecture classic, interested in the study, to ensure that benefits are not shallow.

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.