What is the purpose of having a DataModule in your project? Does it have any special properties compared to a normal class or module? What are the conventions of what it's usually used for?
5 Answers
A datamodule is a non-visual "form" (container) that can contain non-visual components (usually database components, though any component can be added, such as dialogs and Indy components) that can then be shared by many forms.
This wiki explains further: Delphi Wikki.
-
+1; the essentials in a simple sentence. I usually have an
TXyzActionDataModule
containingTAction
instances (packed in aTActionList
or something similar) for a group of forms/frames that are related around some commonXyz
feture. Commented Sep 27, 2012 at 21:19 -
+1, and speaking of which, in Delphi XE2, I do recall once trying to add non-database components to a Data Module and only the database related components would show in the palette... Since then I've been able to do so. Commented Oct 1, 2012 at 5:54
It is normally used for Dataset and DataSource objects to keep them in a central location. A DataModule can only contain nonvisual components on it but are not just limited to Data Access components. You could put a TTimer on it and SaveDialog etc...
It can be useful when you have multiple forms, needing to change the code only in one place, instead of every form.
A datamodule differs from a simple class that it can be designed within the form designer and its components can be edited with the object inspector. The datamodule has also a resource that is streamed in a DFM file.
Furthermore a datamodule can be used by other datamodules, forms and frames and then its components are available for selection in the object inspector, too.
In my desktop applications, I usually have one data module containing a bunch of TImageList
s. If my applications uses TCP/IP
, the components are on a separate data module. Of course almost all of my application's logic goes in different data modules. And, if I need to create a service application, I put my logic in a data module so that I can easily create a desktop application around it for debugging. And, last, but least, I put my data access components on data modules most of the time.
Like a resuable visual unit/class sort of thing--a cruicible for non visual components(that are class based).
-
Why are you answering this question with such a vague answer? What did your contribution add to the existing answers? Commented May 26, 2023 at 10:43
-
Simultaneously comparing class and datamodule in terms of reusability of code. Also mentioned the purpose. Vagueness vs. text book definition depends upon at what level you are looking down from. Extra thing is the essence and intuition. Commented May 26, 2023 at 14:40
TService
also derive from a Data Module?