Layer 2 Structure and Its Application
Concept and Environment
The three-layer result development method in ASP. NET is actually the same as Java. In Java, the three-tier architecture is front-end HTML, JSP, and Servlet, the middle layer is JavaBean and EJB, and the back is the database server. In ASP. NET, the front part is HTML, ASP, and aspx, and the middle layer is a. dll Control compiled by files such as. VB and. CS, followed by a database server.
In our three-tier architecture, our database layer connects and operates through the middle layer, the front end transmits parameters to the middle layer, and accepts parameters of the middle layer. In our ASP. NET, we mainly focus on the data interaction between our intermediate layer and the front-end.
We generally refer to the intermediate layer as a component. The component can be compiled using. VB or the. CS file. The intermediate layer is generally a. dll file. Microsoft's. NET technology is simpler than any previous version in this regard, which is also one of its advantages. In the past, we had to register a. dll file, which was a registration or a restart. on. NET, we used the. dll file and didn't have to worry about registration.
Before visual stutio.net is available, we use. BAT file to put. VB and. the CS file is compiled. DLL file, in. in the BAT file, we write the name of the compiled file, the associated namespace, the name of the file to be compiled, and the corresponding command name, and then run it. It sounds complicated. This is what many beginners fear when compiling the first. dll file. But it is easy to do. The following is an example. bat File Syntax. Suppose we have a file name: Saidy. VB file, we need to compile it into Saidy. DLL file, which uses system, system. data, system. data. SQL namespace, we can create a dblink. the bat file contains the following content:
Vbc/out: ../bin/Saidy. dll/T: Library/R: system. dll/R: system. Data. dll/R: system. Data. SQL. dll
Dblink. VB
This is the command for compiling the. vbprogram. If it is to compile the. CS file, the command will be different. We assume there is a Saidy. CS file. According to the above requirements, we compile it as follows:
CS/out: ../bin/Saidy. dll/T: Library/R: system. dll/R: system. Data. dll/R: system. Data. SQL. dll
Dblink. CS
We can see that most of them are the same.
Of course, if we have a Microsoft vs.net programming environment, we don't have to worry about it. We can compile the. dll file as conveniently as compiling VB or VC programs. Microsoft's vs.net is a mass integrator who integrates various languages and can write programs in different languages in this environment. We will introduce specific applications in special chapters.
An example of a three-tier architecture
We use specific examples to illustrate the application of the three-tier architecture. We will build a small project to illustrate this problem. Sometimes for security, we usually encapsulate the connection to the database with a dynamic connection library file, so that we need to connect the write database. VB or. the CS file is compiled into a dynamic Connection Library. DLL file. Even we compile the database-related operation page into A. dll file.
The following is the main part of our database connection and operation file dblink. VB. The connection to the database:
Dim DBL as sqlconnection
For database operations, we write it in a method and return the corresponding value:
Function getdata () as dataview
Dim scomm as sqldatasetcommand
Dim SDS as Dataset
Dim SSTR as string
DBL = new sqlconnection ("Server = localhost; uid = sa; Password =; database = howff ")
SSTR = "select * from color"
Scomm = new sqldatasetcommand (SSTR, DBL)
SDS = new dataset ()
Scomm. filldataset (SDS, "color ")
Return SDS. Table ["color"]. defaultview
End Function
The sixth statement uses the above connection variable with the database. The function of this function is to select all elements from the table "color" and return the form of the table structure. The complete code is as follows:
Imports system
Imports system. Data
Imports system. Data. SQL
'Create namespace
Namespace DB
'Create a class
Public class dblink
'Establish a database connection
Dim DBL as sqlconnection
'Method
Public Function getdata () as dataview
Dim scomm as sqldatasetcommand
Dim SDS as Dataset
DBL = new sqlconnection ("Server = localhost; uid = sa; Password =; database = howff ")
Dim SSTR as string
SSTR = "select * from color"
Scomm = new sqldatasetcommand (SSTR, DBL)
'Fill data
SDS = new dataset ()
Scomm. filldataset (SDS, "color ")
'Back
Return SDS. Tables ("color"). defaultview
End Function
End Class
End namespace
Let's write another front-end to drop the page Saidy. aspx. First we need to introduce the namespace we created:
<% @ Import namespace = "DB" %>
When loading pages, we use this method:
Sub page_load (sender as object, e as eventargs)
'Create a new object
Dim newdb as dblink
Newdb = new dblink ()
'Data Source
Products. datasource = newdb. getdata ()
'Data binding
Products. databind ()
End sub
Let's take a look at our complete code (advanceapp/dblink. aspx ):
<% @ Import namespace = "DB" %>
<HTML>
<Script language = "VB" runat = "server">
Sub page_load (sender as object, e as eventargs)
'Create a new object
Dim newdb as dblink
Newdb = new dblink ()
'Data Source
Products. datasource = newdb. getdata ()
'Data binding
Products. databind ()
End sub
</SCRIPT>
<Body style = "Font: 10pt verdana" bgcolor = "ccccff">
<Br>
<Center>
<H3>. Net-> three-tier architecture! </H3>
</Center>
<Br>
<Center>
<Asp: datalist id = "Products" showheader = false showfooter = false repeatcolumns = "2" repeatdirection = "horizontal" borderwidth = 0 runat = "server">
<Template Name = "itemtemplate">
<Table>
<Tr>
<TD width = "150" style = "text-align: center; font-size: 8pt; Vertical-align: top;
Height: 50 ">
<P>
<% # Databinder. eval (container. dataitem, "ID") %> <br>
<% # Databinder. eval (container. dataitem, "name", "{0: c}"). tostring () %>
</TD>
</Tr>
</Table>
</Template>
</ASP: datalist>
</Center>
</Body>
</Html>
We can see that there are no statements for interacting with the database on this page, so we can encapsulate data operations very well.
3. Use MSMQ
MSMQ (Microsoft Message Queue, Microsoft Message Queue) is an asynchronous transmission mode for communication between multiple different applications. Applications that communicate with each other can be distributed on the same machine, it can also be distributed anywhere in the connected network space. Its implementation principle is: the message sender puts the information he wants to send into a container (we call it message ), then, it is saved to the message queue in a public space of the system. local or remote message receiving programs then extract the messages sent to it from the queue for processing.
There are two important concepts in the message transmission mechanism. One is a message, and the other is a queue. A message is the information that must be transmitted by both parties. It can be a variety of media, such as text, sound, and image. The final way to understand a message is agreed by both parties. The advantage of this is that data is encrypted simply, second, you can use your own defined format to save the amount of communication transferred. A message can contain the sender and receiver IDs, so that only the specified user can see the information that is passed to him and the receipt that is returned for successful operation. A message can also contain a timestamp, so that the receiver can process certain time-related applications. A message can also contain an expiration time. It indicates that a message is voided if it has not arrived within the specified time, which is mainly used for time-based applications.
A message queue is a public storage space for sending and receiving messages. It can exist in memory or physical files. Messages can be sent in two ways, express and recoverable. The difference between express and recoverable modes is that the express delivery mode enables fast delivery of messages, messages are stored in the memory instead of on the physical disk to obtain high processing capability. The recoverable mode is in each step of the transfer process, all messages are written to the physical disk for better fault recovery. Message queues can be placed on the sender and receiver, or separately on another machine. It is precisely because of the flexibility of message queues in the placement mode that the reliability of the message transmission mechanism is formed. When the machine that stores the Message Queue fails and restarts, messages sent in recoverable mode can be restored to the status before the fault occurs, and messages sent by express delivery are lost. On the other hand, using the message transmission mechanism, the sender must worry about whether the receiver is started, whether a fault occurs, and other non-essential factors. As long as the message is successfully sent, the processing can be considered complete, in fact, the other party may not even start the instance, or the next day may have actually completed the transaction.
The advantage of MSMQ is that, due to asynchronous communication, the sender and receiver can execute the remaining code without waiting for the other party to return a successful message, therefore, it greatly improves the processing capability of things. In the process of information transmission, the information transmission mechanism has a certain function of fault recovery; MSMQ's message transmission mechanism makes it possible for both parties to communicate with each other to have different physical platforms.
Using the MSMQ function provided by Microsoft's. NET platform, you can easily create or delete message queues, send or receive messages, and even manage message queues.
In. Net, a MSMQ class library "system. messaging. dll" is provided ". It provides two classes to operate message objects and Message Queue objects respectively. Before using the MSMQ function, make sure that the MSMQ Message Queue component is installed on your machine and that the service is running. When using ASP. NET programming, you should use it in the header:
<% @ Assembly name = "system. messaging" %>
<% @ Import namespace = "system. messsaging" %>
Introduce the MSMQ class library into the ASP. NET File
1. Create a Message Queue
Dim msgque as messagequeue
Msgque = new messagequeue (msgpath)
Msgpath can be a local private queue, for example ". /myqueue "can also be a public queue of other machines, such as" Saidy/777 $/myqueue ", and Saidy is the name of another machine.
2. Message sending
Dim msgque as messagequeue
Msgque. Send (MSG)
Where: MSG is any object.
3. receive messages
Message receiving is divided into two types: synchronous and asynchronous. synchronous receiving extracts the first message received from the Message Queue within the specified time. When there is no message in the message queue, the program is in the waiting state. The asynchronous receiving method defines an event processing function, which is triggered immediately when the first message in the message queue arrives.
1) synchronization mode
Dim MSG as message
Dim FMT as xmlmessageformatter
FMt = ctype (msgque. formatter, xmlmessageformatter)
FMT. targettypenames = new string () {"system. String "}
MSG = msgque. Receive (New timespan (0, 0, 3 ))
First, define the format in which the received message should be converted, and then receive the message within the specified time.
2) asynchronous mode
Dim FMT as xmlmessageformatter
'Define the type of the received message
FMt = ctype (msgque. formatter, xmlmessageformatter)
FMT. targettypenames = new string () {"system. String "}
'Define the message processing function entry
Addhandler msgque. receivecompleted, new receivecompletedeventhandler
(Addressof onreceivecompleted)
'Define message processing functions
Public shared sub onreceivecompleted (S as object, asyncresult as receiveasynceventargs)
Dim msgque as messagequeue = ctype (S, messagequeue)
Dim MSG as message = msgque. endreceive (asyncresult. asyncresult)
'Msg. body is the retrieved message object.
Msgque. beginreceive ()
'Redefine the asynchronous Receiving Method
End sub
'Start asynchronous Receiving Method
Msgque. beginreceive
Message Queue configuration attributes
Queue attributes
Path attribute: it can be used to reference a queue in three ways: path reference, format name reference, and Id reference.
CATEGORY attribute: identifies the type of the currently used queue. Category is the guid value defined by the queue owner. The GUID value can be generated by a guid generation tool or a user-defined numeric value. The GUID value is not unique, so that multiple message queues can be divided into different categories based on the same guid value ).
Attributes related to the data sending type
Formatter attribute: determines the order in which messages are sent and received in a queue, and what content can be sent in a message.
Attributes related to queue Interaction
Denysharereceive attribute: only one component can access messages in the message queue at a time.
Canread and canwrite attributes: determines whether the queue can be read or written.
Maximumqueuesize and maximumjournalsize attributes: set the maximum message capacity of a queue (log queue) in kilobytes. Once the received message reaches this capacity, new messages will no longer be received.
Generally, the maximum value of the message queue is set by the Message Queue administrator. If this value is not controlled, the maximum capacity of the default message queue is unlimited.
Usejournalqueue attribute: sets whether to copy received messages to the log message queue.