Introduction to variable reference and transmission in Asp.net multi-layer architecture

Source: Internet
Author: User
Recently, it was relatively idle. I used this time to carefully study the multi-layer architecture of Asp.net. The main reference is a copy of wrox. net website programming problem-design-solution>, I personally think this book is good. For developers with some. Net basics, it may seem difficult to understand at the beginning, but after careful research, we will find that this book is an excellent reference manual for engineering applications.
The multi-layer architecture of Asp.net mainly aims to solve the relationship among the data layer, logic layer, and presentation layer. My approach is as follows: first create a datacore base class. The base class encapsulates basic operations of some low-level databases, such as database connection and calling stored procedures. It is worth noting that the stored procedure that calls different functions can be implemented by reloading a function. The following code example:

Protected int runprocedure (string storedprocname, idataparameter [] parameters, out int rowsaffected)
{
Int result;

Connection. open ();
Sqlcommand command = buildintcommand (storedprocname, parameters );
Rowsaffected = command. executenonquery ();
Result = (INT) command. Parameters ["returnvalue"]. value;
Connection. Close ();
Return result;
}

Protected sqldatareader runprocedure (string storedprocname, idataparameter [] parameters)
{
Sqldatareader returnreader;

Connection. open ();
Sqlcommand command = buildquerycommand (storedprocname, parameters );
Command. commandtype = commandtype. storedprocedure;

Returnreader = command. executereader ();
// Connection. Close ();
Return returnreader;
}

Protected dataset runprocedure (string storedprocname, idataparameter [] parameters, string tablename)
{
Dataset dataset = new dataset ();
Connection. open ();
Sqldataadapter sqlda = new sqldataadapter ();
Sqlda. selectcommand = buildquerycommand (storedprocname, parameters );
Sqlda. Fill (dataset, tablename );
Connection. Close ();

Return dataset;
}

Protected void runprocedure (string storedprocname, idataparameter [] parameters, dataset, string tablename)
{
Connection. open ();
Sqldataadapter sqlda = new sqldataadapter ();
Sqlda. selectcommand = buildintcommand (storedprocname, parameters );
Sqlda. Fill (dataset, tablename );
Connection. Close ();
}

The principle is simple. You can understand it at a glance. It is advantageous for future operations.

The second step is to establish a logic layer. This logic layer basically refers to the data layer. After datacore is instantiated, some dataset, datareader, and other statements such as insert, update, and delete are returned for the presentation layer. This logic layer also aims to differentiate different functional modules under the entire project. For example, the user module is named usermodel. CS, and the news module is called newsmodel. CS. Another advantage of the logic layer is that you can create the same object or method that can be instantiated multiple times for the presentation layer. For example, the user class can be called multiple times by the presentation layer for objects queried and created by ID or username.

The presentation layer is used to complete the page logic. It mainly accepts client data and passes it to the logic layer for processing after simple integration and judgment. Similarly, the dataset or datareader passed by the receiving logic layer is displayed on the front-end page.

The relationship between data layers is relatively independent, but continuous.

Independence:

For layers other than the presentation layer, you can take a single object or method out and put it into other projects. This is because each task was previously completed to implement the independent functions in the model. Because the application in similar projects basically does not need to be changed much, especially some more primitive layers, datacore in this example is a typical example.

Continuity:

Data has a strong continuity in the transmission process. For example, in the presentation layer, a dataset is returned Based on the userid in the session, which was originally written as follows:

Presentation Layer:

Dataset userinforrow = objectuser. getuserinfor (int32.parse (session ["userid"]. tostring ()));

Logic layer:

Public dataset getuserinfor (INT userid)
{
Sqlparameter [] parameters = {New sqlparameter ("@ userid", sqldbtype. Int, 4 )};

Parameters [0]. value = userid;

Using (Dataset userinfor = runprocedure ("getuserinfor", parameters, "userinfor "))
{
Return userinfor;
}
}



This can be compiled, but an error is prompted during execution, the type does not match, and there is no error in the syntax. But the error is that the presentation layer transmits an int32, which is indeed an int, 4 in sqlparameter. It was originally thought that such variable types are relatively independent at each layer, however, a problem occurs when data is transmitted between them.

There are two solutions for this problem: Change the presentation layer or change the logic layer. Change the logic layer

Sqlparameter [] parameters = {New sqlparameter ("@ userid", sqldbtype. Int, 32 )};

Change the presentation layer:

Dataset userinforrow = objectuser. getuserinfor (Int. parse (session ["userid"]. tostring ()));

In the two solutions, it is obvious that it is reasonable to change the presentation layer, because the transfer of one variable cannot change the methods that can be called by other presentation layer pages in the logic layer.

Other similar variables and references also encounter similar problems. Although several layers are relatively independent, data transmission is also relatively continuous.

. NET applications on the Web can be very complex and have strong logic. Simple single-page calls are neither. Net nor engineering applications. I am also touched by the tip of the iceberg, hoping to serve as a reference.
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.