A simple understanding of the Asp.net layer-3 framework

Source: Internet
Author: User
Tags connectionstrings

I used the layer-3 framework during training, especially during quizzes! Because I am a newbie! I don't know much about Layer 3. Next I will only tell you what I know, and I hope you can criticize me a lot.

For three layers, I simply put it down. Three layers, as the name suggests, are composed of three layers: presentation layer (UI) and business logic layer (BLL) data access layer (DAL), the main purpose of the three layers is "High Cohesion, low coupling", its essence lies in

Clear levels of capabilities,CodeHigh readability and reusability.

The presentation layer shows the interface to the user. The business logic layer can be said to be a bridge between the presentation layer and the database access layer. It processes the data business logic and sometimes can write some verification. The database access layer directly performs database operations,

It mainly adds, deletes, modifies, and queries data.

There is a special layer model in the layer 3. This is not a layer. It is an entity class mainly used to encapsulate fields and is the carrier of data.

The following is a detailed introduction. The presentation layer is used to display user interfaces and to "Drive" These interfaces with classes and objects at the business layer. This layer includes the ASPX page, Server Control, user control, and some security-related classes and objects.
The business logic layer is used to access the data layer. It extracts, modifies, and deletes the data at the database access layer, and then places the obtained data in datareader or dataset to return it to the presentation layer, it is mainly used to call a method at the data layer.
In the database access layer, we mainly write some methods for adding, deleting, and modifying data. Specifically, we perform database operations, that is, reading data from the database using SQL statements and then calling the business logic layer.
To put it simply, read the data from the database to the presentation layer, that is, a simple example of adding, deleting, and modifying data. First, write the database connection code in webconfig, for example: <connectionstrings>
<Add name = "connectionsql" connectionstring = "Server = 192.1.1.253; database = newspring; uid = sa; Pwd = sa"/>
</Connectionstrings>

Create an object class library model to write data in the database, for example, private string yourname;
Public String yourname
{
Get {return yourname ;}
Set {yourname = value ;}
}
The next step is the three layers. First, create the dbhelper class in the dal and perform database connection operations first in this class, for example:
Private Static string connectionstring = configurationmanager. connectionstrings ["conntectionsql"]. tostring ();
/// <Summary>
/// Method for connecting to the database
/// </Summary>
/// <Returns> return database connection </returns>
Public static sqlconnection getconn ()
{

If (conn = NULL)
{
Conn = new sqlconnection ();
Conn. connectionstring = connectionstring;
Conn. open ();
}
If (conn. State = system. Data. connectionstate. Closed)
{
Conn. open ();
}
Return conn;
}

Then, write some addition, deletion, modification, and query methods in the Dal, for example:

/// <Summary>
/// Query method: returns a able object using a concatenation string (concatenation string method)
/// </Summary>
/// <Param name = "sqlstring"> SQL statement </param>
/// <Returns> datatable </returns>
Public static datatable selinforetrundtbystring (string sqlstring)
{
Try
{
Conn = getconn ();
Cmd = new sqlcommand ();
Cmd. Connection = conn;
Cmd. commandtext = sqlstring;
Sqldatareader reader = cmd. executereader (commandbehavior. closeconnection );
Dt = new datatable ();
DT. Load (Reader );
Reader. Close ();
Return DT;
}
Catch (exception ex)
{
Throw ex;
}
Finally
{
Conn. Close ();
}
}

Then, write the service class in the Dal to read the data in the database, for example:
/// <Summary>
/// Obtain all the content in the log table. The query page must display all Article Information
/// </Summary>
/// <Returns> Returns a sample set </returns>
Public static ilist <articles> getarticels ()
{
String SQL = "select * from articles ";
Datatable dt = dbhelper. selinforetrundtbystring (SQL );
Ilist <articles> List = new list <articles> ();
Foreach (datarow DR in DT. Rows)
{
Articles = new articles ();

articles. id = (INT) Dr ["ID"];
articles. yourname = Dr ["yourname"]. tostring ();
articles. title = Dr ["titile"]. tostring ();
list. add (articles);

}
Return list;
}
Then create a manager in the BLL layer and call the manager in this class, for example:
/// <Summary>
/// Load data display
/// </Summary>
/// <Returns> </returns>
Public static ilist <articles> getarticles ()
{
Return articlesserivec. getarticles ();
}

Finally, it is the presentation layer. You can call it in. aspx. For example, you can use the gridview to display data. The previous code is bound with Eval, for example:
<Asp: templatefield headertext = "yourname">
<Itemtemplate>
<Asp: Label id = "lB1" runat = "server" text = '<% # eval ("yourname") %>'> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
Its backend is simply bound, for example:
Protected void page_load (Object sender, eventargs E)
{
Gridview1.datasource = articlesmanager. getartilces ();
Gridview1.databind ();
}
This is a very simple process of reading data from the database to the page. The idea is to first connect to the database through webconfig, then, write the dbhepler class in the Dal to judge the data status in this class, and write something such as add, delete, and modify. However

Then, create another class service in the Dal to perform basic operations on the database in this class, and then create a manager class in BLL to call some data acquisition methods in the Dal in this class, then you can call it in the presentation layer.

The method is in a variety of ways.

Summary of layer-3: The layer-3 structure is a strictly hierarchical method, that is, the data access layer can only be accessed by the business logic layer, and the business logic layer can only be accessed by the presentation layer, the user sends the request to the business logic layer through the presentation layer, and the business logic layer completes the relevant business rules and rules

And access the database through the data access layer to obtain the data, and then return the data in reverse order to display the data in the presentation layer.

author: qingapple
motto: constantly reflecting on yourself! Then change it!
technologies of interest :. net, database, JavaScript, C #, Ajax, winform, jquery, extjs
the source of this article: http://www.cnblogs.com/xinchun/
the copyright of this article belongs to the author and blog park a total, welcome to reprint, however, this statement must be retained without the author's consent and the original article connection should be given clearly on the article page; otherwise, the legal liability should be held accountable.
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.