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. BelowCodeExample:
| Protected int runprocedure (string storedprocname, idataparameter [] parameters, out introwsaffected) {Int result; connection. open (); Sqlcommand command = buildintcommand (storedprocname, parameters ); Rowsaffected = command. executenonquery (); R Esult = (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.