Summary When using an object-oriented programming tool to process database issues, you can separate the data management logic and user interface logic by creating a class module, i.e., encapsulate management data in a class module. When the application has multiple interfaces to edit the same data, this method can implement code sharing, when the user modifies the underlying database, the user interface logic of its form does not change. This article describes the basic methods of creating data management logic and user interface logic, giving a generalization model, and a specific implementation method is illustrated by VB6.
Key Forms Forms (Database) Packages (Encapsulation)
Many tools can be used when using the object-oriented programming tool to develop, such as VB, Delphi, PB, VFP, etc. Their common feature is to open data management logic and user interface logic by creating class modules. Data management in class modules, such as connecting the database, create result sets, defining private variables to complete the intermediate access of the current record. In the form, by creating a class instance, the database connection can be completed, and the data transfer between the class module and the form is implemented by the operation of the class properties, and the operation of the result set, such as recorded browsing, addition, deletion, etc. Since the specific result set is not operated in the user interface logic, the specific field is not operated, the operation object is only a class attribute, and all the operations of the result set are encapsulated in the class module, which creates a database and The independent layer between the user interface. With this separation technology, many different forms can be created, all forms use the same class without copying any data management logic, and the code in the class module is shared by multiple forms. In addition, modifications to the underlying database are only performed in the class without any changes to its form based on its form.
Regardless of the use of the programming tool, the basic method of implementing data management logic and user interface logic separation is consistent. In this paper, a generalized processing model is given, and then VB6 is an example, the specific implementation method is explained.
General processing model for implementing data management logic and user interface logic separation
(1) Creating a class instance when loading the form. Complete database connections in class initialization, create result sets, and take the current record to a set of private variables.
(2) When you browse the record in the form, the movement of the results set record the pointer by calling the class custom method. At the same time, in the class module, in a set of private variables, it is ready to pass the data to the form.
(3) In the class module, define an attribute for each field. When reading properties, the corresponding private variable value is sent. When writing properties, the content you want to write privately.
⑷ When reading in the form, read only the corresponding attributes, essence is a private variable, and the current record value is stored in the private variable.
When writing the operation, only the current value is written to the property, and it is written to the private variable, and then the result is written by class custom method.
Its rural relationship is available in the following description:
Form Class Module Load Form Create a class instance class instance Initializing the connection database, creating a result set The current record is a private variable read data substance for the read properties. The current record is stored in the class private variable, the essence of the read property is returned.
Private variable
Writing data is actually written in the write attribute write attribute to write the control value to the private variable, and then call the class custom method to write the result set.
User interface logic data management logic
Second, using VB6 to handle the actual example of the above problem
⒈ Create class cstudent and do the following
(1) Do the following definition:
Private cnnstudent as new adodb.connection
Private mstudent as new adoDb.recordset
Private mstrname as string
Private MSTRSEX AS STRING
Private mintage as integer
Public enum cstudentmovefirstRecord = 0
PreviousRecord = 1
NEXTRECORD = 2
LastRecord = 3
END ENUM
Here, ADOs are used, and three private variables MSTRNAME, MSTRSEX, MINTAGE respectively correspond to Name, SEX, AGE, enumeration type CstudEntMove, including four constants, and prepare for the result set pointer.
(2) Add the following code in the CSTUDENT Class_Initialize ():
Private sub coplass_initialize ()
'Create and open the result set
DIM STRSQL AS STRING
CNNSTudent.Provider = "Microsoft.jet.OleDb.3.51"
CNNSTudent.connectionstring = "c: /access/student.mdb"
CNNSTudent.open
Strsql = "SELECT * from Sname"
MStudent.activeConnection = cnnstudent
Mstudent.cursortype = adopenkeyset
Mstudent.lockType = AdlockOptimistic
Mstudent.open strsql
'Call the getCurrentRecord method, take the result set, currently recorded to private variables
GetCurrentRecord
End Sub
(3) Add the following attribute process:
Public property get name () AS STRING
Name = mstrname
End Property
Public property let name (byval vnewvalue as string)
MSTRNAME = VNewValue
End Property
Public property Get Sex () AS String
SEX = MSTRSEX
End Property
Public Property Let SEX (Byval VNewValue As String)
MSTRSEX = VNewValue
End Property
Public property Get Age () AS STRING
Age = mstrage
End Property
Public Property Let Age (Byval VNewValue As String)
Mstrage = VNewValue
End Property
These six attributes correspond to the field name, SEX, AGE. When you read the property, execute the get procedure and write attributes.
⑷ Define SaveRecord and GetCurrentRecord methods in class modules
Public Sub SAVERECORD ()
Mstudent.edit
Mstudent.fields ("name") = mstrname
MSTudent.fields ("sex") = mintsex
Mstudent.fields ("age") = mstrage
Mstudent.Update
End Sub
Public Sub getcurrentRecord ()
MSTRNAME = MSTUDENT.FIELDS ("Name"). Value
Mintage = mstudent.fields ("age"). Value
mstrsex = mstudent.fields ("sex"). ValueEnd Sub
⑸ Define the Move method in the class module is as follows:
Public Sub Move (MoveType As Integer)
SELECT CASE MOVETYPE
Case 0
Mstudent.movefirst
Case 1
Mstudent.moveprevious
Case 2
Mstudent.movenext
Case 3
Mstudent.movelast
End SELECT
GetCurrentRecord
End Sub
⑹ Add the following code in class Class_Terminate ():
Private sub coplass_terminate ()
Mstudent.close
SET MSTUDENT = Nothing
CNNSTUDENT.CLOSE
Set cnnstudent = Nothing
End Sub
⒉ Do as follows in the form
(1) Add the following code in Form_Load ()
Private sub flow_load ()
Set mclsstudent = new cstudent 'creation class instance
GetData
End Sub
(2) Define getData method
Private sub getdata ()
TXT (0) .Text = mclsstudent.name 'read attribute values to control
TXT (1) .text = mclsstudent.sex 'read attribute value to the control
TXT (2) .text = mclsstudent.age 'Read Properties to Controls
End Sub
(3) Defining the Click event of the Command button
Private Sub Cmdmove_Click (INDEX AS INTEGER)
Mclsstudent.saveRecord
SELECT CASE INDEX
Case 0
Mclsstudent.move firstRecord
Case 1
Mclsstudent.move PreviouSrecord
Case 2
Mclsstudent.move NextRecord
Case 3
Mclsstudent.move LastRecord
End SELECT
GetData
End Sub
⑷ Add the following code in Form_unload ()
Private sub flow_unload ()
Mclsstudent.saveRecord
Unload me
End Sub
For simplicity, the table Sname here is only three fields, SEX, AGE.
It can be seen that all data management is made in class modules. In the form, the results set and field operation are not directly operated, and the operation object is a class attribute and a class method. During the separation of data management logic and user interface logic, the independent layer between them has created the foundation for further development of the program. It is not difficult to add functions such as adding, deleting, undoing on this basis.
The superiority of this method will appear when the application has multiple interfaces to edit the same data. It not only allows most of the code of management data to be shared, simplifies programming, but the modification of the program, especially the change of the underlying database is very easy. Truely playing the advantages of object-oriented programming.
Reference: [US] E.Winemiller, J. Roff B.heyman, R.Groom, Gu Bin, Yang Debin
Tsinghua University Press