25

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
  • 7
    @iManBiglari, isn't that every question here?
    – Uwe Raabe
    Commented Sep 27, 2012 at 16:31
  • 2
    +1; great question! Lot's of people don't even think about where a DM could be used for. Commented Sep 27, 2012 at 21:17
  • 1
    It's a basic way to "separate concerns". Normally you use it to keep data access and the UI to display that data separate. Commented Sep 27, 2012 at 22:05
  • 1
    It's hardly separation of concerns. It's nothing more than a way to set properties of components by reading them from a .dfm file. Commented Sep 27, 2012 at 22:36
  • Don't things like a TService also derive from a Data Module? Commented Oct 1, 2012 at 5:56

5 Answers 5

22

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.

2
  • +1; the essentials in a simple sentence. I usually have an TXyzActionDataModule containing TAction instances (packed in a TActionList or something similar) for a group of forms/frames that are related around some common Xyz 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
12

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.

12

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.

8

In my desktop applications, I usually have one data module containing a bunch of TImageLists. 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.

1

Like a resuable visual unit/class sort of thing--a cruicible for non visual components(that are class based).

2
  • 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.
    – user30478
    Commented May 26, 2023 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.