When developing database applications in Delphi, you often need to deal with datasets. tdataset provides operations such as browsing and modifying data, which is very convenient to use. However, because of its ease of use, the code is filled with statements such as fieldbyname. Over time, the entire code is difficult to read due to the maintenance and modification of the project, dataset, as the data storage center, often transmits references between multiple object modules. Other modules also modify data in dataset easily, which may cause data confusion and errors.
In actual work, it is found that a better solution to the above problem is to convert dataset data into business objects and put the definition of business objects in a single unit, in this way, business objects are used in other modules, and the code can be easily understood and modified. Maintaining the definition of objects in a unit also facilitates future maintenance.
Therefore, it is critical to convert dataset into business objects. Here I will provide a simple example to hope Neng can help you.
Example:
The business target is people.
Unit umap_peo;
Interface
Uses dB;
Type
Peorec = record // PEO Structure
Name: string;
Age: integer;
Nation: string;
End;
Ppeorec = ^ peorec;
Tpeofield = (name, age, Nation); // PEO data field Definition
Const
Peofieldname: array [tpeofield] of string = (// field name in the PEO Database
'Name', 'age', 'nation'
);
Type
Tpeo = Class
Private
ADS: tdataset;
Public
Constructor create (ADS: tdataset );
Function getprops (prop: tpeofield): variant;
Procedure setprops (prop: tpeofield; const value: variant );
Property prop [prop: tpeofield]: variant read getprops write setprops; default; // default attribute
Property Data: tdataset read ads;
End;
Procedure newpeo (ADS: tdataset; var APEO: peorec );
Implementation
{Tpeo}
Procedure newpeo (ADS: tdataset; var APEO: peorec );
VaR
Adspeo: tpeo;
Begin
Adspeo: = tpeo. Create (ADS );
Try
APEO. Name: = adspeo [name];
APEO. Age: = adspeo [age];
APEO. Nation: = adspeo [nation];
Finally
Adspeo. Free;
End;
End;
Constructor tpeo. Create (ADS: tdataset );
Begin
Inherited create;
Self. Ads: = ads;
End;
Function tpeo. getprops (prop: tpeofield): variant;
Begin
Result: = ads. fieldbyname (peofieldname [prop]). value;
End;
Procedure tpeo. setprops (prop: tpeofield; const value: variant );
Begin
Ads. fieldbyname (peofieldname [prop]). Value: = value;
End;
End.
Tpeo acts as the datasing set of dataset. Like dataset, operations only affect the current record, such:
VaR
PEO: tpeo
.......
Edtname. Text: = peo [name];
Edtage. Text: = inttostr (peo [age]);
Edtnation. Text: = peo [nation];
....
Generally, the module for creating tpeo is the data management center, which centrally manages the data of dataset in tpeo.
To use one or more pieces of data in this dataset, other modules should use data copies of the dataset instead of passing the dataset over, so as to avoid impact on dataset management in the data center.
Use the data copy of dataset, that is, use the peorec record structure, which occupies a small amount of resources, facilitates operations, and transmits values.
VaR
APEO: peorec;
.......
APEO. Name: = 'xxx ';
........
Data copies are passed through dataset objectization, which effectively avoids the scenario where multiple modules in a program attach multiple datasets to each other and effectively divides the responsibilities between modules, after all, many processing operations require objects rather than datasets.
After delphi2005, the compiler supports a new data type records such:
Type
Peorec = record
VaR
Age: integer;
Name: string;
Class VaR
Nation: string;
Procedure sayhello ();
Procedure setage (value: integer );
Constructor create (const aname: string; VAL: integer); // optional
Property ageprop: integer read age write setage;
Class property nationprop: String read nation;
End;
As you can see, records can view the record type as an object, and allow inclusion of processes, methods, and attributes. It is easier to use.