| Data Modules | ||||||||||||||||||||||
| How to use the TDataModule class - central location for collecting and encapsulating data access objects, their properties, events and code. | ||||||||||||||||||||||
When developing simple database applications (with one or two forms) it is quite usual to place all the data-access components on a form with data-aware components. But if you plan on reusing groups of database and system objects, or if you want to isolate the parts of your application that handle database connectivity and business rules, then Delphi's Data Modules provide a convenient organizational tool.
This chapter of the free database course for Delphi beginners shows how to use Data Modules in Delphi (ADO) database development to partition an application into user interface, application logic and data.
Data modules provide a formal mechanism for collecting and encapsulating DataSet and DataSource objects, their attributes, events and code (business rules) in one central location. Data modules can contain only nonvisual components and are generally used in database and Web development. They provide Delphi developers a visual way to manipulate and code non-visual components of an application.
Generally, a TDataModule class is used for some of the purposes:
- Sharing data access components and code - Design time visual organization - Business rules centralization - Code maintenance New ... Data Module DataModules are not limited to data access components, they can also contain other nonvisual components, such as TMainMenu, TTimer, TSaveDialog or TPopUpMenu).
![]() You place all the components in the right pane ("Components") of data module's window. The tree view on the left is for navigation purposes. Note: in Delphi versions prior to version 6, the TDataModule class was defined in the Forms unit. Delphi 6 moves TDataModule class to the Classes unit to allow smaller GUI-less applications to be written and to separate OS independent classes. In Delphi 6, the Tree diagram and the Data Diagram are separated. The Diagram page on the Code editor provides visual tools for setting relationships among non-visual (and visual) components. When you select the Data Diagram page (for the first time), you are presented with an empty container, in which you can drag any object from the Tree View and then link those objects. Relationships include parent/child, datasource/dataset and similar. For example, after moving a TADOTable and TADOConnection to the Diagram view, you select the Property connector icon, click the ADOTable and drag to the ADOConnection. The connection will be made: TADOTable.Connection property will point to ADOConnection component - as can be seen in the Object Inspector. ![]() To make the data module available to another unit in the application, select that unit, then choose File|Use Unit to add the data module to the uses clause for the unit. If you have several forms that refer to the same data module, make sure the data module is created before it is referenced from those forms - or you'll get an access violation error.
To the next chapter DB Course Next Chapter >> |
||||||||||||||||||||||


