A brief talk on variable reference and transfer in ASP.net multilayer architecture

Source: Internet
Author: User
Tags reference tostring
Asp.net| Variable | The structure to the company has been nearly two weeks, but due to customer demand analysis has not come down, all projects have not started. Just use this time to carefully study the multilayer architecture of ASP.net, the main reference is the Wrox of a <.net WebSite programming Problem-design-solution&gt, personally think this book is good. For developers with a certain. NET base, it may seem difficult to understand at first, but a closer look at this book is an excellent reference manual for engineering applications.

ASP.net's multi-layer architecture is mainly to solve the relationship between data layer, logic layer, presentation layer and so on. My approach is this: first, create a DataCore base class. The base class encapsulates the basic operations of some low-level databases, such as database joins, calling stored procedures, and so on. There's a place in here. It is noteworthy that a stored procedure that invokes different functions can be implemented by overloading 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 DataSet, string tablename)
{
Connection.Open ();
SqlDataAdapter SqlDA = new SqlDataAdapter ();
Sqlda.selectcommand = Buildintcommand (storedprocname, parameters);
Sqlda.fill (DataSet, TableName);
Connection.close ();
}

The reason is very simple, a look will understand. Good for future operations.

The second is to establish a logical layer, which is basically the instantiation of the data-tier datacore after the presentation layer to return some dataset,datareader, or to execute some insert,update,delete statements. This logic layer is also designed to differentiate between the different functional modules under project. For example, the user module named UserModel.cs, News module called NewsModel.cs and so on. Another benefit of the logical layer is that you can create the same object or method for the presentation layer that can be instantiated multiple times. For example, the user class, an object that is queried and created by ID or username, can be called multiple times by the presentation layer.

Finally, the presentation layer, the function of the presentation layer is to complete the page logic. The main is to accept the client data and then through simple integration and judgment, passed to the logic layer processing. Similarly, a DataSet or DataReader received by the logical layer is represented in the foreground page.

The relationship between the data at various levels is relatively independent, but relatively continuous.

Independence:

For several layers outside the presentation layer, a single object or method can be taken directly out of the other project. Because each zengdu is done to implement the independent functions of the model. This is a typical example of DataCore, because applications in similar projects do not have to be changed much, especially in relatively primitive layers.

Continuity:

The data has a strong continuity in the process of transmission. For example, in the presentation layer there is a dataset that is returned UserID according to the session, which I originally wrote:

Presentation layer:

DataSet Userinforrow = objectuser.getuserinfor (Int32.Parse (session["UserId"). ToString ()));

Logical 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 through, but in the execution of the prompt error, type mismatch, there is no error on the syntax. But the error is in, the presentation layer comes in is a Int32, in the SqlParameter is indeed a int,4, originally thought such type of variable is in each level relatively independent, but when they pass data between the time, there are problems.

There are two solutions to this problem, whether to change the presentation layer or to change the logical layer. To change the logical layer, you need to convert

sqlparameter[] Parameters ={new SqlParameter ("@UserID", sqldbtype.int,32)};

Change the presentation layer to read:

DataSet Userinforrow = objectuser.getuserinfor (int. Parse (session["UserId"). ToString ()));

It is more reasonable to change the presentation layer in two scenarios, because it is not possible to change the way the logical layer can be invoked by other presentation layer pages because of the passing of one variable.

Other similar variable passes and references also encounter similar problems, although several levels are relatively independent, but they are also relatively contiguous on the transfer of data.

. NET applications on the Web can be complex and logically strong, and simple single page calls are not a feature of. NET and can not be used as engineering applications. I also touched a point, the tip of the iceberg, I hope to play a role, so that everyone laughed at.



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.