Talking about the variable reference and transfer of the ASP.NET multi-layer architecture

zhaozj2021-02-16  84

It has been almost two weeks old, but because the customer's demand analysis is not discussed, all items have not started. Just use this time to carefully study the multilayer architecture of ASP.NET, the main reference is a <.NET Website Programming Problem-Design-Solution> of Wrox, personal thinking is good. Developers who have a certain .NET basis, it is very difficult to see, but take a closer study, this book is an excellent reference manual for engineering applications.

The multilayer architecture of ASP.NET is mainly to solve the relationship between the data layer, the logic layer, the layer, and the like. My approach is this: first build a base class of Datacore. Some low-level databases are encapsulated in the base class, such as database coupling, calling stored procedures, etc. There is a place in this, it is worth noting that the stored procedure for calling different functions can be invoked 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 returnire;

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 truth is simple, understand it. It is good for future operations.

Second, it is to establish a logical layer. This logic layer is basically an instantiated data layer Datacore to return some DataSet, DataReader, or execute some INSERT, UPDATE, and DELETE. This logic layer is also a different functional module below to distinguish the entire Project. For example, the user module is called UserModel.cs, and the news module is called NewsModel.cs. Another advantage of the logical layer is that the same object or method can be established multiple instantiated multiple times for the representation layer. For example, the user class, the object that is queried by the ID or UserName can be represented by the layer multiple times.

Finally, the function of the layer is said, the function is to complete the page logic. It is mainly to accept client data and then passed through simple integration and judgment to pass to the logical layer processing. Similarly, the DataSet or DataReader transmitted by the logic layer is indicated in the front page.

The relationship between the data is relatively independent, but it is relatively continuous.

Independence:

For several layers other than the layers, a single object or method can be taken out directly from other projects. Because each has been completed in order to achieve independent features in the model. Because applications in similar projects basically do not have much change, especially some relatively raw layers, DataCore in this example is a typical example.

Continuity:

Data has strong continuity during transmission. To give an example, there is such a Dataset in the representation, which is written in the session, originally written like this:

Indicate layer:

DataSet UserinForrow = ObjectUser.getuserinFor (INT32.PARS (session "); 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")) {returnifro}}}

This can be compiled, but when prompted when executed, the type does not match, and there is no error above the syntax. But the error is, indicating that the layer passes is an int32, which is indeed int, 4 in SqlParameter, which originally used the variable type to be relatively independent in every level, but when they pass data between them ,something is wrong.

There are two solutions for this problem, nothing more than changing the representation layer or change the logical layer. Change the logical layer, it is to be changed

SQLParameter [] parameters = {new SQLParameter ("@ userid", sqldbtype.int, 32)}; change representation layer to be changed:

DataSet UserinForrow = Objectuser.getuserinfor (int.parse (session ["userid"]. TOSTRING ()));

Two options are obvious to change the representation of the layer relatively reasonable because it is not possible to change the method in which the logical layer can be called by other representation of the layer page is changed because of a variable.

Other similar variable transfer and references also encounter similar problems, although several hierarchies are relatively independent, but they are relatively continuous on the delivery of data.

The application of .NET can do very complicated, logic is also very strong, simple single page call is not .NET features and cannot be used as engineering applications. I also contacted a little, the horn of the iceberg, hoping to play a role of throwing jade, let everyone laugh.

转载请注明原文地址:https://www.9cbs.com/read-12513.html

New Post(0)